0

So I'm making a basic game and I'm stuck with the mouse. I have it, right now, to where you click and it will print out the coordinates or the position that was clicked. The only problem is that the coords that it's printing are the coords of my whole computer screen and not the coords within my JFrame. I've looked it up and I saw some stuff about adding it to the frame but, I've tried...

frame.addMouseListener(new MouseInput(client));

Here's the basics of what I have.

public void init(){
addMouseListener(new MouseInput(this));
}

public static void main(String[] args){
    Client client = new Client();

    client.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    client.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    client.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    //client.addMouseListener(new MouseInput(client));

    JFrame frame = new JFrame(client.title);
    frame.add(client);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.addMouseListener(new MouseInput(client));

    client.start();
}    

public void mouseClicked(MouseEvent e) {
    PointerInfo a = MouseInfo.getPointerInfo();
    Point b = a.getLocation();
    double x = (double) b.getX();
    double y = (double) b.getY();
    m.setX((int) x);
    m.setY((int) y);
    System.out.println("Mouse Clicked at ( " + x + ", " + y + ")");
}

and this class...:

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class MouseInput extends JFrame implements MouseListener
 {
private int x = 0;
private int y = 0;

Client client;

public MouseInput(Client client){
    this.client = client;
}

public void mouseClicked(MouseEvent e) {
    client.mouseClicked(e);
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
    client.mouseReleased(e);
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public int getX(){
    return x;
}

public int getY(){
    return y;
}

public void setX(int x){
    this.x = x;
}

public void setY(int y){
    this.y = y;
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • http://stackoverflow.com/questions/15685502/jframe-mouse-click-using-jcomponent-and-mouselistener – wwright Aug 04 '14 at 23:57
  • 2
    The listener should be connected to the component of interest, rather than the frame. For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal, Complete, Verifiable Example). – Andrew Thompson Aug 04 '14 at 23:57

2 Answers2

2

Don't use PointerInfo for this, all the information you need is passed to you via the MouseEvent...

MouseEvent contains the local x/y coordinates of the mouse event relative on the component which raised in...

public void mouseClicked(MouseEvent e) {
    int localX = e.getX();
    int localY = e.getY();

To translate the localX/Y to another component's context, you would use something like...

e = SwingUtilities.convertMouseEvent(e.getComponent(), e, frame);
// Where frame would be the component you want to translate the current
// local context into

Take a look at SwingUtilities.convertMouseEvent(Component, MouseEvent, Component), SwingUtilities.convertPoint(Component, Point, Component) and SwingUtilities.convertPoint(Component, int, int, Component) for more details.

Also, take a look at How to Write a Mouse Listener

Generally, you are better off attaching listeners to the component you are interested, rather than the frame, a frame is made up of a number layered components, all of which could consume mouse events, preventing your frame from receiving mouse events

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Instead of using the PointerInfo, you can just use the MouseEvent that's being passed into your callback method. Specifically you can use e.getX() and e.getY() to get the x and y positions relative to the upper left of the frame. See http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseEvent.html