Important alert: (current site time 7/15/2013 4:17:15 PM EDT)
 

VB icon

Double Buffer

Email
Submitted on: 6/7/2000 8:20:22 AM
By: Dave Smith 
Level: Beginner
User Rating: Unrated
Compatibility: Java (JDK 1.1)
Views: 19831
(About the author)
 
     Demonstates the use of off screen graphics for smoother animation.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Double Buffer
// Description:Demonstates the use of off screen graphics for smoother animation.
// By: Dave Smith
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1862&lngWId=2//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;
	}
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
5/29/2006 1:18:43 AMdagout

very good
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.