0

I am calling a 'simulationFinal class' from the 'calling' class using a simulationFinal constructor. The simulationFinal class is a 2D simulation class which simulate a coordinate plane and the sun position. The error I got when I call the 'simulationFinal class' from the 'calling' class is as below:

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)

The code of the calling class is:

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

the code of the simulationFinal class is:

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


}

I am not sure how do I replace the

simulationFinal game = new simulationFinal();

to avoid this error. Can you give me some hint to resolve this problem? I am using eclipse on Mac OSX. Thanks for your help, Daniel.

3
  • 2
    Why do you call the constructor within the constructor? Commented Nov 28, 2015 at 18:24
  • remove simulationFinal game=new simulationFinal(); Commented Nov 28, 2015 at 18:26
  • But once I remove simulationFinal game=new simulationFinal(); , how can I declare the 'game' variable? Commented Nov 28, 2015 at 18:53

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.