05-13-2013, 09:20 PM
I have created a class which automatically implements and runs double-buffering. What does this mean? If you're making a game and want to utilize the mechanics of double-buffering, but don't know how, simply make your game class extend jBFCanvas and you will have a working Canvas object that automatically works as a double-buffering machine. Simply override the DoLogic and Draw methods!
[code2=java]import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
/*
* This class is created to make making games easier, it allows
* classes to automatically inherit double-buffer capabilities and
* canvas properties
* To use, force your class to extend this class and override the draw and dologic methods
* created by brandonio21, Brandon Milton, <!-- m --><a class="postlink" href="http://brandonsoft.com">http://brandonsoft.com</a><!-- m -->
*/
public class jBFCanvas
extends Canvas
implements Runnable
{
private static final long serialVersionUID = 1L;
private int waitTime;
private BufferStrategy bfStrategy;
private Graphics bfGraphics;
private Thread loopThread;
public jBFCanvas(int waitTime, Dimension preferredSize)
{
this.waitTime = waitTime;
this.setPreferredSize(preferredSize);
//constructor, we need to intialize the thread
if (loopThread == null)
loopThread = new Thread(this);
}
public void paint(Graphics g)
{
//initialize backbuffer
if (bfStrategy == null)
{
this.createBufferStrategy(2);
bfStrategy = this.getBufferStrategy();
}
try
{
loopThread.start();
}
catch (Exception e) {} //this means the thread was already started. Don't do anything
}
public void DoLogic()
{
//this is where apps calculations will go
}
public void Draw(Graphics g)
{
//this is where all apps drawings will go
}
public void Render()
{
bfStrategy = this.getBufferStrategy();
bfGraphics = null;
try
{
bfGraphics = bfStrategy.getDrawGraphics();
bfGraphics.clearRect(0, 0, this.getPreferredSize().width, this.getPreferredSize().height);
Draw(bfGraphics);
}
catch (Exception e) {}
finally
{
if (bfGraphics != null)
bfGraphics.dispose();
}
bfStrategy.show();
Toolkit.getDefaultToolkit().sync();
}
@Override
public void run() {
//loop. Ignore this
while (true)
{
DoLogic();
Render();
//now we wait
Thread.currentThread();
try
{
Thread.sleep(waitTime);
}
catch (Exception e) {}
}
}
public int getWaitTime() {
return waitTime;
}
public void setWaitTime(int waitTime) {
this.waitTime = waitTime;
}
public Graphics getGraphics() {
return bfGraphics;
}
public void setGraphics(Graphics bfGraphics) {
this.bfGraphics = bfGraphics;
}
}[/code2]
Pastebin Link:
<!-- m --><a class="postlink" href="http://pastebin.com/TjKbECee">http://pastebin.com/TjKbECee</a><!-- m -->
[code2=java]import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
/*
* This class is created to make making games easier, it allows
* classes to automatically inherit double-buffer capabilities and
* canvas properties
* To use, force your class to extend this class and override the draw and dologic methods
* created by brandonio21, Brandon Milton, <!-- m --><a class="postlink" href="http://brandonsoft.com">http://brandonsoft.com</a><!-- m -->
*/
public class jBFCanvas
extends Canvas
implements Runnable
{
private static final long serialVersionUID = 1L;
private int waitTime;
private BufferStrategy bfStrategy;
private Graphics bfGraphics;
private Thread loopThread;
public jBFCanvas(int waitTime, Dimension preferredSize)
{
this.waitTime = waitTime;
this.setPreferredSize(preferredSize);
//constructor, we need to intialize the thread
if (loopThread == null)
loopThread = new Thread(this);
}
public void paint(Graphics g)
{
//initialize backbuffer
if (bfStrategy == null)
{
this.createBufferStrategy(2);
bfStrategy = this.getBufferStrategy();
}
try
{
loopThread.start();
}
catch (Exception e) {} //this means the thread was already started. Don't do anything
}
public void DoLogic()
{
//this is where apps calculations will go
}
public void Draw(Graphics g)
{
//this is where all apps drawings will go
}
public void Render()
{
bfStrategy = this.getBufferStrategy();
bfGraphics = null;
try
{
bfGraphics = bfStrategy.getDrawGraphics();
bfGraphics.clearRect(0, 0, this.getPreferredSize().width, this.getPreferredSize().height);
Draw(bfGraphics);
}
catch (Exception e) {}
finally
{
if (bfGraphics != null)
bfGraphics.dispose();
}
bfStrategy.show();
Toolkit.getDefaultToolkit().sync();
}
@Override
public void run() {
//loop. Ignore this
while (true)
{
DoLogic();
Render();
//now we wait
Thread.currentThread();
try
{
Thread.sleep(waitTime);
}
catch (Exception e) {}
}
}
public int getWaitTime() {
return waitTime;
}
public void setWaitTime(int waitTime) {
this.waitTime = waitTime;
}
public Graphics getGraphics() {
return bfGraphics;
}
public void setGraphics(Graphics bfGraphics) {
this.bfGraphics = bfGraphics;
}
}[/code2]
Pastebin Link:
<!-- m --><a class="postlink" href="http://pastebin.com/TjKbECee">http://pastebin.com/TjKbECee</a><!-- m -->