awt - Tracking mouse movement in java -
i've been working on basic tower defense game , looking track movements of mouse. don't need track mouse movement when dragging when hovering on screen user can see tower going placed. i've extended mouseadapter make controller , clicking place tower works can't figure how track mouse movement otherwise. should overriding mousemoved this? tried little success. after putting print statement see event not firing. 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 appreciated. thanks!
here's official tutorial you're trying do: http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html
here's relevant snippet inlined:
public class mousemotioneventdemo extends jpanel implements mousemotionlistener { //...in initialization code: //register mouse events on blankarea , 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
Post a Comment