/* This example demonstrates a bouncing ball without flicker */ import java.awt.*; import java.applet.*; public class Bounce2 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; Image offscreenImg; Graphics offscreenG; public void init() { offscreenImg = createImage(size().width, size().height); offscreenG = offscreenImg.getGraphics(); } 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) { offscreenG.setColor(Color.black); offscreenG.fillRect(0,0,size().width, size().height); offscreenG.setColor(Color.red); offscreenG.fillOval(x, y, XSIZE, YSIZE); g.drawImage(offscreenImg, 0, 0, this); } public void update(Graphics g) { paint(g); } }