hi, I really hope someone can help me with this, i'm trying to create a program which draws a triangle and then the triangle bounces around within a box, for example the shape keeps moving up till it hits the top of the frame then moves in another direction.
I have a program which bounces a ball but I can't change it to bounce any other shape, please can someone help me with this!
Here is the ball program:
import java.awt.*;
import java.applet.Applet;
public class OriginalBall extends
Applet {
/*
Written November 2001
Illustrates simple animation of a grapics object and uses
Thread.sleep () for producing a simple time delay
*/
private int x = 52, xChange = 5; // initial positions plus movement increment
private int y = 12, yChange = 5;
private int diameter = 10; // diameter of ball
private int rectLeftX = 50, rectRightX = 250; // board border coordinates
private int rectTopY = 10, rectBottomY = 180;
public void paint (Graphics g)
{
// draw a board for the game
g.drawRect (rectLeftX, rectTopY, rectRightX - rectLeftX, rectBottomY - rectTopY);
g.setColor (Color.lightGray);
g.fillRect ((rectLeftX+1), (rectTopY+1), (rectRightX-rectLeftX-1), (rectBottomY-rectTopY-1));
for (int n = 1; n < 120; n++) // set up game period loop
{
g.setColor (Color.lightGray); // make ball invisible at original position
g.fillOval(x, y, diameter, diameter);
// check for boundaries and reverse direction if reached
if (x + xChange <= rectLeftX) // left border reached
xChange = -xChange;
else if (x + xChange + diameter >= rectRightX) // right border reached
xChange = -xChange;
else if (y + yChange <= rectTopY) // top border reached
yChange = -yChange;
else if (y + yChange + diameter >= rectBottomY) // bottom border reached
yChange = -yChange;
x = x + xChange; // move ball
y = y + yChange;
g.setColor(Color.red); // make ball visible at new position
g.fillOval (x, y, diameter, diameter);
try
{
Thread.sleep (50); // delay for n ticks
}
catch (InterruptedException e) // must have this exception handler
{
// do nothing
}
}
}
}