Search code examples
javaimagecoordinatesscreenshotarea

Java - Capturing screenshot with screen coordinates


I'm trying to capture area screenshots based on user entered coordinates. Basically user clicks on the screen gets x,y coordinates then clicks elsewhere for another pair of x,y coordinates which are then are put into a rectangle and create a screen capture using the robot library.

Problem i'm having is that i'm getting random screenshots which are not of the coordinates the user has entered and how can I account for coordinates that include 0 since rectangle values must be over 1.

Here my code so far:

try
        {
            Robot robo=new Robot();
            imgAddress=getFilePath();
            while(y2==-1 || y1==-1)
            {
                mouseInput();
            }
            Rectangle captureSize=new Rectangle(getX1(), getY1(), getX2(), getY2());
            System.out.println(captureSize.toString());

            BufferedImage image=robo.createScreenCapture(captureSize);

            ImageIO.write(image,"png",new File(imgAddress));
        }

Solution

  • My bet is that you are using the constructor for Rectangle which accepts point coordinates and dimensions, and you are passing in two points coordinates.