Search code examples
javaswinggraphics2dawtrobot

How could I show X & Y mouse position while the mouse is moving with Graphics2D?


How could I show X & Y mouse position while the mouse is moving with Graphics2D?

I'm trying to show the coordinates when the mouse is moving, but I can do it with System.out.println, I have in mind using drawString.join("", 10, 5).

How could I reach that then?

*Here's what I did

public class Bell2 extends JPanel {
    static JFrame frame=new JFrame();


    public Bell() {

    }

     public void paint(Graphics g){
            Graphics2D g2=(Graphics2D)g;
            g2.setColor(Color.yellow);
            //Here's where I struggle
            g2.drawString.join ("mouseX, mouseY, C");


     }
    public static void main(String[] args) {

        frame.setSize(500,300);
        frame.setLocation(300,200);
        frame.setVisible(true);
        frame.setBackground(Color.black);

         Robot robot = null;
            try {
                robot = new Robot();
            } catch (AWTException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
              Color c = robot.getPixelColor(456,141);


        double mouseY=1.0;
        double mouseX=1.0;
        while(mouseX !=0 || mouseY  !=0) {

            mouseX = MouseInfo.getPointerInfo().getLocation().getX();
             mouseY = MouseInfo.getPointerInfo().getLocation().getY();

             System.out.println("x: "+mouseX+" y: "+mouseY+" c: "+c);

        }

    }

}

Solution

  • Not sure if this is exactly what you're looking for, but, alternatively, you could consider doing something like this, if you want to stay fairly close to your original example:

        public static void main(String[] args) {
            frame.setSize(500,300);
            frame.setLocation(300,200);
            frame.setVisible(true);
            frame.setBackground(Color.black);
    
            try {
                final Robot robot = new Robot();
                handleMouse(robot);
            } catch (final AWTException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        private static void handleMouse(final Robot robot) {
            int mouseX = 1;
            int mouseY = 1;
            while (mouseX !=0 || mouseY !=0) {
                final Point mouseLocation = MouseInfo.getPointerInfo().getLocation();
                mouseX = mouseLocation.x;
                mouseY = mouseLocation.y;
                final Color currentColor = robot.getPixelColor(mouseX, mouseY);
                System.out.println(String.format("x: %d, y: %d, c: %s", mouseX, mouseY, currentColor));
            }
        }
    

    Note that the currentColor is getting updated each time the mouseX and mouseY are; this was not the case in your original snippet.

    Another thing to note if you're watching the output on the terminal - the color will only appear to change if mouseX and mouseY remain <= 255; beyond that value, you'll likely only see this output:

    java.awt.Color[r=255,g=255,b=255]