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.
#2
this seems very interesting i'm going to use it but can you post it on pastebin? cause all the formatation of the code is gone if you post it on the forum
#3
andrei.xd23 Wrote:this seems very interesting i'm going to use it but can you post it on pastebin? cause all the formatation of the code is gone if you post it on the forum
I have updated the post to include a pastebin version!
My Blog | My Setup | My Videos | Have a wonderful day.
#4
thanks <!-- sSmile --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt="Smile" title="Smile" /><!-- sSmile -->
#5
it seems that i still can't get it to work... <!-- m --><a class="postlink" href="http://pastebin.com/j2SLGsC3">http://pastebin.com/j2SLGsC3</a><!-- m -->
maybe you could give me some advices
#6
andrei.xd23 Wrote:it seems that i still can't get it to work... <!-- m --><a class="postlink" href="http://pastebin.com/j2SLGsC3">http://pastebin.com/j2SLGsC3</a><!-- m -->
maybe you could give me some advices
Well it seems to me that you're not actually overriding the "Draw" or "DoLogic" methods of the jBFCanvas class. In order to get it to work, you must overwrite thes methods with your own code.
My Blog | My Setup | My Videos | Have a wonderful day.
#7
is this what you mean? http://pastebin.com/Rrrc0Z7N
or i need to make a launcher class or something?
i'm really new to java


Forum Jump:


Users browsing this thread: 1 Guest(s)