Robot rob = new Robot();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rec = new Rectangle(dim);
Thread.sleep(3000L);
BufferedImage bim = rob.createScreenCapture(rec);
/**
* x,y,r,g,b
*/
List<int[]> pointRgbList = new ArrayList<int[]>(bim.getWidth() * bim.getHeight());
for (int x = 0; x < bim.getWidth(); x++) {
for (int y = 0; y < bim.getHeight(); y++) {
int rgb = bim.getRGB(x, y);
pointRgbList.add(new int[]{x, y, (rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF});
}
}
Collections.sort(pointRgbList, new Comparator<int[]>() {
/**
* red first
*/
public int compare(int[] o1, int[] o2) {
return (o2[2] - o2[3] - o2[4]) - (o1[2] - o1[3] - o1[4]);
}
});
// draw red pixels
final BufferedImage toDraw = new BufferedImage(bim.getWidth(), bim.getHeight(), bim.getType());
Iterator<int[]> iter = pointRgbList.iterator();
while (iter.hasNext()) {
int[] e = iter.next();
if (e[2] <= 0) { // not red
break;
}
toDraw.setRGB(e[0], e[1], bim.getRGB(e[0], e[1]));
}
JFrame jf = new JFrame();
jf.add(new JScrollPane(new JLabel(new ImageIcon(toDraw))));
jf.pack();
jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
jf.setVisible(true);