Search code examples
javaappletmouseeventawtrobot

Robot class clicks within confines of applet


For me, the robot class works but using robot.mouseMouse(x, y); will be the x and y of the screen. How do I make it only do the confines of the applet itself? So in other words the coordinates 1,1 would usually be the top left of the screen, but instead I want it to be the top left of the applet..

How is this achievable?


Solution

  • You could calculate the position. You can ask for it's Location

    myApplet.getLocation();
    

    And for its size

    myApplet.getSize();
    

    That way you can create a method

    public void mouseToPosition(int x, int y){
        int zeroPosX = myApplet.getLocation().getX();
        int zeroPosY = myApplet.getLocation().getY();
    
        int newPosX = zeroPosX + x;
        int newPosY = zeroPosY + y;
    
        ...
        //now you have to check if it's in the bounds of the Applet, maybe throw an error
        //and you can add your click/move/whatever robot logic
    }