jBFCanvas - Printable Version +- BP Forums (https://bpforums.info) +-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55) +--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56) +---- Forum: Java (https://bpforums.info/forumdisplay.php?fid=19) +----- Forum: Code Snippets (https://bpforums.info/forumdisplay.php?fid=32) +----- Thread: jBFCanvas (/showthread.php?tid=785) |
jBFCanvas - brandonio21 - 05-13-2013 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 --> Re: jBFCanvas - andrei.xd23 - 05-14-2013 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 Re: jBFCanvas - brandonio21 - 05-14-2013 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 forumI have updated the post to include a pastebin version! Re: jBFCanvas - andrei.xd23 - 05-16-2013 thanks <!-- s --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt="" title="Smile" /><!-- s --> Re: jBFCanvas - andrei.xd23 - 05-17-2013 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 Re: jBFCanvas - brandonio21 - 05-17-2013 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 -->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. Re: jBFCanvas - andrei.xd23 - 05-18-2013 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 |