TheGadgetCatTumblr

RPN Calculator (March 19th)

Mar 19th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.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 RPNCalc {
  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.     //JButton array
  56.     public static JButton[] buttons;
  57.    
  58.     public static void main(String[] args){
  59.        
  60.         //initialize all the components
  61.         Initialize();
  62.         //set up the GUI
  63.         GUI();
  64.         //update the button presses and do the math
  65.         Update();
  66.        
  67.     }
  68.    
  69.     public static void Initialize(){
  70.        
  71.         //simple inits for frame, panels and text fields
  72.         frame = new JFrame("Calculator");
  73.        
  74.         mainPanel = new JPanel();
  75.         numbersPanel = new JPanel();
  76.         buttonPanel = new JPanel();
  77.        
  78.         output = new JTextArea();
  79.         input = new JTextArea();
  80.        
  81.         frame.setSize(300,380);
  82.         frame.setResizable(false);
  83.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  84.        
  85.         mainPanel.setSize(300,380);
  86.        
  87.         //create a border for the numbersPanel
  88.         Border inputOutputBorder = BorderFactory.createLineBorder(Color.black);
  89.        
  90.         numbersPanel.setPreferredSize(new Dimension(280,60));
  91.         numbersPanel.setBackground(Color.white);
  92.         numbersPanel.setBorder(inputOutputBorder);
  93.        
  94.         buttonPanel.setPreferredSize(new Dimension(290,380));
  95.        
  96.         output.setPreferredSize(new Dimension(268,19));
  97.         output.setEditable(false);
  98.        
  99.         input.setPreferredSize(new Dimension(268,29));
  100.         input.setEditable(false);
  101.        
  102.         //universal dimension to make it easier to change later
  103.         Dimension buttonDimension = new Dimension(65,50);
  104.        
  105.         //create buttons
  106.         one = new JButton("1");
  107.         two = new JButton("2");
  108.         three = new JButton("3");
  109.         four = new JButton("4");
  110.         five = new JButton("5");
  111.         six = new JButton("6");
  112.         seven = new JButton("7");
  113.         eight = new JButton("8");
  114.         nine = new JButton("9");
  115.         zero = new JButton("0");
  116.        
  117.         //operator buttons
  118.         add = new JButton("+");
  119.         subtract = new JButton("-");
  120.         multiply = new JButton("x");
  121.         divide = new JButton("\\");
  122.         equals = new JButton("=");
  123.    
  124.         //other buttons
  125.         clear = new JButton("C");
  126.         clearAll = new JButton("Clear All");
  127.         decimal = new JButton(".");
  128.         back = new JButton("<==");
  129.        
  130.         //add buttons to buttons array
  131.         buttons = new JButton[]{one,two,three,add,
  132.                                 four,five,six,subtract,
  133.                                 seven,eight,nine,multiply
  134.                                 ,zero,decimal,clear,divide,
  135.                                 clearAll, equals, back};
  136.        
  137.         //set dimensions
  138.         for(int i=0; i < buttons.length; i++){
  139.             buttons[i].setPreferredSize(buttonDimension);
  140.         }
  141.        
  142.         clearAll.setPreferredSize(new Dimension(100,50));
  143.     }
  144.    
  145.     public static void GUI(){
  146.        
  147.         //add everything to the appropriate frame/panel
  148.         frame.add(mainPanel);
  149.        
  150.         mainPanel.add(numbersPanel);
  151.         mainPanel.add(buttonPanel);
  152.        
  153.         numbersPanel.add(output);
  154.         numbersPanel.add(input);
  155.        
  156.         //add buttons to button panel
  157.         for(int i = 0; i < buttons.length; i++){
  158.             buttonPanel.add(buttons[i]);
  159.         }
  160.        
  161.         //make sure it's all visible
  162.         frame.setVisible(true);
  163.     }
  164.    
  165.     public static void Update(){
  166.        
  167.         //the action listener to control all the buttons
  168.         buttonListener = new ActionListener(){
  169.             public void actionPerformed(ActionEvent e) {
  170.                 //input for numerical buttons
  171.                 if(e.getSource().equals(one))
  172.                     overwriteCheck(1);
  173.                 else if(e.getSource().equals(two))
  174.                     overwriteCheck(2);
  175.                 else if(e.getSource().equals(three))
  176.                     overwriteCheck(3);
  177.                 else if(e.getSource().equals(four))
  178.                     overwriteCheck(4);
  179.                 else if(e.getSource().equals(five))
  180.                     overwriteCheck(5);
  181.                 else if(e.getSource().equals(six))
  182.                     overwriteCheck(6);
  183.                 else if(e.getSource().equals(seven))
  184.                     overwriteCheck(7);
  185.                 else if(e.getSource().equals(eight))
  186.                     overwriteCheck(8);
  187.                 else if(e.getSource().equals(nine))
  188.                     overwriteCheck(9);
  189.                 else if(e.getSource().equals(zero))
  190.                     overwriteCheck(0);
  191.                
  192.                 //for adding decimals
  193.                 else if(e.getSource().equals(decimal))
  194.                     input.append(".");
  195.                
  196.                 //for inputting math operators
  197.                 else if(e.getSource().equals(add) || e.getSource().equals(subtract)
  198.                         || e.getSource().equals(multiply) || e.getSource().equals(divide)){
  199.                     //don't deal with empty strings
  200.                     if(input.getText().equals(""))
  201.                         numOne = 0;
  202.                     else
  203.                         numOne = Double.parseDouble(input.getText());
  204.                    
  205.                     //double check the button and do the appropriate math
  206.                     if(e.getSource().equals(add)){
  207.                         numTwo += numOne;
  208.                         displayOperator = "+";
  209.                         overwrite = true;
  210.                     }
  211.                     else if(e.getSource().equals(subtract)){
  212.                         numTwo -= numOne;
  213.                         displayOperator = "-";
  214.                         overwrite = true;
  215.                     }
  216.                     else if(e.getSource().equals(multiply)){
  217.                         numTwo *= numOne;
  218.                         displayOperator = "*";
  219.                         overwrite = true;
  220.                     }
  221.                     else if(e.getSource().equals(divide)){
  222.                         numTwo /= numOne;
  223.                         displayOperator = "/";
  224.                         overwrite = true;
  225.                     }
  226.                    
  227.                     //display the math
  228.                     output.append(numOne + " " + displayOperator + " ");
  229.                 }
  230.                
  231.                 //clear the input
  232.                 else if(e.getSource().equals(clear)){
  233.                     //clear text field
  234.                     input.setText("");
  235.                    
  236.                     //clear number in memory
  237.                     numOne = 0;
  238.                 }
  239.                
  240.                 //clear the whole calculator
  241.                 else if(e.getSource().equals(clearAll)){
  242.                     //clear the text fields
  243.                     input.setText("");
  244.                     output.setText("");
  245.                    
  246.                     //clear the numbers in memory
  247.                     numOne = 0;
  248.                     numTwo = 0;
  249.                 }
  250.                
  251.                 //Actually display the answer to the users
  252.                 else if(e.getSource().equals(equals)){
  253.                     //clear math
  254.                     output.setText("");
  255.                    
  256.                     //display answer in input
  257.                     input.setText("" + numTwo);
  258.                    
  259.                     //allow past input to be totally overwritten
  260.                     overwrite = true;
  261.                 }
  262.                    
  263.                 //delete last entered number
  264.                 else if(e.getSource().equals(back)){
  265.                     //copy the input for modification
  266.                     String changedInput = input.getText();
  267.                    
  268.                     //delete the last inputed character in the string
  269.                     if(!input.getText().equals(""))
  270.                         changedInput = changedInput.substring(0, changedInput.length()-1);
  271.                    
  272.                     //set the input to the reduced string
  273.                     input.setText(changedInput);
  274.                 }
  275.             }
  276.         };
  277.        
  278.         //add all the button listeners
  279.         for(int i = 0; i < buttons.length; i++){
  280.             buttons[i].addActionListener(buttonListener);
  281.         }
  282.     }
  283.    
  284.     //if overwrite is true, overwrite current input and then switch to false
  285.     public static void overwriteCheck(int x){
  286.         if(overwrite == true){
  287.             input.setText("" + x);
  288.             overwrite = false;
  289.         }
  290.         else
  291.             input.append("" + x);
  292.     }
  293. }
Advertisement
Add Comment
Please, Sign In to add comment