import java.awt.*; import java.applet.Applet; public class BallBox1 extends Applet { private int x = 7, xChange = 7; private int y = 2, yChange = 2; private int diameter = 10; private int LeftX = 0, RightX = 100; private int TopY = 0, BottomY = 100; public void paint(Graphics g) { g.drawRect(LeftX, TopY, RightX - LeftX, BottomY - TopY); for (int n = 1; n < 100; n++) { Color backgroundColor = getBackground(); g.setColor(backgroundColor); g.fillOval(x, y, diameter, diameter); if (x <= LeftX) xChange = -xChange; if (x >= RightX) xChange = -xChange; if (y <= TopY) yChange = -yChange; if (y >= BottomY) yChange = -yChange; x = x + xChange; y = y + yChange; g.setColor(Color.RED); g.fillOval(x, y, diameter, diameter); try { Thread.sleep(50); } catch (InterruptedException e) { System.err.println("sleep exception"); } } } }