• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help in moving ball to mouse click coordinates

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !


 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic