NiNoes
NiNoes

Reputation: 47

How can I get the mouse coordinates relative to a JFrame rather than the entire screen

Question: I want to get the coordinates of my mouse relative to the JFrame instead of to the screen. I am using a screen with an array of JPanels that change colour when I click on them. How can I do this?

Most of the solutions I've been able to find suggest getting the coordinates of the component, but I can't do that as this would mean it would find the coordinates relative to each JPanel as well?

Code:

    public TestCoordinates() {
            initComponents();
            JPanel[][] arrGrid = new JPanel[3][4];
            int tileColour = 0;
    
            //nested for loops create the grid
            for (int y = 0; y < 3; y++) {
                tileColour++;
                for (int x = 0; x < 4; x++) {
                    arrGrid[y][x] = new JPanel();
                    add(arrGrid[y][x]);
                    arrGrid[y][x].setBounds((x * 100) + 100, (y * 100) + 100, 100, 100);
                    arrGrid[y][x].setVisible(true);
    
                    //sets grid colour to alternating colours
                    tileColour++;
                    double tileColourDouble = tileColour;
                    double tileColour2 = tileColourDouble / 2;
                    arrGrid[y][x].setBackground(Color.cyan);
                    if ((tileColour2 == Math.floor(tileColour2)) && !Double.isInfinite(tileColour2)) {
                        arrGrid[y][x].setBackground(Color.green);
                    } else {
                        arrGrid[y][x].setBackground(Color.cyan);
                    }
    
                    //adds mouse listener
                    arrGrid[y][x].addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            int yCo = getRow(e.getLocationOnScreen().y - 100);
                            int xCo = getColumn(e.getLocationOnScreen().x - 100);
                            System.out.println(e.getLocationOnScreen());
                            System.out.println(yCo + ", " + xCo);
                            arrGrid[yCo][xCo].setBackground(Color.gray);
                        }
    
                        //converts screen location to single x-coordinate of clicked JPanel's y-coordinate
                        private int getRow(int y) {
                            int row = 0;
                            for (int i = 0; i < y / 100; i++) {
                                row++;
                            }
                            return row;
                        }
    
                        //converts screen location to single x-coordinate of clicked JPanel's x-coordinate
                        private int getColumn(int x) {
                            int column = 0;
                            for (int i = 0; i < x / 100; i++) {
                                column++;
                            }
                            return column;
                        }
                    });
                }
            }
        }

Upvotes: 0

Views: 99

Answers (1)

camickr
camickr

Reputation: 324088

I want to get the coordinates of my mouse... I am using a screen with an array of JPanels that change colour when I click on them.

You don't need the mouse coordinates. You can just use the getComponent() method of the MouseEvent to get the component you clicked on. Then change the background.

arrGrid[y][x].addMouseListener(new MouseAdapter() {

Your current code is creating a unique listener for every component. This is unnecessary.

Instead create a generic listener that can be shared by all panels. So outside your looping code you would do something like:

MouseListener ml = new MouseAdapter()
{
    @Override mousePressed(MouseEvent e)
    {
        Component panel = e.getComponent();
        panel.setBackground( Color.GRAY );
    }
};

Then in your looping code you simply do:

arrGrid[y][x].addMouseListener( ml );

Upvotes: 1

Related Questions