/* This example demonstrates a bouncing ball with flicker */ import java.awt.*; import java.applet.*; public class Bounce1 extends Applet implements Runnable { private static final int XSIZE = 30; private static final int YSIZE = 30; private int x = 0; private int y = 0; private int dx = 2; private int dy = 2; Thread myThread; public void init() { setBackground(Color.black); } public void start() { if (myThread == null) { myThread = new Thread(this); myThread.start(); } } public void stop() { if (myThread != null) { myThread.stop(); myThread = null; } } public void run() { while (true) { repaint(); x += dx; y += dy; Dimension d = size(); if (x < 0) { x = 0; dx = -dx; } if (x + XSIZE >= d.width) { x = d.width - XSIZE; dx = -dx; } if (y < 0) { y = 0; dy = -dy; } if (y + YSIZE >= d.height) { y = d.height - YSIZE; dy = -dy; } try { Thread.sleep(15); } catch(InterruptedException e) {} } } public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(x, y, XSIZE, YSIZE); } }