December 24, 2020

Published December 24, 2020 by

Freehand Drawing Application with Java Swing

Here is a sample Java program which is used for freehand drawing with mouse. Features include: - Colors: You can draw in different colors. The supported colors are Black, Blue, Cyan, Gray, Green, Magenta, Orange, Pink, Red, White and Yellow. - Pointer Location: The program always shows the mouse pointer location in the title bar. The title is changed dynamically for the mouse movement. This is not a serous program – it’s a fun. You can use it for education purpose only to learn about how to draw in Java.

Output of the program

Here is the full source code:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.WindowConstants;

public class FreehandDrawing extends JFrame {

    private static final long serialVersionUID = 1L;

    public FreehandDrawing() {
        initComponents();
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(dimension.width, dimension.height - 30);
    }

    private void initComponents() {

        colorButtonGroup = new ButtonGroup();
        drawingPanel = new JPanel();
        colorPanel = new JPanel();
        radioButtonBlack = new JRadioButton();
        radioButtonBlue = new JRadioButton();
        radioButtonCyan = new JRadioButton();
        radioButtonGray = new JRadioButton();
        radioButtonGreen = new JRadioButton();
        radioButtonMagenta = new JRadioButton();
        radioButtonOrange = new JRadioButton();
        radioButtonPink = new JRadioButton();
        radioButtonRed = new JRadioButton();
        radioButtonWhite = new JRadioButton();
        radioButtonYellow = new JRadioButton();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        drawingPanel.setBorder(BorderFactory.createEtchedBorder());
        drawingPanel.addMouseListener(new MouseAdapter() {
            public void mouseExited(MouseEvent evt) {
                drawingPanelMouseExited(evt);
            }
        });
        drawingPanel.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent evt) {
                drawingPanelMouseDragged(evt);
            }

