1

I found the below code here: How to use double buffering inside a thread and applet

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?

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();
           }

     }
}

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)

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) {}    
}
Community
  • 1
  • 1
flea whale
  • 1,783
  • 3
  • 25
  • 38

2 Answers2

2

Your drawArea is null. You will have to call super.init() first:

public class ExtendTest extends AppletGameCore {
    public void init() {
      super.init();
      setSize(420, 420);
      run();
    }

    public void draw(Graphics2D g) {}

    public void update(long l) {}    
}

Then its created in AppletGameCore with drawArea = new Canvas();

mtsz
  • 2,725
  • 7
  • 28
  • 41
2

Looks like you need to call the super init() method to initialize the drawArea variable used in the run method like so:

public class ExtendTest extends AppletGameCore {
    public void init() {
      super.init();
      setSize(420, 420);
      run();
    }

    public void draw(Graphics2D g) {}

    public void update(long l) {}    
}
John Ericksen
  • 10,995
  • 4
  • 45
  • 75