How can I “control” the center of the screen?

In Java, I want to essentially focus on 1 pixel at the dead center of the screen and detect / trigger an action if a color change occurs (for example, the center focuses on a white background, and suddenly I open a green background that 1 pixel was detected as a transition from white → green). I know that I will need the height and width of the resolution and determine the center.

The only problem now is that I don’t know what code I will need to go further to this process. Can someone guide me through what I can do? I know this is pretty simple as it does not contain any code.

+4
source share
1 answer

Here is a quick and dirty example, maybe this helps:

public class PixelBot { private final Robot bot; private boolean running = true; private int lastPixelValue = 0; public static void main(String[] args) throws Exception { new PixelBot(); } public PixelBot() throws AWTException { this.bot = new Robot(); this.runInBackground(); } private void checkPixel() { Rectangle areaOfInterest = getAreaOfInterest(); BufferedImage image = bot.createScreenCapture(areaOfInterest); int clr = image.getRGB(0, 0); if (clr != lastPixelValue) { int red = (clr & 0x00ff0000) >> 16; int green = (clr & 0x0000ff00) >> 8; int blue = clr & 0x000000ff; System.out.println("\nPixel color changed to: Red: " + red + ", Green: " + green + ", Blue: " + blue); Toolkit.getDefaultToolkit().beep(); lastPixelValue = clr; } else { System.out.print("."); } } private Rectangle getAreaOfInterest() { // screen size may change: Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // half of screen, minus 1 pixel to be captured: int centerPointX = (int) (screenSize.getWidth() / 2 - 1); int centerPointY = (int) (screenSize.getHeight() / 2 - 1); Point centerOfScreenMinusOnePixel = new Point(centerPointX, centerPointY); return new Rectangle(centerOfScreenMinusOnePixel, new Dimension(1, 1)); } private void runInBackground() { new Thread(new Runnable() { @Override public void run() { while (running) { checkPixel(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } public void stop() { this.running = false; } } 
+4
source

All Articles