I have just created this simple program to progressively get closer to the value of pi and display it. I can't figure out a solution for my problem. Here is my code(sorry for length):
Code:
package PiCounter;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class PiGUI extends JFrame {
JTextField piDisplay = new JTextField();
JTextField calcDisplay = new JTextField();
JLabel calcLabel = new JLabel("# of calculations");
JLabel piLabel = new JLabel("Value of PI");
Calculator calc = new Calculator(this);
PiGUI() {
// Frame listener - closeing window
addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});
// Configure GUI layout
Container inter = getContentPane();
inter.setLayout ( null );
// Set the box not editable
piDisplay.setEditable(false);
calcDisplay.setEditable(false);
// Set the box background
piDisplay.setBackground(Color.black);
calcDisplay.setBackground(Color.black);
// Set the box forground
piDisplay.setBackground(Color.black);
calcDisplay.setForeground(Color.red);
// Add components to GUI
inter.add(piDisplay);
inter.add(calcDisplay);
inter.add(piLabel);
inter.add(calcLabel);
piLabel.setBounds(5,5,150,20);
piDisplay.setBounds(5,30,150,40);
calcLabel.setBounds(5,75,150,20);
calcDisplay.setBounds(5,100,150,40);
setSize(170,170); // Size of window
setTitle("PI Calculator"); // Title of window
setVisible(true); // Set visible
setResizable(false); // Disallow resize
}
}
class Calculator {
PiGUI actionGUI;
int calcCounter = 0;
double denomCount = 3;
double pi = 4;
public Calculator(PiGUI tempGUI) {
actionGUI = tempGUI;
calcSub();
}
public void calcSub() {
pi -= (4/denomCount);
denomCount += 2;
calcCounter++;
calcAdd();
}
public void calcAdd() {
pi += (4/denomCount);
denomCount += 2;
calcCounter++;
if((calcCounter % 100000) == 0) {
actionGUI.piDisplay.setText(String.valueOf(pi));
actionGUI.calcDisplay.setText(String.valueOf(calcCounter));
}
calcSub();
}
}
public class PiCounter {
public static void main(String[] args) {
PiGUI piGUI = new PiGUI();
}
}
And the error I am recieving is:
Exception in thread "main" java.lang.StackOverflowError
The display window isn't even popping up. This is most likely a simple problem that I am overlooking. That where some of you might be able to help me :-).
Thanks,
Mike