开发者

Help with stack-overflow error?

开发者 https://www.devze.com 2023-03-03 18:55 出处:网络
Here\'s the error I\'m getting: java.lang.StackOverflowError at apple.awt.CGraphicsDevice.getScreenInsets(Native Method)

Here's the error I'm getting:

java.lang.StackOverflowError
    at apple.awt.CGraphicsDevice.getScreenInsets(Native Method)
    at apple.awt.CGraphicsDevice.getScreenInsets(CGraphicsDevice.java:673)
    at apple.awt.CToolkit.getScreenInsets(CToolkit.java:741)
    at java.awt.Window.init(Window.java:394)
    at java.awt.Window.<init>(Window.java:432)
    at java.awt.Frame.<init>(Frame.java:403)
    at java.awt.Frame.<init>(Frame.java:368)
    at javax.swing.JFrame.<init>(JFrame.java:158)
    at D3D.<init>(D3D.java:35)
    at player.<init>(player.java:1)
    at D3D.<init>(D3D.java:17)
    at player.<init>(player.java:1)

And here's the player class:

public class player extends D3D
{
  int playerX, playerY;
  boolean east, west, south, north;
  public void setPlayer()
  {
    playerX = 1; playerY = 1;
    east=true; west=false; north=false; south=false;
  }
}

And here's the D3D class:

public class D3D extends JFrame
{
  player player = new player();
  mapgeneration levelmap =开发者_如何学编程 new mapgeneration();
  boolean ONE, TWO, THREE, FOUR, FIVE;
  boolean ONEhighlight,TWOhighlight,THREEhighlight,FOURhighlight,FIVEhighlight;
  Timer timer = new Timer(250,new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
      repaint();
    }
  });

  String tracer;

  Image Example = Toolkit.getDefaultToolkit().getImage("images/example.png");
  Image Startup = Toolkit.getDefaultToolkit().getImage("images/Startup.png");
  Image ButtonHighlight = Toolkit.getDefaultToolkit().getImage("images/ButtonHighlight.png");

  public D3D()
  {
    super();
    setSize(342,277);

      ...
    JPanel main = new JPanel()
    {
      public void paintComponent(final Graphics g)
      {
        super.paintComponent(g); 
        timer.start();
        g.drawImage(Startup,0,0,this);
        ...
      };
    };
    add(main);
  }
  public void init()
  {
    player.setPlayer();
    levelmap.populateGraph();
  }
  public static void main(String[] args)
  {
    D3D game = new D3D();
    game.setTitle("Dungens:3D");
    game.init();
    game.setVisible(true);
  }
}

I've been looking at this for hours and have narrowed it down to what you see here. To be honest, it's probably some stupid little petty thing that I'm glancing over.

Thanks guys.


I see your problem. Your Player class extends your D3D GUI -- causing a kind of crazy circular referencing. This will cause a recursion as you cyclically keep creating players and D3D objects.

To prove that I'm right, just run this very simple version of your code:

public class D3D {
   Player player = new Player();

   public static void main(String[] args) {
      D3D game = new D3D();
   }
}

class Player extends D3D {

}

So when the D3D object is created, it creates a Player object, and since it extends D3D, it creates another Player object which in turn since it's a D3D object, creates another Player object, and on and on.

But even if it didn't cause recursion, Player should never extend the GUI since a player is not a gui; they're totally different logical constructs.

0

精彩评论

暂无评论...
验证码 换一张
取 消