javajframemousemotionlistener

mouseMoved event seems to only be getting called once


Trying to make a simple RPG game with a overhead camera. When I went to go implement a MouseMotionListener, which isn't the first time I used a MouseMotionListener, it appears that the mousedMoved MouseEvent only get's called once then simple doesn't seem to work anymore. I've been stuck on this for a fair bit of time and all my research leads me to dead ends.

I do have a full game loop with a tick and render methods. But as you see in the code below, in the mouse handling class (The bottom bit of code) It returns a number which gets increased by one every time the mouse moves (In the tick method I print to the console the value of the number) and it also should print to the console the mouse x and y coordinates. When you run the program the number is equal to one and it only prints the mouse coordinates once. An example would be the console output will look something like this:

124 82
1

Or if my mouse isn't hovering the JFrame when to program launches it'll look like this:

0

Here's my init method for my game loop:

private void init() {
    display = new Display("Operation Blood Bath: Alpha 0_5.0", 800, 600);
    game = new Game();
    Assets.init();
}

Here's my display class which gets called in the init method:

public class Display {

private JFrame frame;
private Canvas canvas;

public Display(String title, int width, int height) {
    frame = new JFrame(title);
    frame.setSize(width, height);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    canvas = new Canvas();
    canvas.setPreferredSize(new Dimension(width, height));
    canvas.setMaximumSize(new Dimension(width, height));
    canvas.setMinimumSize(new Dimension(width, height));
    canvas.setFocusable(false);

    frame.add(canvas);
    frame.addMouseMotionListener(new MouseMotionHandling());
    frame.pack();
}

public Canvas getCanvas() {
    return canvas;
}

public JFrame getFrame() {
    return frame;
}

}

The tick method:

private void tick() {
    game.tick();
    System.out.println(MouseMotionHandling.HELLO());
}

Here's the mouse handling class which gets called in the display class:

public class MouseMotionHandling implements MouseMotionListener {

private static int number = 0;

@Override
public void mouseDragged(MouseEvent arg0) {

}

@Override
public void mouseMoved(MouseEvent arg0) {
    System.out.println(arg0.getX() + " " + arg0.getY());
    number++;
}

public static int HELLO () {
    return number;
}

}

Where am I going work here?


Solution

  • The problem is that your canvas completely fills the content pane of the JFrame and so captures all mouse events. The easiest fix is to add your mouse listener to the canvas rather than the frame.

    As for why you're receiving a single mouse event on your frame - I think this must be due to timing issues in the creation of the various components.