This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes Need help with Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Need help with "Square must move at constant speed and bounce of the borders"... " Watch "Need help with "Square must move at constant speed and bounce of the borders"... " New topic
Author

Need help with "Square must move at constant speed and bounce of the borders"...

Alex Reynders
Greenhorn

Joined: Nov 16, 2008
Posts: 13
Hi, I please need help with an assignment that I need to do,

Here is the assignment:

"Write an application that contains a 10 x 10 solid blue square.
The square must move at constant speed and must bounce off the borders of the display area. Each time the square collides with a border, its direction must change.
Create two classes. The first class must extend JFrame and create an instance of the second class. The second class will extend JPanel"

The code that I have tried so far look like this:



The problem is, is that I do not know what to do from here, when I run this code the square is shown, but how do I code the rest of the program to start the square moving as well as bounce off the borders of the display?

Please tell me if I have to change my code anywhere and what code to add.

I really need your help, because I do not have a clue what to further. :?: :?:
Thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38436
    
  23
Sounds like a Swing-related problem, so I shall move it.

If you only have one bouncy object, you can use a javax.swing.Timer to set it off. Make sure the "ticks" are at least 10 milliseconds apart, to allow enough time for repainting.

You only need to thread if you have more than one object bouncing at once.
Alex Reynders
Greenhorn

Joined: Nov 16, 2008
Posts: 13
Can you please give me an example of how to do this.
I'm not sure where to code this, or what the code should look like...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38436
    
  23
Have you been through the API? We are not able to give code for assignments, but are willing to help if you have a specific problem. And you will remember it much better if you ferret out the information for yourself.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38436
    
  23
By the way, never write if (isVisible == true). You can get nasty errors if you write = instead of ==. You just write if (isVisible) or if (!isVisible).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with "Square must move at constant speed and bounce of the borders"...