*
The moose likes Swing / AWT / SWT and the fly likes Random moving Circle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Random moving Circle" Watch "Random moving Circle" New topic
Author

Random moving Circle

Moein Sol
Greenhorn

Joined: Feb 13, 2012
Posts: 7
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

Joined: Oct 13, 2005
Posts: 38100
    
  22
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

Joined: Oct 24, 2010
Posts: 557
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.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2278
    
  28

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

Joined: Feb 13, 2012
Posts: 7
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

Joined: Jun 09, 2003
Posts: 4632
> ...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

Joined: May 03, 2008
Posts: 4523
    
    5

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


luck, db
There are no new questions, but there may be new answers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Random moving Circle
 
Similar Threads
Creating your own class
JSS Prog Project 6.18
Trying to finish program
Creating Java applet
Random numbers