This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Moving Square in an Applet  RSS feed

 
Mike Meakin
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • 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!
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • 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
  • 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.
}
 
Ryan McGuire
Ranch Hand
Posts: 1142
9
  • Mark post as helpful
  • send pies
  • 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
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!