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;
}
}