TheGadgetCatTumblr

SciCalc (March 20th)

Mar 20th, 2012
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.63 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.*;
  5. import javax.swing.border.Border;
  6.  
  7. public class SciCalc {
  8.  
  9.     //Frame constructs
  10.     public static JFrame frame;
  11.     public static JPanel mainPanel;
  12.     public static JPanel numbersPanel;
  13.     public static JPanel buttonPanel;
  14.     public static JTextArea output;
  15.     public static JTextArea input;
  16.    
  17.     //numerical buttons
  18.     public static JButton one;
  19.     public static JButton two;
  20.     public static JButton three;
  21.     public static JButton four;
  22.     public static JButton five;
  23.     public static JButton six;
  24.     public static JButton seven;
  25.     public static JButton eight;
  26.     public static JButton nine;
  27.     public static JButton zero;
  28.    
  29.     //operator buttons
  30.     public static JButton add;
  31.     public static JButton subtract;
  32.     public static JButton multiply;
  33.     public static JButton divide;
  34.     public static JButton equals;
  35.    
  36.     //other functions
  37.     public static JButton clear;
  38.     public static JButton clearAll;
  39.     public static JButton decimal;
  40.     public static JButton back;
  41.    
  42.     //Action listener
  43.     public static ActionListener buttonListener;
  44.    
  45.     //Doubles for storing math
  46.     public static double numOne = 0;
  47.     public static double numTwo = 0;
  48.    
  49.     //String for displaying the operator
  50.     public static String displayOperator = "";
  51.    
  52.     //Boolean for when to overwrite the current input with totally new input
  53.     public static boolean overwrite = false;
  54.    
  55.     public static boolean firstInput = true;
  56.    
  57.     //JButton array
  58.     public static JButton[] buttons;
  59.    
  60.     public static void main(String[] args){
  61.        
  62.         //initialize all the components
  63.         Initialize();
  64.         //set up the GUI
  65.         GUI();
  66.         //update the button presses and do the math
  67.         Update();
  68.        
  69.     }
  70.    
  71.     public static void Initialize(){
  72.        
  73.         //simple inits for frame, panels and text fields
  74.         frame = new JFrame("Calculator");
  75.        
  76.         mainPanel = new JPanel();
  77.         numbersPanel = new JPanel();
  78.         buttonPanel = new JPanel();
  79.        
  80.         output = new JTextArea();
  81.         input = new JTextArea();
  82.        
  83.         frame.setSize(300,380);
  84.         frame.setResizable(false);
  85.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  86.        
  87.         mainPanel.setSize(300,380);
  88.        
  89.         //create a border for the numbersPanel
  90.         Border inputOutputBorder = BorderFactory.createLineBorder(Color.black);
  91.        
  92.         numbersPanel.setPreferredSize(new Dimension(280,60));
  93.         numbersPanel.setBackground(Color.white);
  94.         numbersPanel.setBorder(inputOutputBorder);
  95.        
  96.         buttonPanel.setPreferredSize(new Dimension(290,380));
  97.        
  98.         output.setPreferredSize(new Dimension(268,19));
  99.         output.setEditable(false);
  100.        
  101.         input.setPreferredSize(new Dimension(268,29));
  102.         input.setEditable(false);
  103.        
  104.         //universal dimension to make it easier to change later
  105.         Dimension buttonDimension = new Dimension(65,50);
  106.        
  107.         //create buttons
  108.         one = new JButton("1");
  109.         two = new JButton("2");
  110.         three = new JButton("3");
  111.         four = new JButton("4");
  112.         five = new JButton("5");
  113.         six = new JButton("6");
  114.         seven = new JButton("7");
  115.         eight = new JButton("8");
  116.         nine = new JButton("9");
  117.         zero = new JButton("0");
  118.        
  119.         //operator buttons
  120.         add = new JButton("+");
  121.         subtract = new JButton("-");
  122.         multiply = new JButton("x");
  123.         divide = new JButton("/");
  124.         equals = new JButton("=");
  125.    
  126.         //other buttons
  127.         clear = new JButton("C");
  128.         clearAll = new JButton("Clear All");
  129.         decimal = new JButton(".");
  130.         back = new JButton("<==");
  131.        
  132.         //add buttons to buttons array
  133.         buttons = new JButton[]{one,two,three,add,
  134.                                 four,five,six,subtract,
  135.                                 seven,eight,nine,multiply
  136.                                 ,zero,decimal,clear,divide,
  137.                                 clearAll, equals, back};
  138.        
  139.         //set dimensions
  140.         for(int i=0; i < buttons.length; i++){
  141.             buttons[i].setPreferredSize(buttonDimension);
  142.         }
  143.        
  144.         clearAll.setPreferredSize(new Dimension(100,50));
  145.     }
  146.    
  147.     public static void GUI(){
  148.        
  149.         //add everything to the appropriate frame/panel
  150.         frame.add(mainPanel);
  151.        
  152.         mainPanel.add(numbersPanel);
  153.         mainPanel.add(buttonPanel);
  154.        
  155.         numbersPanel.add(output);
  156.         numbersPanel.add(input);
  157.        
  158.         //add buttons to button panel
  159.         for(int i = 0; i < buttons.length; i++){
  160.             buttonPanel.add(buttons[i]);
  161.         }
  162.        
  163.         //make sure it's all visible
  164.         frame.setVisible(true);
  165.     }
  166.    
  167.     public static void Update(){
  168.        
  169.         //the action listener to control all the buttons
  170.         buttonListener = new ActionListener(){
  171.             public void actionPerformed(ActionEvent e) {
  172.                 //input for numerical buttons
  173.                 if(e.getSource().equals(one))
  174.                     overwriteCheck(1);
  175.                 else if(e.getSource().equals(two))
  176.                     overwriteCheck(2);
  177.                 else if(e.getSource().equals(three))
  178.                     overwriteCheck(3);
  179.                 else if(e.getSource().equals(four))
  180.                     overwriteCheck(4);
  181.                 else if(e.getSource().equals(five))
  182.                     overwriteCheck(5);
  183.                 else if(e.getSource().equals(six))
  184.                     overwriteCheck(6);
  185.                 else if(e.getSource().equals(seven))
  186.                     overwriteCheck(7);
  187.                 else if(e.getSource().equals(eight))
  188.                     overwriteCheck(8);
  189.                 else if(e.getSource().equals(nine))
  190.                     overwriteCheck(9);
  191.                 else if(e.getSource().equals(zero))
  192.                     overwriteCheck(0);
  193.                
  194.                 //for adding decimals
  195.                 else if(e.getSource().equals(decimal))
  196.                     input.append(".");
  197.                
  198.                 //for inputting math operators
  199.                 else if(e.getSource().equals(add) || e.getSource().equals(subtract)
  200.                         || e.getSource().equals(multiply) || e.getSource().equals(divide)){
  201.                     //don't deal with empty strings
  202.                     if(!input.getText().equals(""))
  203.                         numOne = Double.parseDouble(input.getText());
  204.                    
  205.                     if(e.getSource().equals(add)){
  206.                         displayOperator = "+";
  207.                         numTwo += numOne;
  208.                     }
  209.                     else if(e.getSource().equals(subtract)){
  210.                         displayOperator = "-";
  211.                         if(firstInput)
  212.                             numTwo = numOne;
  213.                         else
  214.                             numTwo -= numOne;
  215.                     }
  216.                     else if(e.getSource().equals(multiply)){
  217.                         displayOperator = "*";
  218.                         if(firstInput)
  219.                             numTwo = numOne;
  220.                         else
  221.                             numTwo *= numOne;
  222.                     }
  223.                     else if(e.getSource().equals(divide)){
  224.                         displayOperator = "/";
  225.                         if(firstInput)
  226.                             numTwo = numOne;
  227.                         else
  228.                             numTwo /= numOne;
  229.                     }
  230.                    
  231.                     output.append(numOne + " " + displayOperator + " ");
  232.                     overwrite = true;
  233.                     firstInput = false;
  234.                 }
  235.                
  236.                 //clear the input
  237.                 else if(e.getSource().equals(clear)){
  238.                     //clear text field
  239.                     input.setText("");
  240.                    
  241.                     //clear number in memory
  242.                     numOne = 0;
  243.                 }
  244.                
  245.                 //clear the whole calculator
  246.                 else if(e.getSource().equals(clearAll)){
  247.                     //clear the text fields
  248.                     input.setText("");
  249.                     output.setText("");
  250.                    
  251.                     //clear the numbers in memory
  252.                     numOne = 0;
  253.                     numTwo = 0;
  254.                 }
  255.                
  256.                 //Actually display the answer to the users
  257.                 else if(e.getSource().equals(equals)){
  258.                     //do last math
  259.                     String lastOperator = "";
  260.                     if(!output.getText().equals("")){
  261.                         lastOperator = output.getText();
  262.                         int length = output.getText().length();
  263.                         lastOperator = lastOperator.substring(length-2);
  264.                         lastOperator = lastOperator.trim();
  265.                     }
  266.                    
  267.                     System.out.println(lastOperator);
  268.                    
  269.                     //do the last bit of math with the input, depending on the last operator
  270.                     double tempInput;
  271.                    
  272.                     if(!input.getText().equals(null))
  273.                          tempInput = Double.parseDouble(input.getText());
  274.                     else
  275.                         tempInput = 0;
  276.                    
  277.                     if(lastOperator.equals("+"))
  278.                         numTwo += tempInput;
  279.                     else if(lastOperator.equals("-"))
  280.                         numTwo -= tempInput;
  281.                     else if(lastOperator.equals("*"))
  282.                         numTwo *= tempInput;
  283.                     else if(lastOperator.equals("/"))
  284.                         numTwo /= tempInput;
  285.                    
  286.                     //clear math
  287.                     output.setText("");
  288.                    
  289.                     //display answer in input
  290.                     input.setText("" + numTwo);
  291.                    
  292.                     //allow past input to be totally overwritten
  293.                     overwrite = true;
  294.                     firstInput = true;
  295.                    
  296.                     //clear internal values
  297.                     numOne = 0;
  298.                     numTwo = 0;
  299.                 }
  300.                    
  301.                 //delete last entered number
  302.                 else if(e.getSource().equals(back)){
  303.                     //copy the input for modification
  304.                     String changedInput = input.getText();
  305.                    
  306.                     //delete the last inputed character in the string
  307.                     if(!input.getText().equals(""))
  308.                         changedInput = changedInput.substring(0, changedInput.length()-1);
  309.                    
  310.                     //set the input to the reduced string
  311.                     input.setText(changedInput);
  312.                 }
  313.             }
  314.         };
  315.        
  316.         //add all the button listeners
  317.         for(int i = 0; i < buttons.length; i++){
  318.             buttons[i].addActionListener(buttonListener);
  319.         }
  320.     }
  321.    
  322.     //if overwrite is true, overwrite current input and then switch to false
  323.     public static void overwriteCheck(int x){
  324.         if(overwrite == true){
  325.             input.setText("" + x);
  326.             overwrite = false;
  327.         }
  328.         else
  329.             input.append("" + x);
  330.     }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment