1

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!

mKorbel
110k20 gold badges138 silver badges331 bronze badges
asked Aug 19, 2013 at 19:45
3
  • try taking out final MouseEvent e. It should just be MouseEvent e Commented 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 changes Commented Aug 19, 2013 at 20:03
  • 1
    I 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. Commented Aug 19, 2013 at 20:08

2 Answers 2

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);
 }
}
answered Aug 19, 2013 at 19:51
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Aug 19, 2013 at 21:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.