Bouncing ball program... having problems..
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class PracticalExam extends JApplet
{
int x = 70;
int y = 100;
int xVelocity = 5;
int yVelocity = 5;
public void start()
{
System.out.println("firing start...");
Timer timer;
final int FREQ = 33;
timer = new Timer(FREQ, new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
repaint();
}
});
timer.start();
}
public void paint(Graphics g)
{
if ( (x > 350) || (x < 50) )
{
xVelocity = -xVelocity;
}
if ( (x < 350) || (x > 50) )
{
g.setColor(Color.white);
g.fillRect(x,y,30,30);
x += xVelocity;
y += yVelocity;
g.setColor(Color.green);
g.fillRect(x,y,30,30);
}
if ( (y > 250) || (y < 50) )
{
yVelocity = -yVelocity;
}
if ( (y < 250) || (y > 50) )
{
g.setColor(Color.white);
g.fillOval(x,y,50,50);
x += xVelocity;
y += yVelocity;
g.setColor(Color.red);
g.fillRect(x,y,50,50);
}
}
}
I was making a progam that a ball bounces the wall.
My purpose was to make the ball change its shape and color when it bounces.
But I am having problems...
Please help me editting...... emergency T^T