Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
jBFCanvas
#1
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 -->
My Blog | My Setup | My Videos | Have a wonderful day.


Messages In This Thread
jBFCanvas - by brandonio21_phpbb3_import2 - 05-13-2013, 09:20 PM
Re: jBFCanvas - by andrei.xd23 - 05-14-2013, 09:46 AM
Re: jBFCanvas - by brandonio21_phpbb3_import2 - 05-14-2013, 03:02 PM
Re: jBFCanvas - by andrei.xd23 - 05-16-2013, 01:23 AM
Re: jBFCanvas - by andrei.xd23 - 05-17-2013, 09:47 AM
Re: jBFCanvas - by brandonio21_phpbb3_import2 - 05-17-2013, 03:03 PM
Re: jBFCanvas - by andrei.xd23 - 05-18-2013, 08:30 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)