This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random moving Circle

 
Moein Sol
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50235
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Greg Brannon
Bartender
Posts: 563
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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 ) .

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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);
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic