如何扩展这个类并在我的Java applet中使用它?我得到NullPointerException

时间:2012-02-05 22:11:51

标签: java applet

我在这里找到了以下代码: How to use double buffering inside a thread and applet

它说“扩展AppletGameCore并定义自己的子类来实现所需的方法。”有人可以告诉我你如何做到这一点,以便我可以在我的小程序中使用双缓冲吗?

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

     }
}

以下是我的尝试。我收到错误:java.lang.NullPointerException     在AppletGameCore.run(AppletGameCore.java:76)     在ExtendTest.init(ExtendTest.java:10)     在sun.applet.AppletPanel.run(未知来源)     在java.lang.Thread.run(未知来源)

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) {}    
}

2 个答案:

答案 0 :(得分:2)

您的drawArea为空。您必须先致电super.init()

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

    public void draw(Graphics2D g) {}

    public void update(long l) {}    
}

然后使用drawArea = new Canvas();

在AppletGameCore中创建它

答案 1 :(得分:2)

看起来你需要调用super init()方法来初始化run方法中使用的drawArea变量,如下所示:

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

    public void draw(Graphics2D g) {}

    public void update(long l) {}    
}