/* * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Oracle or the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * CelsiusConverterGUI.java * */ package piCalc; import java.awt.event.ActionEvent; public class piCalc2 extends javax.swing.JFrame { /** Creates new form CelsiusConverterGUI */ public piCalc2() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // //GEN-BEGIN:initComponents private void initComponents() { tempTextField = new javax.swing.JTextField(); limitLabel = new javax.swing.JLabel(); toggleButton = new javax.swing.JButton(); resetButton = new javax.swing.JButton(); nLabel = new javax.swing.JLabel(); piLabel = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Pi Calculator"); limitLabel.setText("Limit of n?"); nLabel.setText("n = 0"); piLabel.setText("π = 4"); toggleButton.setText("Start"); resetButton.setText("Reset"); toggleButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { togglePi(evt); } }); resetButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { reset(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(limitLabel)) .addGroup(layout.createSequentialGroup() .addComponent(toggleButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(nLabel) .addContainerGap(100, Short.MAX_VALUE))) .addGroup(layout.createSequentialGroup() .addComponent(resetButton)) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(piLabel)) .addContainerGap(200, Short.MAX_VALUE)) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {toggleButton, tempTextField}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(limitLabel)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(toggleButton) .addComponent(nLabel) .addComponent(piLabel)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(resetButton)) .addContainerGap(100, Short.MAX_VALUE)) ); pack(); } // //GEN-END:initComponents /* private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_convertButtonActionPerformed //Parse degrees Celsius as a double and convert to Fahrenheit int tempFahr = (int)((Double.parseDouble(tempTextField.getText())) * 1.8 + 32); fahrenheitLabel.setText(tempFahr + " Fahrenheit"); } */ private void togglePi(ActionEvent evt) { checkPlay(); } public void checkPlay(){ System.out.println(play); if(play == 0) { play = 1; calcPi(); System.out.println("calc"); toggleButton.setText("Calculating..."); } else { System.out.println("pausing..."); play = 0; toggleButton.setText("Paused"); } } private void calcPi() { if (tempTextField.getText().length() != 0) { nLimit = (int)(Double.parseDouble(tempTextField.getText())); recurse = true; } else { nLimit = 1000000000; recurse = false; } /* int nLimit = (int)(Double.parseDouble(tempTextField.getText())); if(nLimit == null) System.out.println('null'); */ /*if (play == 1 && n < nLimit) { sum += (Math.pow(-1, n)* 4)/(2*n+1); n++; nLabel.setText("n = "+n); piLabel.setText("π = "+sum); /*if(recurse) { calcPi(); } } */ while (n < nLimit) { sum += (Math.pow(-1, n)* 4)/(2*n+1); n += 1; nLabel.setText("n = "+n); piLabel.setText("π = "+sum); System.out.println(n+" "+sum); } checkPlay(); } public void reset(ActionEvent evt) { n = 0; sum = 0; nLabel.setText("n = "+n); piLabel.setText("π = "+sum); toggleButton.setText("Start"); } //GEN-LAST:event_convertButtonActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new piCalc2().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel limitLabel; private javax.swing.JButton toggleButton; private javax.swing.JButton resetButton; private javax.swing.JLabel nLabel; private javax.swing.JLabel piLabel; private javax.swing.JTextField tempTextField; int play = 0; int n = 0; double sum = 0; int nLimit = 10000; boolean recurse; // End of variables declaration//GEN-END:variables }