I was playing around with
Java Robot Class and decided to try and make
another bot! I'm not posting the full source simply because I don't think it's that useful. But part of it is. I basically used some loops to find sequences of colors or graphics. In the example below it'll find facebook's favicon (for testing). It takes a little bit of time (took me 16 seconds) so if you can think of any improvements so I don't have to test every pixel that would be great!
import java.awt.*;
class urlGathering {
private static String intToHex(int y) {
String numeric = "0123456789ABCDEF";
String result;
int r = y % 16;
if ((y-r)== 0) {
result = numeric.substring(r, r+1);
} else {
result = intToHex((y-r)/16) + numeric.substring(r,r+1);
}
return result;
}
private static String colorToHex(Color y) {
return intToHex(y.getRed())+intToHex(y.getGreen())+intToHex(y.getBlue());
}
public static boolean isColorMatch(Robot myBot, int x1, int y1, String[][] list) {
Color color;
for(int y2 = y1;y2<(list.length+y1);y2++) {
for(int x2 = x1;x2<(list[y2-y1].length+x1);x2++) {
color = myBot.getPixelColor(x2,y2);
if(colorToHex(color).compareToIgnoreCase(list[y2-y1][x2-x1]) != 0) {
return false;
}
}
}
return true;
}
public static int[] findColorSequence(Robot myBot, String[][] list){
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
for(int x = 0;x<screenSize.width;x++){
for(int y = 0;y<screenSize.height;y++){
if(isColorMatch(myBot, x, y, list)) {
return new int[] {x, y};
}
}
}
return new int[] {-1,-1};//return ret;
}
public static void main(String[] args) throws AWTException, InterruptedException {
//Thread.sleep(6000);
long startTime = System.nanoTime();
Robot myBot = new Robot();
int[] pixels = findColorSequence(myBot, new String[][] {
{"3b5998","3b5998","6078ab","ebeef4","ffffff","ffffff"},
{"3b5998","3b5998","ebeef4","ffffff","ffffff","ffffff"},
{"3b5998","3b5998","ffffff","ffffff","3b5998","3b5998"},
{"3b5998","3b5998","ffffff","ffffff","3b5998","3b5998"},
{"ffffff","ffffff","ffffff","ffffff","ffffff","ffffff"},
{"ebeef4","ebeef4","ffffff","ffffff","ebeef4","ebeef4"},
{"3b5998","3b5998","ffffff","ffffff","3b5998","3b5998"},
{"3b5998","3b5998","ffffff","ffffff","3b5998","3b5998"},
{"3b5998","3b5998","ffffff","ffffff","3b5998","3b5998"},
{"6d84b4","6d84b4","ffffff","ffffff","6d84b4","6d84b4"},
{"6d84b4","6d84b4","ffffff","ffffff","6d84b4","6d84b4"}
});
System.out.println("Found:\n\tX:"+pixels[0]+"\n\tY:"+pixels[1]+"\nNanoseconds: "+(System.nanoTime()-startTime));
}
}On another note, I realized multi dimensional arrays are kinda messed up.... if your actually writing them out on paper you might do something like:
[0][1][2]
[1][a][b]
[2][c][d]
if it's x,y... then 2,1 would be "b" but for an array it's backwards!! So visual help should be drawn sideways! lol