Professional Documents
Culture Documents
Bouncing Ball PDF
Bouncing Ball PDF
Bouncing Ball PDF
// Ball Size
float radius = 20;
float diameter = radius * 2;
// Center of Call
float X = radius + 50;
float Y = radius + 20;
// Direction
float dx = 3;
float dy = 3;
public BouncingBall() {
wi = getWidth();
he= getHeight();
X = X + dx ;
Y = Y + dy;
if (X - radius < 0) {
dx = -dx;
X = radius;
} else if (X + radius > wi) {
dx = -dx;
X = wi - radius;
}
if (Y - radius < 0) {
dy = -dy;
Y = radius;
} else if (Y + radius > he) {
dy = -dy;
Y = he - radius;
}
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
}
}
}
};
thread.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.fillOval((int)(X-radius), (int)(Y-radius), (int)diameter,
(int)diameter);
}