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) {}
}