Ski Ski - 8 days ago 6
Java Question

Making the getX() and getY() methods to get the locations of the panel, not the frame

I'm using the

getX()
and
getY()
methods in the
MouseListener
class to get the location of the panel, but it's getting the location of the frame instead, and that includes the top of the title bar and the sides, so it's not getting the location that I want. I also have the:

@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}


method in my paint class to override the frame so when you paint in graphics, and set the paint location of x, and y, it sets it to the panel location, and not the frame location, but I don't know how to make it so that the MouseListener class does the same. Please help. Thanks.

Code for the
MouseListener
class:

package events;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Events implements ActionListener, MouseListener, MouseMotionListener {

static Events events = new Events();

int x;
int y;

public void actionPerformed(ActionEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
x = e.getX();
y = e.getY();

System.out.println("X:" + x + " " + "Y:" + y);
}

public void mouseMoved(MouseEvent e) {
}

public void mouseDragged(MouseEvent e) {
}
}

Ski Ski
Answer

Nevermind. I can just call the panel name for the panel x, and y locations. But thanks anyways.

x = P.g.getX();
y = P.g.getY()
Comments