Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to extend this class and use it in my Java applet? I get NullPointerException
    primarykey
    data
    text
    <p>I found the below code here: <a href="https://stackoverflow.com/questions/2873506/how-to-use-double-buffering-inside-a-thread-and-applet">How to use double buffering inside a thread and applet</a></p> <p>It says "extend AppletGameCore and define your own subclass that implements the required methods." Can someone please show me how you might do this so that I can use double buffering in my applet?</p> <pre><code>import java.awt.Canvas; import java.awt.Graphics2D; import java.awt.Dimension; import java.awt.image.BufferStrategy; import java.applet.Applet; /** *AppletGameCore.java *@author David Graham */ public abstract class AppletGameCore extends Applet implements Runnable { private BufferStrategy bufferStrategy; private Canvas drawArea;/*Drawing Canvas*/ private boolean stopped = false;/*True if the applet has been destroyed*/ private int x = 0; public void init() { Thread t = new Thread(this); drawArea = new Canvas(); setIgnoreRepaint(true); t.start(); } public void destroy() { stopped = true; /*Allow Applet to destroy any resources used by this applet*/ super.destroy(); } public void update() { if(!bufferStrategy.contentsLost()) { //Show bufferStrategy bufferStrategy.show(); } } //Return drawArea's BufferStrategy public BufferStrategy getBufferStrategy() { return bufferStrategy; } //Create drawArea's BufferStrategies public void createBufferStrategy(int numBuffers) { drawArea.createBufferStrategy(numBuffers); } //Subclasses should override this method to do any drawing public abstract void draw(Graphics2D g); public void update(Graphics2D g) { g.setColor(g.getBackground()); g.fillRect(0,0,getWidth(),getHeight()); } //Update any sprites, images, or primitives public abstract void update(long time); public Graphics2D getGraphics() { return (Graphics2D)bufferStrategy.getDrawGraphics(); } //Do not override this method public void run() { drawArea.setSize(new Dimension(getWidth(),getHeight())); add(drawArea); createBufferStrategy(2); bufferStrategy = drawArea.getBufferStrategy(); long startTime = System.currentTimeMillis(); long currTime = startTime; //animation loop while(!stopped) { //Get time past long elapsedTime = System.currentTimeMillis()-currTime; currTime += elapsedTime; //Flip or show the back buffer update(); //Update any sprites or other graphical objects update(elapsedTime); //Handle Drawing Graphics2D g = getGraphics(); update(g); draw(g); //Dispose of graphics context g.dispose(); } } } </code></pre> <p>Below is my attempt. I get the error: java.lang.NullPointerException at AppletGameCore.run(AppletGameCore.java:76) at ExtendTest.init(ExtendTest.java:10) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source)</p> <pre><code>import java.awt.Canvas; import java.awt.Graphics2D; import java.awt.Dimension; import java.awt.image.BufferStrategy; import java.applet.Applet; public class ExtendTest extends AppletGameCore { public void init() { setSize(420, 420); run(); } public void draw(Graphics2D g) {} public void update(long l) {} } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload