Do you receive the Programmer's Heaven newsletter? If not, why not subscribe?

Java Beginners

Moderators: zibadian
Number of threads: 889
Number of posts: 1946

This Forum Only
Post New Thread

Report
Double Buffering [UNSOLVED] Posted by Daphron on 3 Nov 2009 at 6:15 PM
I'm new to java and am working my way through a tutorial but I want to know how to add double buffering to the code below. The tutorial's code doesn't work, so how do you guys do it? (commented code is helpful
// DOUBLE BUFFERING

import java.applet.*;
import java.awt.*;




public class Game2p2 extends Applet implements Runnable
{
	int x_pos = 10;
	int y_pos = 100;
	int radius = 20;
	
	public void init() {}
	

	public void start(){
		//define a new thread
		Thread th = new Thread(this);
		// start this thread
		th.start();
	}
	
	
	public void stop() {}
	
	
	public void destroy(){}
	
	public void run() {
		// lower ThreadPriority
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		
		// run a long while (true) this means in our case 'allways'
		while(true){
			// repaint the applet
			x_pos++;
			repaint();
			
			try{
				// Stop thread for 20 milliseconds
				Thread.sleep(20);
			}
			catch(InterruptedException ex){
				// do nothing
			}
			
			// set ThreadPriority to maximum value
			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		}
		
	}
	
	public void paint(Graphics g){
		// set color
		g.setColor(Color.red);
		
		// paint a filled colored circle
		g.fillOval(x_pos - radius, y_pos - radius, 2*radius, 2*radius);
	}
}

)





 
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A bootstrapLabs project.