Alright, here it is , and it consists of two classes i have in different files, not sure if it's necessary to have them in seperate files though:
What it's suppose to do is pop up a JFrame, and from there you can select a photo aka a portion of your screen that you would like to search for on the screen (Could be useful for some games), and once it finds it it moves your mouse to the "photo".
Currently i havn't gotten it to work quite as i would like it to, but sometimes it comes really close and other times it is far off, so if anyone could post any advice on whats wrong on that it's be helpful (errors come in RunnerProgram.locatePhoto)
So thanks for all who both judge to see if this would be good to keep of following, and to those who give advice on how to solve errors produced.
import java.awt.*;
import java.awt.event.*;
import javax.swing.JWindow;
public class window extends JWindow implements MouseListener,
MouseMotionListener {
Robot rob;
Toolkit tk = Toolkit.getDefaultToolkit();
Image img, img2;
int x = 0, y = 0, width = 1, height = 1;
public window() {
setBackground(Color.RED);
addMouseMotionListener(this);
addMouseListener(this);
setSize(tk.getScreenSize().width, tk.getScreenSize().height - 50);
try {
rob = new Robot();
} catch (AWTException e) {
}
}
public void setVisible(boolean t) {
img = rob.createScreenCapture(new Rectangle(tk.getScreenSize().width,
tk.getScreenSize().height));
if (t)
super.setVisible(true);
else {
img2 = rob.createScreenCapture(new Rectangle(x+1, y+1, width-1, height-1));
super.setVisible(t);
}
}
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
g.drawRect(x, y, width, height);
}
public Image getImage() {
return img2;
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
x = MouseInfo.getPointerInfo().getLocation().x;
y = MouseInfo.getPointerInfo().getLocation().y;
width = 0;
height = 0;
repaint();
}
public void mouseReleased(MouseEvent e) {
repaint();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
width = MouseInfo.getPointerInfo().getLocation().x - x;
height = MouseInfo.getPointerInfo().getLocation().y - y;
repaint();
}
public void mouseMoved(MouseEvent e) {}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.ArrayList;
import javax.swing.*;
public class RunnerProgram extends JFrame implements ActionListener,
KeyListener {
BufferedImage photo = null, i;
Robot rob;
static boolean windowisup = false, bfollow = false;
Point point = new Point(500, 500);
static JFrame frame = new JFrame("Visual/Photo recognition");
JWindow p = new JWindow();
window w = new window();
double prob = 0;
static JButton getScreen = new JButton("Get Photo"), follow = new JButton(
"Follow Image"), followandclick = new JButton("Follow and Click");
Toolkit tk = Toolkit.getDefaultToolkit();
Timer following = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
locatePhoto();
rob.mouseMove(point.x, point.y);
following.stop();
}
});
public RunnerProgram() {
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
try {
rob = new Robot();
} catch (AWTException e) {
}
follow.addActionListener(this);
getScreen.addActionListener(this);
followandclick.addActionListener(this);
frame.setLayout(new FlowLayout());
frame.setSize(tk.getScreenSize().width, 50);
frame.getContentPane().add(getScreen);
frame.getContentPane().add(follow);
frame.getContentPane().add(followandclick);
frame.setLocation(0, 0);
frame.pack();
frame.setVisible(true);
frame.setAlwaysOnTop(true);
}
protected void locatePhoto() {
photo = (BufferedImage) w.getImage();
if (photo == null)
return;
for (int q = 0; q < 1024; q++) {
for (int j = 0; j < 768; j++) {
if (areSimilar(photo, q, j)) {
System.out.println("Found One");
point = new Point(q + 5, j + 5);
return;
}
}
}
System.out.println("Hit edge of screen");
following.stop();
}
private boolean areSimilar(BufferedImage pic, int q, int j) {
prob = 0;
int c = pic.getRGB(0, 0);
int red = (c & 0x00ff0000) >> 16;
int green = (c & 0x0000ff00) >> 8;
int blue = c & 0x000000ff;
Color color = new Color(red, green, blue);
// if(q==50)
// System.out.println(color+" "+rob.getPixelColor(q,j));
if (!rob.getPixelColor(q, j).equals(color))
return false;
// System.out.println("Gets to this point");
BufferedImage pic2 = rob.createScreenCapture(new Rectangle(photo
.getHeight() + 100, photo.getWidth() + 100));
for (int i = 0; i < pic.getWidth(); i++)
for (int we = 0; we < pic.getHeight(); we++) {
// FIX THIS\/\/\/
c = pic.getRGB(i, we);
red = (c & 0x00ff0000) >> 16;
green = (c & 0x0000ff00) >> 8;
blue = c & 0x000000ff;
color = new Color(red, green, blue);
int a, b, g, d;
a = pic2.getRGB(i, we);
g = (a & 0x00ff0000) >> 16;
b = (a & 0x0000ff00) >> 8;
d = c & 0x000000ff;
if (!color.equals(new Color(g, b, d))) {
// System.out.println("Kicks Here");
return false;
} /*else {
// System.out.println("This looks the same");
prob += (pic.getHeight() * pic.getWidth() / 100);
if (prob >= 90)
return true;
}*/
}
System.out.println("Does");
return true;
}
public static void main(String args[]) {
new RunnerProgram();
window w = new window();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == getScreen) {
windowisup = !windowisup;
if (windowisup) {
getScreen.setLabel("OK");
w.setVisible(true);
} else {
getScreen.setLabel("Get Photo");
w.setVisible(false);
}
}
if (e.getSource() == follow) {
bfollow = !bfollow;
if (bfollow)
following.start();
else
following.stop();
}
}
@Override
public void keyTyped(KeyEvent e) {
if (e.getSource().equals(KeyEvent.VK_SPACE))
following.stop();
}
@Override
public void keyPressed(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
}