            public void mouseMoved(MouseEvent evt) {
                drawingPanelMouseMoved(evt);
            }
        });

        getContentPane().add(drawingPanel, java.awt.BorderLayout.CENTER);

        colorPanel.setBorder(BorderFactory.createEtchedBorder());
        colorPanel.setLayout(new java.awt.GridLayout(1, 0));

        radioButtonBlack.setBackground(Color.BLACK);
        colorButtonGroup.add(radioButtonBlack);
        radioButtonBlack.setForeground(Color.WHITE);
        radioButtonBlack.setSelected(true);
        radioButtonBlack.setText("Black");
        radioButtonBlack.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.BLACK;
            }
        });
        colorPanel.add(radioButtonBlack);

        radioButtonBlue.setBackground(Color.BLUE);
        colorButtonGroup.add(radioButtonBlue);
        radioButtonBlue.setText("Blue");
        radioButtonBlue.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.BLUE;
            }
        });
        colorPanel.add(radioButtonBlue);

        radioButtonCyan.setBackground(Color.CYAN);
        colorButtonGroup.add(radioButtonCyan);
        radioButtonCyan.setText("Cyan");
        radioButtonCyan.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.CYAN;
            }
        });
        colorPanel.add(radioButtonCyan);

        radioButtonGray.setBackground(Color.GRAY);
        colorButtonGroup.add(radioButtonGray);
        radioButtonGray.setText("Gray");
        radioButtonGray.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.GRAY;
            }
        });
        colorPanel.add(radioButtonGray);

        radioButtonGreen.setBackground(Color.GREEN);
        colorButtonGroup.add(radioButtonGreen);
        radioButtonGreen.setText("Green");
        radioButtonGreen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.GREEN;
            }
        });
        colorPanel.add(radioButtonGreen);

        radioButtonMagenta.setBackground(Color.MAGENTA);
        colorButtonGroup.add(radioButtonMagenta);
        radioButtonMagenta.setText("Magenta");
        radioButtonMagenta.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.MAGENTA;
            }
        });
        colorPanel.add(radioButtonMagenta);

        radioButtonOrange.setBackground(Color.ORANGE);
        colorButtonGroup.add(radioButtonOrange);
        radioButtonOrange.setText("Orange");
        radioButtonOrange.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.ORANGE;
            }
        });
        colorPanel.add(radioButtonOrange);

        radioButtonPink.setBackground(Color.PINK);
        colorButtonGroup.add(radioButtonPink);
        radioButtonPink.setText("Pink");
        radioButtonPink.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.PINK;
            }
        });
        colorPanel.add(radioButtonPink);

        radioButtonRed.setBackground(Color.RED);
        colorButtonGroup.add(radioButtonRed);
        radioButtonRed.setText("Red");
        radioButtonRed.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.RED;
            }
        });
        colorPanel.add(radioButtonRed);

        radioButtonWhite.setBackground(Color.WHITE);
        colorButtonGroup.add(radioButtonWhite);
        radioButtonWhite.setText("White");
        radioButtonWhite.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.WHITE;
            }
        });
        colorPanel.add(radioButtonWhite);

        radioButtonYellow.setBackground(Color.YELLOW);
        colorButtonGroup.add(radioButtonYellow);
        radioButtonYellow.setText("Yellow");
        radioButtonYellow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.YELLOW;
            }
        });
        colorPanel.add(radioButtonYellow);

        getContentPane().add(colorPanel, java.awt.BorderLayout.PAGE_START);

        pack();
    }

    private void drawingPanelMouseMoved(MouseEvent evt) {
        oldX = evt.getX();
        oldY = evt.getY();
        setTitle(oldX + "," + oldY + " - Freehand Drawing");
    }

    private void drawingPanelMouseExited(MouseEvent evt) {
        setTitle("Freehand Drawing");
    }

    private void drawingPanelMouseDragged(MouseEvent evt) {

        newX = evt.getX();
        newY = evt.getY();
        graphics = drawingPanel.getGraphics();
        graphics.setColor(color);
        graphics.drawLine(oldX, oldY, newX, newY);
        oldX = newX;
        oldY = newY;
        setTitle(oldX + "," + oldY + " - Freehand Drawing");
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FreehandDrawing().setVisible(true);
            }
        });
    }

    private ButtonGroup colorButtonGroup;
    private JPanel colorPanel;
    private JPanel drawingPanel;
    private JRadioButton radioButtonBlack;
    private JRadioButton radioButtonBlue;
    private JRadioButton radioButtonCyan;
    private JRadioButton radioButtonGray;
    private JRadioButton radioButtonGreen;
    private JRadioButton radioButtonMagenta;
    private JRadioButton radioButtonOrange;
    private JRadioButton radioButtonPink;
    private JRadioButton radioButtonRed;
    private JRadioButton radioButtonWhite;
    private JRadioButton radioButtonYellow;
    private int oldX, oldY, newX, newY;
    private Color color = Color.BLACK;
    private Graphics graphics;
}
Read More

December 23, 2020

Published December 23, 2020 by

Simple Calculator Program with Java Swing

A simple standard calculator developed with Java Swing. Features of the calculator includes add, subtract, multiply, divide, square root, square, cubic root, cube etc. AC and DEL button also exist for clear and delete operations.

Here is the full source code.

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;

public class SimpleJavaCalculator extends JDialog {

    private static final long serialVersionUID = 1L;

