• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Moving Square in an Applet

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Java Geezers! Just wondering if anyone is able to help me with this:

"Write an applet that contains an 8 x 8 solid green square. The square is to move at a constant speed. It bounces off the boarders of the aplet display area when it comes into contact with them. Each time the square contacts the border its speed remains constant, but its velocity changes"

Well I've drawn the green square but I haven't a clue how to make it move, heres my code so far: Please help!
 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Michael,
First of all you have to keep track of the position of the green square by keeping its co-ordinates in two variables, eg x and y.
Now in a method suppose moveSquare() you have to keep a for loop running where the values of x and y keep increasing by vx and vy respectively (where vx and vy are variables). And after increasing the values keep calling repaint().


void moveSquare()
{
for loop change values of x and y
inside loop
call a method here that checks whether the square conincides
with any of the borders
if it does not then call repaint
if it does then change vx and vy according to the border it is
co-inciding with If it co=incides with top or bottom
border change value of vy to its reverse power(if its
positive make it negative and vice-versa)Left ot Right
Border change the vx value to its reverse.
}

void paint(Graphics g)
{
Draw the green square but in the parameters DON'T pass constants instead pass the values of x and y and width and height
}

If I was not clear and you got confused inform me and I will try to explain again.
 
Mike Meakin
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I basically need the coding for all of this:

void moveSquare()
{
for loop change values of x and y
inside loop
call a method here that checks whether the square conincides
with any of the borders
if it does not then call repaint
if it does then change vx and vy according to the border it is
co-inciding with If it co=incides with top or bottom
border change value of vy to its reverse power(if its
positive make it negative and vice-versa)Left ot Right
Border change the vx value to its reverse.
}
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup you do. Which part are you having trouble with?

Can you determnine the initializing, terminating, and incrementing expressions for the for() loop? (If the initialization is complicated and the increment is complicated or non-existant, consider a while() instead.)

Can you tell when the square is touching a border?

Can you change the x and/or y component of the velocity?

Can you make it look like a moving square instead of a line drawn with an 8x8 "pen"?

Ryan
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you translate the pseudocode given into actual Java code? As a hint, look closely for words that are keywords in Java.

Layne
 
Mike Meakin
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have a clue. All I need is a code which will make the square move around the applet and bounce off the walls, I don't have a clue where to start. Apart from drawing the square as in the code above.
 
Screaming fools! It's nothing more than a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic