I've been working on a basic tower defense game and was looking to track the movements of the mouse. I don't need it to track mouse movement when dragging but just when hovering over the screen so that the user can see where the tower is going to be placed. I've extended MouseAdapter to make a controller and clicking to place a tower works but I can't figure how to track mouse movement otherwise. Should overriding mouseMoved do this? I tried with little success. After putting a print statement I could see the event was not firing. This is what I have currently:
public class MouseController extends MouseAdapter {
private final Board my_board;
private final int square_size;
public MouseController(final Board the_board, final int the_square_size) {
my_board = the_board;
square_size = square_size;
}
@Override
public void mouseClicked(final MouseEvent e) {
super.mouseClicked(e);
Point p = e.getPoint();
my_board.placeTower(p.x / square_size, p.y / square_size - 1);
}
@Override
public void mouseMoved(final MouseEvent e) {
super.mouseMoved(e);
Point p = e.getPoint();
my_board.placeCursor(p.x / square_size, p.y / square_size - 1);
}
}
Any help is appreciated. Thanks!
-
try taking out final MouseEvent e. It should just be MouseEvent euser2277872– user22778722013年08月19日 19:47:23 +00:00Commented Aug 19, 2013 at 19:47
-
2@user2277872 shouldn't make a difference, in fact, it can be argued that it is more correct (to have final) as the reference of the object never changesMadProgrammer– MadProgrammer2013年08月19日 20:03:16 +00:00Commented Aug 19, 2013 at 20:03
-
1I was actually told by one of my professors that final should almost always be used on method parameters. Regardless, it was that I didn't add the controller as both a mouseListener as well as MouseMotionListener. I've made that mistake before. Thanks anyways.eatinasandwich– eatinasandwich2013年08月19日 20:08:44 +00:00Commented Aug 19, 2013 at 20:08
2 Answers 2
Here's an official tutorial to do exactly what you're trying to do: http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html
Here's the relevant snippet inlined:
public class MouseMotionEventDemo extends JPanel
implements MouseMotionListener {
//...in initialization code:
//Register for mouse events on blankArea and panel.
blankArea.addMouseMotionListener(this);
addMouseMotionListener(this);
...
}
public void mouseMoved(MouseEvent e) {
saySomething("Mouse moved", e);
}
public void mouseDragged(MouseEvent e) {
saySomething("Mouse dragged", e);
}
void saySomething(String eventDescription, MouseEvent e) {
textArea.append(eventDescription
+ " (" + e.getX() + "," + e.getY() + ")"
+ " detected on "
+ e.getComponent().getClass().getName()
+ newline);
}
}
Comments
How do you add your MouseController to your code? I think MouseAdaptor implements MouseListener and MouseMotionListener. Make sure to also call component.addMouseMotionListener(myMouseController) and not only component.addMouseListener(myMouseController).
If only mouseClicked is called but not mouseMoved, it sounds like you only added your mouse controller as mouse listener, but not yet as mouse motion listener.
Comments
Explore related questions
See similar questions with these tags.