• 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

Random moving Circle

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
How can i make for example a circle that have random movements(in random speed) in a panel?
I know Java fundamentally but I am not so familiar with GUI.e.g I don't know what a Graphic object is? or when we override paintComponent method what happens?I don't know difference of Swing and AWT and how to work with them in detail.
Thanks.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest you start with much simpler examples, creating a frame which you can see at all, for example. You cannot add a circle to a frame until you have a frame to add it to!
Try the Java Tutorials and use ctrl-F-“Graphical User Interfaces”.

And welcome to the Ranch
 
Bartender
Posts: 563
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How can i make for example a circle that have random movements(in random speed) in a panel?


And all of that randomness might not be very pleasing to the eye. I recommend first learning to move an object predictably within the boundaries of the container and then adding randomness as it makes sense or is required by the application.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would implement this by using random velocity instead of random movement.

Anyways, I agree with Campbell, you need to learn how to draw a circle before you randomly move it
 
Moein Sol
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I would suggest you start with much simpler examples, creating a frame which you can see at all, for example. You cannot add a circle to a frame until you have a frame to add it to!
Try the Java Tutorials and use ctrl-F-“Graphical User Interfaces”.


Thanks Campbell,
I know how to create a JFrame and how to add components or implemention of handlers ;) .but clearly my question is that, suppose I extend JPanel class and override the paindComponent method (e.g. add methods like g.fillrect(x,y,z,w) ... to add rectangular to it) . but now, I want to change the position of this rectangular.How can I do that?
There are many tutorials about GUI , but most of them are very long and take much time(like suggested link,of course I review that ) .

 
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> ...g.fillrect(x,y,z,w)....I want to change the position of this rectangular.How can I do that?

you make the variables x and y accessible by a swing timer, and in the timer code
you change the values of x,y then call [panel].repaint()

make sure your first line in paintComponent is
super.paintComponent(g);
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Moein Sol wrote:There are many tutorials about GUI , but most of them are very long and take much time


Rome wasn't built in a day. You have to learn to walk before you can run. Etc. etc.

An assignment like this is evidently meant as a learning exercise. Any short cuts here and you won't be prepared for the next exercise. So bite the bullet and go through those tutorials, one example at a time. You won't regret it.

db
reply
    Bookmark Topic Watch Topic
  • New Topic