Please visit our sponsor
UNKNOWN //************************************** // Name: Double Buffer // Description:Demonstates the use of off screen graphics for smoother animation. // By: Dave Smith // // // Inputs:None // // Returns:None // //Assumes:None // //Side Effects:None //This code is copyrighted and has limited warranties. //Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.1862/lngWId.2/qx/vb/scripts/ShowCode.htm //for details. //************************************** import java.applet.Applet; import java.awt.*; import java.lang.*; public class DoubleBuffer extends Applet implements Runnable{ private Image offScreenImage; int holdX[]; int holdY[]; int holdRad[]; Thread thisThread = new Thread(this); int intThrow = 0; int intSpeed = 1; public void init() { setBackground(Color.black); offScreenImage = createImage(getSize().width, getSize().height); holdX = new int[200]; holdY = new int[200]; holdRad = new int[200]; for (int count=0;count<200;count++) { holdX[count] = (int)(getSize().width*Math.random()); holdY[count] = (int)(getSize().height*Math.random()); holdRad[count] = (int)(4*Math.random()); } repaint(); thisThread.start(); } public void update(Graphics g) { Graphics offScreenGraphics = offScreenImage.getGraphics(); offScreenGraphics.setColor(Color.black); offScreenGraphics.fillRect(0,0,getSize().width, getSize().height); offScreenGraphics.setColor(Color.white); for (int count=0;count<200;count++) { offScreenGraphics.fillRect(holdX[count], holdY[count], holdRad[count], holdRad[count]); holdX[count] += holdRad[count]+intSpeed; if (holdRad[count] <= 1) { offScreenGraphics.setColor(Color.white); } else { offScreenGraphics.setColor(Color.white); } if (holdX[count]>=getSize().width) { holdX[count] = 1; } if (holdX[count] < 0) { holdX[count] = getSize().width; } } paint(offScreenGraphics); g.drawImage(offScreenImage,0,0,this); } public void run() { do { repaint(); try { Thread.currentThread().sleep(15); } catch (Exception sleepProblem) { // Slow the bugger down a bit. System.out.println(sleepProblem); } } while (intThrow==0); } public boolean keyDown(Event event, int keyCode) { if (event.shiftDown()) { intSpeed+=1; } if (event.controlDown()) { intSpeed-=1; } return true; } }