Ski Ski - 1 year ago 141
Java Question

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

I'm using the

methods in the
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:

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

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 Source

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()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download