Advertisement
Shinmera

Anon Exercise Un-Fucked

Dec 15th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.72 KB | None | 0 0
  1. // Un fucked code from: http://pastebin.com/xJJ4jYdE
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.text.DecimalFormat;
  5. import javax.swing.*;
  6.  
  7. class HoveredButton extends JButton implements MouseListener{
  8.     private Color defaultForeground;
  9.  
  10.     public HoveredButton(String label){
  11.         super(label);
  12.         addMouseListener(this);
  13.     }
  14.  
  15.     public void mouseClicked(MouseEvent evt){}
  16.     public void mousePressed(MouseEvent evt){}
  17.     public void mouseReleased(MouseEvent evt){}
  18.     public void mouseEntered(MouseEvent evt){
  19.         defaultForeground = evt.getComponent().getForeground();
  20.         setForeground(Color.WHITE);
  21.     }
  22.  
  23.     public void mouseExited(MouseEvent evt){
  24.         setForeground(defaultForeground);
  25.     }
  26. }
  27.  
  28. public class Calculator extends JFrame implements ActionListener {
  29.     private static final DecimalFormat FORMAT = new DecimalFormat("###,##0.######;-###,##0.######");
  30.  
  31.     private JTextField display = null;
  32.     private String lastOperation = "";
  33.     private double buffer = 0.0;
  34.     private String current = "0";
  35.  
  36.     public static void main(String[] args){
  37.         Calculator calc = new Calculator();
  38.         calc.setVisible(true);
  39.     }
  40.  
  41.     private HoveredButton addButton(JPanel panel, String label, Color color){
  42.         final HoveredButton button = new HoveredButton(label);
  43.         Action action = new AbstractAction(label){
  44.                 public void actionPerformed(ActionEvent e){button.doClick();}
  45.             };
  46.         action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(label));
  47.         button.getActionMap().put("autoAction", action);
  48.         button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put((KeyStroke)action.getValue(Action.ACCELERATOR_KEY), "autoAction");
  49.         button.addActionListener(this);
  50.         button.setBackground(color);
  51.         panel.add(button);
  52.         return button;
  53.     }
  54.  
  55.     public Calculator() {
  56.         setTitle("My Calculator v1.0");
  57.         setSize(248, 222);
  58.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  59.         setLayout(new BorderLayout());
  60.        
  61.         display = new JTextField(15);
  62.         display.setText("0");
  63.         display.setHorizontalAlignment(JTextField.RIGHT);
  64.         display.setEditable(false);
  65.    
  66.         JPanel panel = new JPanel();
  67.         panel.setLayout(new GridLayout(5, 4));
  68.         addButton(panel, "7", Color.LIGHT_GRAY);
  69.         addButton(panel, "8", Color.LIGHT_GRAY);
  70.         addButton(panel, "9", Color.LIGHT_GRAY);
  71.         addButton(panel, "+", Color.CYAN);
  72.  
  73.         addButton(panel, "4", Color.LIGHT_GRAY);
  74.         addButton(panel, "5", Color.LIGHT_GRAY);
  75.         addButton(panel, "6", Color.LIGHT_GRAY);
  76.         addButton(panel, "-", Color.CYAN);
  77.  
  78.         addButton(panel, "1", Color.LIGHT_GRAY);
  79.         addButton(panel, "2", Color.LIGHT_GRAY);
  80.         addButton(panel, "3", Color.LIGHT_GRAY);
  81.         addButton(panel, "*", Color.CYAN);
  82.  
  83.         addButton(panel, ".", Color.LIGHT_GRAY);
  84.         addButton(panel, "0", Color.LIGHT_GRAY);
  85.         addButton(panel, "Sqrt", Color.CYAN);
  86.         addButton(panel, "/", Color.CYAN);
  87.  
  88.         addButton(panel, "C", Color.MAGENTA);
  89.         addButton(panel, "<-", Color.MAGENTA);
  90.         addButton(panel, "%", Color.CYAN);
  91.         addButton(panel, "=", Color.GREEN);
  92.        
  93.         JMenuBar menuBar = new JMenuBar();
  94.         JMenu m_file = new JMenu("File");
  95.         JMenuItem i_exit = new JMenuItem("Exit");
  96.         i_exit.addActionListener(this); //Should have a separate actionlistener for the menu, but I can't be bothered.
  97.        
  98.         m_file.add(i_exit);
  99.         menuBar.add(m_file);
  100.  
  101.         setJMenuBar(menuBar);      
  102.         add(display, BorderLayout.PAGE_START);
  103.         add(panel, BorderLayout.CENTER);
  104.        
  105.         pack();
  106.     }
  107.  
  108.     @Override
  109.     public void actionPerformed(ActionEvent e) {
  110.         String cmd = e.getActionCommand();
  111.  
  112.         if(cmd.equals("<-")){
  113.             if(current.length() > 1){
  114.                 current=Double.parseDouble(current.substring(0, current.length()-1))+"";
  115.             }else{current="0";}
  116.         }else if(cmd.equals("C")){
  117.             current = "0";
  118.             buffer = 0.0;
  119.         }else if(cmd.equals("%")){
  120.             buffer = Double.parseDouble(current)/100;
  121.             current = buffer+"";
  122.         }else if(cmd.equals("+")||cmd.equals("-")||cmd.equals("*")||cmd.equals("/")){
  123.             buffer = Double.parseDouble(current);
  124.             current = "0";
  125.             lastOperation = cmd;
  126.         }else if(cmd.equals("Sqrt")){
  127.             buffer = Math.sqrt(Double.parseDouble(current));
  128.             current = buffer+"";
  129.             lastOperation = cmd;
  130.         }else if(cmd.equals("=") && !lastOperation.isEmpty()){
  131.             try{
  132.                 buffer = calculate(buffer, Double.parseDouble(current), lastOperation.charAt(0));
  133.                 current = buffer+"";
  134.                 lastOperation = "";
  135.             }catch(IllegalArgumentException ex){/*Apparently we don't care about this.*/}
  136.         }else if(cmd.equals(".")){
  137.             if(current.indexOf('.')==-1)
  138.                 current = current+".";
  139.         }else if(cmd.equals("Exit")){
  140.             System.exit(0);
  141.         }else if(Character.isDigit(cmd.charAt(0))){
  142.             current = current+cmd;
  143.         }
  144.  
  145.         display.setText(FORMAT.format(Double.parseDouble(current)));
  146.     }
  147.    
  148.     double calculate(double loperand, double roperand, char operator) {
  149.         switch(operator){
  150.         case '+': return loperand + roperand;
  151.         case '-': return loperand - roperand;
  152.         case '*': return loperand * roperand;
  153.         case '/': return loperand / roperand;
  154.         }
  155.         throw new IllegalArgumentException("calculate: Illegal operator encountered");
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement