mouse motion listener only in one direction

后端 未结 4 936
粉色の甜心
粉色の甜心 2020-12-07 00:28

i have been working on mouse motion listener in Java couldn\'t sort it out completely because i want the object to move towards the direction where ever on the screen the mo

4条回答
  •  难免孤独
    2020-12-07 00:58

    Your problem is with this line:

    public void mouseMoved(MouseEvent e){
     xpos=getX(); // ******
     if(xpos<0){polyrot--;}
     else if(xpos>0){polyrot++;}
     repaint();
     break;
    }
    

    That returns the x position of the applet not the mouse cursor. You need to use your MouseEvent object, e and instead get the mouse's position. Change it to:

    xpos = e.getX();
    

    Please don't ignore the comment that I made to your question. Please remember that we're volunteers who help on our free time. Please don't make it any more difficult than it has to be to help you.


    I've tried to edit your code so that it compiles, and now is indented. Consider creating a Swing application, not an AWT application, since Swing apps are more flexible, powerful and robust.

提交回复
热议问题