    public SimpleJavaCalculator(java.awt.Frame parent, boolean modal) {
        super(parent, "Simple Calculator", modal);
        initComponents();
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }
        SwingUtilities.updateComponentTreeUI(this);
    }

    private void initComponents() {

        panel1 = new JPanel();
        txtOutput = new JTextField();
        panel2 = new JPanel();
        btnXSquare = new JButton();
        btnXCube = new JButton();
        btn10Ex = new JButton();
        btnDel = new JButton();
        btnAC = new JButton();
        panel3 = new JPanel();
        btn7 = new JButton();
        btn8 = new JButton();
        btn9 = new JButton();
        btnQuotient = new JButton();
        btnSqrt = new JButton();
        panel4 = new JPanel();
        btn4 = new JButton();
        btn5 = new JButton();
        btn6 = new JButton();
        btnProduct = new JButton();
        btnCbrt = new JButton();
        panel5 = new JPanel();
        btn1 = new JButton();
        btn2 = new JButton();
        btn3 = new JButton();
        btnMinus = new JButton();
        btn1byX = new JButton();
        panel6 = new JPanel();
        btn0 = new JButton();
        btnPlusMinus = new JButton();
        btnDot = new JButton();
        btnPlus = new JButton();
        btnEqual = new JButton();

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridLayout(6, 0, 5, 5));

        panel1.setLayout(new java.awt.GridLayout(1, 0));

        txtOutput.setEditable(false);
        txtOutput.setHorizontalAlignment(JTextField.RIGHT);
        panel1.add(txtOutput);

        getContentPane().add(panel1);

        panel2.setLayout(new java.awt.GridLayout(0, 5, 5, 5));

        btnXSquare.setText("<html><body>x<sup>2</sup></body></html>");
        btnXSquare.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXSquareActionPerformed(evt);
            }
        });
        panel2.add(btnXSquare);

        btnXCube.setText("<html><body>x<sup>3</sup></body></html>");
        btnXCube.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXCubeActionPerformed(evt);
            }
        });
        panel2.add(btnXCube);

        btn10Ex.setText("<html><body>10<sup>x</sup></body></html>");
        btn10Ex.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn10ExActionPerformed(evt);
            }
        });
        panel2.add(btn10Ex);

        btnDel.setForeground(new java.awt.Color(255, 0, 0));
        btnDel.setText("DEL");
        btnDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelActionPerformed(evt);
            }
        });
        panel2.add(btnDel);

        btnAC.setForeground(new java.awt.Color(255, 0, 0));
        btnAC.setText("AC");
        btnAC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnACActionPerformed(evt);
            }
        });
        panel2.add(btnAC);

        getContentPane().add(panel2);

        panel3.setLayout(new java.awt.GridLayout(0, 5, 5, 5));

        btn7.setText("7");
        btn7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn7ActionPerformed(evt);
            }
        });
        panel3.add(btn7);

        btn8.setText("8");
        btn8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn8ActionPerformed(evt);
            }
        });
        panel3.add(btn8);

        btn9.setText("9");
        btn9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn9ActionPerformed(evt);
            }
        });
        panel3.add(btn9);

        btnQuotient.setText("/");
        btnQuotient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuotientActionPerformed(evt);
            }
        });
        panel3.add(btnQuotient);

        btnSqrt.setText("sqrt");
        btnSqrt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSqrtActionPerformed(evt);
            }
        });
        panel3.add(btnSqrt);

        getContentPane().add(panel3);

        panel4.setLayout(new java.awt.GridLayout(0, 5, 5, 5));

        btn4.setText("4");
        btn4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn4ActionPerformed(evt);
            }
        });
        panel4.add(btn4);

        btn5.setText("5");
        btn5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn5ActionPerformed(evt);
            }
        });
        panel4.add(btn5);

        btn6.setText("6");
        btn6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn6ActionPerformed(evt);
            }
        });
        panel4.add(btn6);

        btnProduct.setText("*");
        btnProduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProductActionPerformed(evt);
            }
        });
        panel4.add(btnProduct);

        btnCbrt.setText("cbrt");
        btnCbrt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCbrtActionPerformed(evt);
            }
        });
        panel4.add(btnCbrt);

        getContentPane().add(panel4);

        panel5.setLayout(new java.awt.GridLayout(0, 5, 5, 5));

        btn1.setText("1");
        btn1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn1ActionPerformed(evt);
            }
        });
        panel5.add(btn1);

        btn2.setText("2");
        btn2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn2ActionPerformed(evt);
            }
        });
        panel5.add(btn2);

        btn3.setText("3");
        btn3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn3ActionPerformed(evt);
            }
        });
        panel5.add(btn3);

        btnMinus.setText("-");
        btnMinus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMinusActionPerformed(evt);
            }
        });
        panel5.add(btnMinus);

        btn1byX.setText("1/x");
        btn1byX.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn1byXActionPerformed(evt);
            }
        });
        panel5.add(btn1byX);

        getContentPane().add(panel5);

        panel6.setLayout(new java.awt.GridLayout(0, 5, 5, 5));

        btn0.setText("0");
        btn0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn0ActionPerformed(evt);
            }
        });
        panel6.add(btn0);

        btnPlusMinus.setText("+/-");
        btnPlusMinus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPlusMinusActionPerformed(evt);
            }
        });
        panel6.add(btnPlusMinus);

        btnDot.setText(".");
        btnDot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDotActionPerformed(evt);
            }
        });
        panel6.add(btnDot);

        btnPlus.setText("+");
        btnPlus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPlusActionPerformed(evt);
            }
        });
        panel6.add(btnPlus);

        btnEqual.setText("=");
        btnEqual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEqualActionPerformed(evt);
            }
        });
        panel6.add(btnEqual);

        getContentPane().add(panel6);

        pack();
    }

    private void btnQuotientActionPerformed(java.awt.event.ActionEvent evt) {
        operatorClickOperation();
        setClickedOperator('/');
    }

    private void btn7ActionPerformed(java.awt.event.ActionEvent evt) {
        showDigit('7');
    }

    private void btn8ActionPerformed(java.awt.event.ActionEvent evt) {
        showDigit('8');
    }

    private void btn9ActionPerformed(java.awt.event.ActionEvent evt) {
        showDigit('9');
    }

    private void btn4ActionPerformed(java.awt.event.ActionEvent evt) {
        showDigit('4');
    }

    private void btn5ActionPerformed(java.awt.event.ActionEvent evt) {
        showDigit('5');
    }

    private void btn6ActionPerformed(java.awt.event.ActionEvent evt) {
        showDigit('6');
    }

    private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {
        showDigit('1');
    }

    private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {
        showDigit('2');
    }

    private void btn3ActionPerformed(java.awt.event.ActionEvent evt) {
        showDigit('3');
    }

    private void btn0ActionPerformed(java.awt.event.ActionEvent evt) {
        showDigit('0');
    }

    private void btnPlusMinusActionPerformed(java.awt.event.ActionEvent evt) {

        String input = txtOutput.getText();

        if (!input.equals("")) {
            double result = Double.parseDouble(input) * -1;
            showDotZeroTruncatedOutput(String.valueOf(result));
        }
        setFunctionButtonClicked(true);
    }

    private void btnDotActionPerformed(java.awt.event.ActionEvent evt) {
        if (!dotExist())
            txtOutput.setText(txtOutput.getText() + ".");
    }

    private void btnXSquareActionPerformed(java.awt.event.ActionEvent evt) {

        double input = Double.parseDouble(txtOutput.getText());
        double square = Math.pow(input, 2);
        String output = String.valueOf(square);
        showDotZeroTruncatedOutput(output);
        setFunctionButtonClicked(true);
    }

    private void btnXCubeActionPerformed(java.awt.event.ActionEvent evt) {
        double input = Double.parseDouble(txtOutput.getText());
        double square = Math.pow(input, 3);
        String output = String.valueOf(square);
        showDotZeroTruncatedOutput(output);
        setFunctionButtonClicked(true);
    }

    private void btn10ExActionPerformed(java.awt.event.ActionEvent evt) {
        double input = Double.parseDouble(txtOutput.getText());
        double square = Math.pow(10, input);
        String output = String.valueOf(square);
        showDotZeroTruncatedOutput(output);
        setFunctionButtonClicked(true);
    }

    private void btnDelActionPerformed(java.awt.event.ActionEvent evt) {

        String input = txtOutput.getText();

        if (!input.equals("")) {
            String output = input.substring(0, input.length() - 1);
            txtOutput.setText(output);
        }
        setFunctionButtonClicked(true);
    }

    private void btnACActionPerformed(java.awt.event.ActionEvent evt) {
        txtOutput.setText("");
        setFunctionButtonClicked(true);
        setOperatorClicked(false);
        beforeFirstOperatorClick = true;
        clickedOperator = '\0';
        result = 0;

    }

    private void btnSqrtActionPerformed(java.awt.event.ActionEvent evt) {
        String input = txtOutput.getText();
        if (!input.equals("")) {
            double squareRoot = Math.sqrt(Double.parseDouble(input));
            String output = String.valueOf(squareRoot);
            showDotZeroTruncatedOutput(output);
        }
        setFunctionButtonClicked(true);
    }

    private void btnCbrtActionPerformed(java.awt.event.ActionEvent evt) {

        String input = txtOutput.getText();
        if (!input.equals("")) {
            double cubicRoot = Math.cbrt(Double.parseDouble(input));
            String output = String.valueOf(cubicRoot);
            showDotZeroTruncatedOutput(output);
        }
        setFunctionButtonClicked(true);
    }

    private void btn1byXActionPerformed(java.awt.event.ActionEvent evt) {
        String input = txtOutput.getText();
        if (!input.equals("")) {
            double result = 1 / Double.parseDouble(input);
            String output = String.valueOf(result);
            showDotZeroTruncatedOutput(output);
        }
        setFunctionButtonClicked(true);
    }

    private void btnPlusActionPerformed(java.awt.event.ActionEvent evt) {
        operatorClickOperation();
        setClickedOperator('+');
    }

    private void btnMinusActionPerformed(java.awt.event.ActionEvent evt) {
        operatorClickOperation();
        setClickedOperator('-');
    }

    private void btnProductActionPerformed(java.awt.event.ActionEvent evt) {
        operatorClickOperation();
        setClickedOperator('*');
    }

    private void btnEqualActionPerformed(java.awt.event.ActionEvent evt) {
        operatorClickOperation();
        setClickedOperator('\0');
        beforeFirstOperatorClick = true;
    }

    public boolean dotExist() {
        if (txtOutput.getText().indexOf('.') >= 0) {
            return true;
        } else {
            return false;
        }
    }

    public void showDigit(char digit) {
        if (!(operatorClicked || functionButtonClicked))
            txtOutput.setText(txtOutput.getText() + digit);
        else
            txtOutput.setText(String.valueOf(digit));

        operatorClicked = false;
        setFunctionButtonClicked(false);
    }

    public void showDotZeroTruncatedOutput(String output) {
        if (output.endsWith(".0")) {
            output = output.substring(0, output.length() - 2);
        }

        txtOutput.setText(output);
    }

    public boolean isNumber(String input) {
        if (input.startsWith("-"))
            input = input.substring(1, input.length());

        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            if (!(Character.isDigit(ch) || ch == '.')) {
                return false;
            }
        }

        double number = Double.parseDouble(input);
        if (Double.isNaN(number) || Double.isInfinite(number)) {
            return false;
        }

        return true;
    }

    public void setFunctionButtonClicked(boolean functionButtonClicked) {
        this.functionButtonClicked = functionButtonClicked;
    }

    public void setOperatorClicked(boolean operatorClicked) {
        this.operatorClicked = operatorClicked;
    }

    public void setClickedOperator(char clickedOperator) {
        this.clickedOperator = clickedOperator;
    }

    public void operatorClickOperation() {
        String input = txtOutput.getText();
        if (isNumber(input)) {
            double number = Double.parseDouble(input);

            if (!beforeFirstOperatorClick) {

                switch (clickedOperator) {
                case '+':
                    result += number;
                    break;
                case '-':
                    result -= number;
                    break;
                case '*':
                    result *= number;
                    break;
                case '/':
                    result /= number;
                    break;
                }

                showDotZeroTruncatedOutput(String.valueOf(result));

            }

            else {
                result = number;
                beforeFirstOperatorClick = false;
            }

            setOperatorClicked(true);
        } else {
            result = 0;
            setClickedOperator('\0');
            beforeFirstOperatorClick = true;
        }

    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SimpleJavaCalculator dialog = new SimpleJavaCalculator(new JFrame(), true);
                dialog.setLocationRelativeTo(null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    private JButton btn0;
    private JButton btn1;
    private JButton btn10Ex;
    private JButton btn1byX;
    private JButton btn2;
    private JButton btn3;
    private JButton btn4;
    private JButton btn5;
    private JButton btn6;
    private JButton btn7;
    private JButton btn8;
    private JButton btn9;
    private JButton btnAC;
    private JButton btnCbrt;
    private JButton btnDel;
    private JButton btnDot;
    private JButton btnEqual;
    private JButton btnMinus;
    private JButton btnPlus;
    private JButton btnPlusMinus;
    private JButton btnProduct;
    private JButton btnQuotient;
    private JButton btnSqrt;
    private JButton btnXCube;
    private JButton btnXSquare;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;
    private JPanel panel5;
    private JPanel panel6;
    private JTextField txtOutput;

    private boolean operatorClicked = false;
    private boolean functionButtonClicked = false;

    private double result;

    private char clickedOperator;
    private boolean beforeFirstOperatorClick = true;
}
Read More