• 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

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

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
reply
    Bookmark Topic Watch Topic
  • New Topic