Java Beginners

Moderators: zibadian
Number of threads: 1140
Number of posts: 2551

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

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);
	}
}

)





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 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.
Operated by CommunityHeaven, a BootstrapLabs company.