Get accurate coordinates within JPanel in a JFrame

47 views Asked by At

How does one get accurate xy coordinates from a JPanel placed within a JFrame.

I tried figuring this out on my own but the coordinates are slightly off and I dont know why. I know this probably isn't efficient code. I'm still learning and just trying out different things for fun.

I used a Mouse event within the windowpane to change the title of the window to the coordinates of the mouse. The overall goal is to make it easier for me to get the xy coordinates of the place I want to place a component.

Despite not needing it, also made a inner class for the Rectangle because I wanted to play around with inner classes. I am not sure if thats what is messing it up.

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import javax.swing.*

public class GetCoordinates extends JFrame {
    private Dimension size;

    public GetCoordinates(int x, int y) {
        size = new Dimension(x, y);

        setTitle("Click to get coordinates");
        setSize(size);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel windowPane = new JPanel();

        windowPane.addMouseListener(new MouseAdapter() {     
            @Override
            public void mouseClicked(MouseEvent e) {

                int x = e.getX();
                int y = e.getY();
                
                setTitle(String.format("x: %s, y: %s", x, y));
            }
        });
        GetCoordinates.Rect rectangle = new GetCoordinates.Rect(100, 100);
        windowPane.add(rectangle);
        add(windowPane);
    }
  
    public class Rect extends JComponent {
        private int x, y;
        private final int SIZE = 50;
        
        public Rect(int x, int y) {
            this.x = x;
            this.y = y;
            
            setPreferredSize(size);
        }
        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            
            Rectangle2D.Double r = new Rectangle2D.Double(x, y, SIZE, SIZE);
            g2d.setColor(Color.BLUE);
            g2d.fill(r);
        }
    }
    public static void main(String[] args) {
        GetCoordinates gc = new GetCoordinates(640, 480);
        gc.setVisible(true);
    }
}

1

There are 1 answers

1
queeg On

Getting the mouse location for a component will deliver the coordinates relative to the component's top left location. If you ask the container's location you may get different coordinates for the same point, and this continues up to the root.

If you want to translate coordinates from one component to another, it is best to go via absolute coordinates (the screen). There are a few methods that help you with that:

But looking overall it seems you want to use the coordinates to place components on absolute coordinates. Would this kind of involve your window is not resizable, and the whole layout is very static? Usually you would offload the calculation of coordinates to LayoutManagers, and you just pass on the rules how to place components (as in 'this component will be on the bottom of the window, from the very left to the very right. Grow as needed/possible).