// JDK 1.1.1 import java.awt.*; public class BouncingBallCanvas extends Canvas { int x, y, dx = -4, dy = -8, radius = 20; Dimension d; Image im; Graphics offscreen; Color ballcolor = Color.red; public void init() { d = getSize(); x = d.width * 2 / 3 ; y = d.height - radius; } public void update(Graphics g) { if (im == null) { im = createImage(d.width, d.height); offscreen = im.getGraphics(); } offscreen.setColor(Color.white); offscreen.fillRect(0,0,d.width,d.height); if (x < radius || x > d.width - radius) { dx = -dx; } if (y < radius || y > d.height - radius) { dy = -dy; } x += dx; y += dy; offscreen.setColor(ballcolor); offscreen.fillOval(x - radius, y - radius, radius * 2, radius * 2); g.drawImage(im, 0, 0, this); } public void paint(Graphics g) { update(g); } public void setColor(Color c) { ballcolor = c; } }