/* * ============================================================= * BouncePanel.java : Implements double buffering to avoid problems * with overlapping circles. Overrides update to avoid flickering * problems. * * Written by : Vasilios Lagakos March, 2001 * ============================================================== */ import javax.swing.*; import java.awt.*; import java.util.Vector; public class BouncePanel extends JComponent implements Runnable { private Vector circles; private int width, height; private Thread animationThread = null; boolean Reset = true; Image offScreenImage; Graphics2D offScreenGraphics2D; public BouncePanel() { setDoubleBuffered(false); circles = new Vector(); } //---------------------------------------------------- /*** Skip the usual screen-clearing step of update so that there is * no "flicker" between each drawing step. */ public void update(Graphics g){ paint(g); } //---------------------------------------------------- /*** Draw all circles onto an offscreen image and * then copy them to the display area. */ public void paint(Graphics g) { width = getSize().width; height = getSize().height; // Create off screen image when JApplet starts if(offScreenImage == null) { offScreenImage = createImage(width, height); offScreenGraphics2D = (Graphics2D)offScreenImage.getGraphics(); offScreenGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } /* If you do not check if the off screen graphics is not null before using * it to draw circles you will get a NullPointerException, a Runtime * exception that occurs when a method tries to access a member of an object * through a null reference. */ if(offScreenGraphics2D != null) { offScreenGraphics2D.clearRect(0, 0, width, height); MovingCircle circle; for(int i=0; i