This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help in moving ball to mouse click coordinates  RSS feed

 
yuval yuvitech
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys !

I have this code of a moving ball from one corner to another inside the JFrame,

i added a mouse click event so the ball will move to the coordinates i have just clicked inside the frame.

but now, after i added this click event , the ball isn't moving .


pleae help, whats wrong with the code ?

thanks !


 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't use the paint method. Use a different method, like this:-You also need to add a Listener. I usually don't like adding Listeners to panels, but in the case of a mouse listener, the panel is a logical place to get the information from, so that is an exception to my policy. If you add a mouse listener it will have the mouse clicked method. You might do well to create a subclass of a mouse adapter in which case you only need to override one method.
Never use Thread#sleep in a GUI component because they are not thread‑safe and can only use one thread and you will simply stop the rest of your GUI from working. Find out about Swing Timers. You can read more in the Java™ Tutorials.

I shall move this discussion to the GUIs forum.
 
yuval yuvitech
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your answer,

next time i'll pay attention where to post my question.

and about my question, if i understand your reply, do i need to add subclass of a mouse adpter ?

can you please show me how ?


thanks !
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you look at the "Java Tutorials"?

There is a section on "How to Write a MouseListener".

Use this example as a starting point for your program. It will show you how to better structure your code so the GUI components are created on the Event Dispatch Thread and so you don't use static variables.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating a subclass of a mouse adapter is probably one of the easier ways to do it.

If you find a copy of Horstmann and Cornell's book (Core Java II), you will see that they advise against the XYZAdapter classes, because it is too easy to make a tiny spelling mistake when you override the method and end up with a method which does nothing at all. But you can overcome that problem by using the @Override annotation which you appear to know about already.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!