Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Alex Reynders
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48909
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48909
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48909
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic