使用带有2D动态图形的构造函数时出现堆栈溢出错误

时间:2015-11-28 18:23:09

标签: java

我使用simulationFinal构造函数从'calling'类调用'simulationFinal类'。 simulationFinal类是2D模拟类,它模拟坐标平面和太阳位置。当我从'calling'类调用'simulationFinal类'时,我得到的错误如下:

Exception in thread "main" 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:758)
at java.awt.Window.init(Window.java:397)
at java.awt.Window.<init>(Window.java:433)
at java.awt.Frame.<init>(Frame.java:403)
at javax.swing.JFrame.<init>(JFrame.java:202)
at simulationFinal.<init>(simulationFinal.java:39)
at simulationFinal.<init>(simulationFinal.java:40)

调用类的代码是:

public class calling {  
    public static void main(String args[]) {    
        simulationFinal object = new simulationFinal(); 
    }
}

simulationFinal类的代码是:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

import javax.swing.*;

public class simulationFinal extends JComponent implements ActionListener {

    int Sunx1;
    int Suny1;
    int Mx11;
    int Mx22;
    int Mx33;
    int Mx44;
    double Mx1;
    double Mx2;
    double Mx3;
    double Mx4;
    double Sunx;
    double Suny;
    double angleAzimuth=0;
    double lengthSun;
    double targetHelight;
    double targetXdistance;
    double targetYdistance;
    double mirrorOrientation;


    public simulationFinal() {

        JFrame window = new JFrame("DanGame");
        simulationFinal game = new simulationFinal();
        window.add(game);
        window.pack();
            window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          window.setLocationRelativeTo(null);
        window.setVisible(true);

        Timer t = new Timer(10, game);//1000 miniseconds
        t.start();
    }

    public Dimension getPreferredSize() {

        return new Dimension(1000, 500);

    }

    @Override
    protected void paintComponent(Graphics g) {

       //line x1
        g.setColor(new Color(0,0,0));
        g.drawLine(50,250,450,250);

        //line y1
        g.drawLine(250,50,250,450);

        //Tower
        targetXdistance = 3;
        targetYdistance = 4;
        targetXdistance = targetXdistance*30;
        targetYdistance = -targetYdistance*30;
        g.fillOval((int)targetXdistance+250-10,(int)targetYdistance+250-10, 20, 20);

        //Tower to center
        g.drawLine((int)targetXdistance+250,(int)targetYdistance+250, 250, 250);

      //Mirror
        g.drawLine(Mx11, Mx22, Mx33, Mx44);

        //Sun line
      g.drawLine(Sunx1+15,Suny1+15, 250, 250);

        //Sun
        g.setColor(new Color(255, 0, 0));
        g.fillOval(Sunx1, Suny1, 30, 30);


    }

    @Override
    public void actionPerformed(ActionEvent e){

        angleAzimuth = angleAzimuth+0.01;
        Sunx = 250+200*Math.sin(angleAzimuth)-25;
        Sunx1 = (int)Sunx;
        Suny = 250-200*Math.cos(angleAzimuth)-25;
        Suny1 = (int)Suny;

        mirrorOrientation=mirrorOrientation+0.02;
        Mx1=250+50*Math.sin(mirrorOrientation);
        Mx2=250-50*Math.cos(mirrorOrientation);
        Mx3=250-50*Math.sin(mirrorOrientation);
        Mx4=250+50*Math.cos(mirrorOrientation);

        Mx11 = (int)Mx1;
        Mx22 = (int)Mx2;
        Mx33 = (int)Mx3;
        Mx44 = (int)Mx4;

        repaint();
    }


}

我不知道如何更换

simulationFinal game = new simulationFinal();

避免此错误。 你能给我一些提示来解决这个问题吗? 我在Mac OSX上使用eclipse。 谢谢你的帮助, 丹尼尔。

0 个答案:

没有答案