This week's book giveaway is in the Agile and other Processes forum.We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login Win a copy of The Mikado Method this week in the Agile and other Processes forum!

# Moving Square in an Applet

Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
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

Joined: May 02, 2005
Posts: 209
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.

With Best Regards,
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
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.
}
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 945
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
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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

Joined: Dec 30, 2004
Posts: 88
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.

I agree. Here's the link: http://aspose.com/file-tools

subject: Moving Square in an Applet