Skip to content

Instantly share code, notes, and snippets.

@isauravmanitripathi
Created June 1, 2020 13:42
Show Gist options
  • Save isauravmanitripathi/6477adb8299ab7e0dd006795ab0baa9a to your computer and use it in GitHub Desktop.
Save isauravmanitripathi/6477adb8299ab7e0dd006795ab0baa9a to your computer and use it in GitHub Desktop.
import java.awt.Cursor;
import java.awt.Font;
import java.util.regex.Pattern;
import java.awt.Color;
import javax.swing.*;
import java.lang.Math;
public class calculator {
private static final int WINDOW_WIDTH = 410;
private static final int WINDOW_HEIGHT = 600;
private static final int BUTTON_WIDTH = 80; // Button width
private static final int BUTTON_HEIGHT = 70; // Button height
private static final int MARGIN_X = 20;
private static final int MARGIN_Y = 60;
private JFrame window; // Main window
private JTextField inText; // Input text
private JButton btnC, btnBack, btnMod, btnDiv, btn7, btn8, btn9,
btnMul, btn4, btn5, btn6, btnSub, btn1, btn2, btn3, btnAdd, btnPoint, btn0, btnEqual, btnRoot, btnPower, btnLog, btnSwitchThemes, btnSwitchToScientificMode;
private char opt = ' '; // Save the operator
private boolean go = true; // For calculate with Opt != (=)
private boolean addWrite = true; // Racordé des Nombres dans l'Affichage
private double val = 0; // Save value typed for calculation
private boolean isToggleColorSelected = false;
private boolean isScientificMode = false;
/*
Mx Calculator:
X = Row
Y = Column
+-------------------+
| +-----------+ | y[0]
| | | |
| +-----------+ |
| |
| C <- % / | y[1]
| 7 8 9 * | y[2]
| 4 5 6 - | y[3]
| 1 2 3 + | y[4]
| . 0 = | y[5]
+-------------------+
x[0] x[1] x[2] x[3]
*/
/*
+-------------------+
| +-----------+ | y[0]
| | | |
| +-----------+ |
| |
| 0 1 1 3 | y[1]
| 4 5 6 7 | y[2]
| 8 9 10 11 | y[3]
| 12 13 14 15 | y[4]
| 16 17 18 | y[5]
+-------------------+
x[0] x[1] x[2] x[3]
*/
private calculator() {
window = new JFrame("Calculator");
window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Set the width and the Height of the window
window.setLocationRelativeTo(null); // Move Window To Center
// Button fonts
Font btnFont = new Font("Comic Sans MS", Font.PLAIN, 28);
Font smallTxtBtnFont = new Font("Comic Sans MS", Font.PLAIN, 24);
btnSwitchThemes = new JButton();
btnSwitchThemes.setBounds(230, 30, 140, 18);
btnSwitchThemes.setText("Toggle colors");
btnSwitchThemes.setBackground(Color.GREEN.darker());
btnSwitchThemes.setForeground(Color.WHITE);
btnSwitchThemes.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnSwitchThemes.addActionListener(event -> onSwitchTheme());
window.add(btnSwitchThemes);
btnSwitchToScientificMode = new JButton();
btnSwitchToScientificMode.setBounds(30, 30, 140, 18);
btnSwitchToScientificMode.setText("Scientific Mode");
btnSwitchToScientificMode.setBackground(Color.BLUE.darker());
btnSwitchToScientificMode.setForeground(Color.WHITE);
btnSwitchToScientificMode.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnSwitchToScientificMode.addActionListener(event -> onShowScientificMode());
window.add(btnSwitchToScientificMode);
int j = -1;
int k = -1;
int[] x = {MARGIN_X, MARGIN_X + 90, 200, 290, 380};
int[] y = {MARGIN_Y, MARGIN_Y + 100, MARGIN_Y + 180, MARGIN_Y + 260, MARGIN_Y + 340, MARGIN_Y + 420};
inText = new JTextField("0");
inText.setBounds(x[0], y[0], 350, 70);
inText.setEditable(false);
inText.setBackground(Color.WHITE);
inText.setFont(new Font("Comic Sans MS", Font.PLAIN, 33));
window.add(inText);
btnC = new JButton("C");
btnC.setBounds(x[0], y[1], BUTTON_WIDTH, BUTTON_HEIGHT);
btnC.setFont(btnFont);
btnC.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnC.addActionListener(event -> {
repaintFont();
inText.setText("0");
opt = ' ';
val = 0;
});
window.add(btnC);
btnBack = new JButton("<-");
btnBack.setBounds(x[1], y[1], BUTTON_WIDTH, BUTTON_HEIGHT);
btnBack.setFont(btnFont);
btnBack.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnBack.addActionListener(event -> {
repaintFont();
String str = inText.getText();
StringBuilder str2 = new StringBuilder();
for (int i = 0; i < (str.length() - 1); i++) {
str2.append(str.charAt(i));
}
if (str2.toString().equals("")) {
inText.setText("0");
} else {
inText.setText(str2.toString());
}
});
window.add(btnBack);
btnMod = new JButton("%");
btnMod.setBounds(x[2], y[1], BUTTON_WIDTH, BUTTON_HEIGHT);
btnMod.setFont(btnFont);
btnMod.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnMod.addActionListener(event -> {
repaintFont();
if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
if (go) {
val = calc(val, inText.getText(), opt);
if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
inText.setText(String.valueOf((int) val));
} else {
inText.setText(String.valueOf(val));
}
opt = '%';
go = false;
addWrite = false;
}
});
window.add(btnMod);
btnDiv = new JButton("/");
btnDiv.setBounds(x[3], y[1], BUTTON_WIDTH, BUTTON_HEIGHT);
btnDiv.setFont(btnFont);
btnDiv.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnDiv.addActionListener(event -> {
repaintFont();
if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
if (go) {
val = calc(val, inText.getText(), opt);
if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
inText.setText(String.valueOf((int) val));
} else {
inText.setText(String.valueOf(val));
}
opt = '/';
go = false;
addWrite = false;
} else {
opt = '/';
}
});
window.add(btnDiv);
btn7 = new JButton("7");
btn7.setBounds(x[0], y[2], BUTTON_WIDTH, BUTTON_HEIGHT);
btn7.setFont(btnFont);
btn7.setCursor(new Cursor(Cursor.HAND_CURSOR));
btn7.addActionListener(event -> {
repaintFont();
if (addWrite) {
if (Pattern.matches("[0]*", inText.getText())) {
inText.setText("7");
} else {
inText.setText(inText.getText() + "7");
}
} else {
inText.setText("7");
addWrite = true;
}
go = true;
});
window.add(btn7);
btn8 = new JButton("8");
btn8.setBounds(x[1], y[2], BUTTON_WIDTH, BUTTON_HEIGHT);
btn8.setFont(btnFont);
btn8.setCursor(new Cursor(Cursor.HAND_CURSOR));
btn8.addActionListener(event -> {
repaintFont();
if (addWrite) {
if (Pattern.matches("[0]*", inText.getText())) {
inText.setText("8");
} else {
inText.setText(inText.getText() + "8");
}
} else {
inText.setText("8");
addWrite = true;
}
go = true;
});
window.add(btn8);
btn9 = new JButton("9");
btn9.setBounds(x[2], y[2], BUTTON_WIDTH, BUTTON_HEIGHT);
btn9.setFont(btnFont);
btn9.setCursor(new Cursor(Cursor.HAND_CURSOR));
btn9.addActionListener(event -> {
repaintFont();
if (addWrite) {
if (Pattern.matches("[0]*", inText.getText())) {
inText.setText("9");
} else {
inText.setText(inText.getText() + "9");
}
} else {
inText.setText("9");
addWrite = true;
}
go = true;
});
window.add(btn9);
btnMul = new JButton("*");
btnMul.setBounds(x[3], y[2], BUTTON_WIDTH, BUTTON_HEIGHT);
btnMul.setFont(btnFont);
btnMul.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnMul.addActionListener(event -> {
repaintFont();
if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
if (go) {
val = calc(val, inText.getText(), opt);
if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
inText.setText(String.valueOf((int) val));
} else {
inText.setText(String.valueOf(val));
}
opt = '*';
go = false;
addWrite = false;
} else {
opt = '*';
}
});
window.add(btnMul);
btn4 = new JButton("4");
btn4.setBounds(x[0], y[3], BUTTON_WIDTH, BUTTON_HEIGHT);
btn4.setFont(btnFont);
btn4.setCursor(new Cursor(Cursor.HAND_CURSOR));
btn4.addActionListener(event -> {
repaintFont();
if (addWrite) {
if (Pattern.matches("[0]*", inText.getText())) {
inText.setText("4");
} else {
inText.setText(inText.getText() + "4");
}
} else {
inText.setText("4");
addWrite = true;
}
go = true;
});
window.add(btn4);
btn5 = new JButton("5");
btn5.setBounds(x[1], y[3], BUTTON_WIDTH, BUTTON_HEIGHT);
btn5.setFont(btnFont);
btn5.setCursor(new Cursor(Cursor.HAND_CURSOR));
btn5.addActionListener(event -> {
repaintFont();
if (addWrite) {
if (Pattern.matches("[0]*", inText.getText())) {
inText.setText("5");
} else {
inText.setText(inText.getText() + "5");
}
} else {
inText.setText("5");
addWrite = true;
}
go = true;
});
window.add(btn5);
btn6 = new JButton("6");
btn6.setBounds(x[2], y[3], BUTTON_WIDTH, BUTTON_HEIGHT);
btn6.setFont(btnFont);
btn6.setCursor(new Cursor(Cursor.HAND_CURSOR));
btn6.addActionListener(event -> {
repaintFont();
if (addWrite) {
if (Pattern.matches("[0]*", inText.getText())) {
inText.setText("6");
} else {
inText.setText(inText.getText() + "6");
}
} else {
inText.setText("6");
addWrite = true;
}
go = true;
});
window.add(btn6);
btnSub = new JButton("-");
btnSub.setBounds(x[3], y[3], BUTTON_WIDTH, BUTTON_HEIGHT);
btnSub.setFont(btnFont);
btnSub.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnSub.addActionListener(event -> {
repaintFont();
if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
if (go) {
val = calc(val, inText.getText(), opt);
if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
inText.setText(String.valueOf((int) val));
} else {
inText.setText(String.valueOf(val));
}
opt = '-';
go = false;
addWrite = false;
} else {
opt = '-';
}
});
window.add(btnSub);
btn1 = new JButton("1");
btn1.setBounds(x[0], y[4], BUTTON_WIDTH, BUTTON_HEIGHT);
btn1.setFont(btnFont);
btn1.setCursor(new Cursor(Cursor.HAND_CURSOR));
btn1.addActionListener(event -> {
repaintFont();
if (addWrite) {
if (Pattern.matches("[0]*", inText.getText())) {
inText.setText("1");
} else {
inText.setText(inText.getText() + "1");
}
} else {
inText.setText("1");
addWrite = true;
}
go = true;
});
window.add(btn1);
btn2 = new JButton("2");
btn2.setBounds(x[1], y[4], BUTTON_WIDTH, BUTTON_HEIGHT);
btn2.setFont(btnFont);
btn2.setCursor(new Cursor(Cursor.HAND_CURSOR));
btn2.addActionListener(event -> {
repaintFont();
if (addWrite) {
if (Pattern.matches("[0]*", inText.getText())) {
inText.setText("2");
} else {
inText.setText(inText.getText() + "2");
}
} else {
inText.setText("2");
addWrite = true;
}
go = true;
});
window.add(btn2);
btn3 = new JButton("3");
btn3.setBounds(x[2], y[4], BUTTON_WIDTH, BUTTON_HEIGHT);
btn3.setFont(btnFont);
btn3.setCursor(new Cursor(Cursor.HAND_CURSOR));
btn3.addActionListener(event -> {
repaintFont();
if (addWrite) {
if (Pattern.matches("[0]*", inText.getText())) {
inText.setText("3");
} else {
inText.setText(inText.getText() + "3");
}
} else {
inText.setText("3");
addWrite = true;
}
go = true;
});
window.add(btn3);
btnAdd = new JButton("+");
btnAdd.setBounds(x[3], y[4], BUTTON_WIDTH, BUTTON_HEIGHT);
btnAdd.setFont(btnFont);
btnAdd.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnAdd.addActionListener(event -> {
repaintFont();
if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
if (go) {
val = calc(val, inText.getText(), opt);
if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
inText.setText(String.valueOf((int) val));
} else {
inText.setText(String.valueOf(val));
}
opt = '+';
go = false;
addWrite = false;
} else {
opt = '+';
}
});
window.add(btnAdd);
btnPoint = new JButton(".");
btnPoint.setBounds(x[0], y[5], BUTTON_WIDTH, BUTTON_HEIGHT);
btnPoint.setFont(new Font("Comic Sans MS", Font.BOLD, 32));
btnPoint.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnPoint.addActionListener(event -> {
repaintFont();
if (addWrite) {
inText.setText(inText.getText() + ".");
} else {
inText.setText("0.");
addWrite = true;
}
go = true;
});
window.add(btnPoint);
btn0 = new JButton("0");
btn0.setBounds(x[1], y[5], BUTTON_WIDTH, BUTTON_HEIGHT);
btn0.setFont(btnFont);
btn0.setCursor(new Cursor(Cursor.HAND_CURSOR));
btn0.addActionListener(event -> {
repaintFont();
if (addWrite) {
if (Pattern.matches("[0]*", inText.getText())) {
inText.setText("0");
} else {
inText.setText(inText.getText() + "0");
}
} else {
inText.setText("0");
addWrite = true;
}
go = true;
});
window.add(btn0);
btnEqual = new JButton("=");
btnEqual.setBounds(x[2], y[5], 2 * BUTTON_WIDTH + 10, BUTTON_HEIGHT);
btnEqual.setFont(btnFont);
btnEqual.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnEqual.addActionListener(event -> {
if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
if (go) {
val = calc(val, inText.getText(), opt);
if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
inText.setText(String.valueOf((int) val));
} else {
inText.setText(String.valueOf(val));
}
opt = '=';
addWrite = false;
}
});
window.add(btnEqual);
btnRoot = new JButton("√");
btnRoot.setBounds(x[4], y[1], BUTTON_WIDTH, BUTTON_HEIGHT);
btnRoot.setFont(btnFont);
btnRoot.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnRoot.addActionListener(event -> {
if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
if (go) {
val = Math.sqrt(Double.parseDouble(inText.getText()));
if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
inText.setText(String.valueOf((int) val));
} else {
inText.setText(String.valueOf(val));
}
opt = '√';
addWrite = false;
}
});
btnRoot.setVisible(false);
window.add(btnRoot);
btnPower = new JButton("pow");
btnPower.setBounds(x[4], y[2], BUTTON_WIDTH, BUTTON_HEIGHT);
btnPower.setFont(smallTxtBtnFont);
btnPower.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnPower.addActionListener(event -> {
repaintFont();
if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
if (go) {
val = calc(val, inText.getText(), opt);
if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
inText.setText(String.valueOf((int) val));
} else {
inText.setText(String.valueOf(val));
}
opt = '^';
go = false;
addWrite = false;
} else {
opt = '^';
}
});
btnPower.setVisible(false);
window.add(btnPower);
btnLog = new JButton("log");
btnLog.setBounds(x[4], y[3], BUTTON_WIDTH, BUTTON_HEIGHT);
btnLog.setFont(smallTxtBtnFont);
btnLog.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnLog.addActionListener(event -> {
if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
if (go) {
val = Math.log(Double.parseDouble(inText.getText()));
if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
inText.setText(String.valueOf((int) val));
} else {
inText.setText(String.valueOf(val));
}
opt = 'l';
addWrite = false;
}
});
btnLog.setVisible(false);
window.add(btnLog);
window.setLayout(null);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // If Click into The Red Button => End The Processus
window.setVisible(true);
}
private double calc(double x, String input, char opt) {
inText.setFont(inText.getFont().deriveFont(Font.BOLD));
double y = Double.parseDouble(input);
if (opt == '+') {
return x + y;
} else if (opt == '-') {
return x - y;
} else if (opt == '*') {
return x * y;
} else if (opt == '/') {
return x / y;
} else if (opt == '%') {
return x % y;
} else if (opt == '^') {
return Math.pow(x, y);
}
inText.setFont(inText.getFont().deriveFont(Font.PLAIN));
return y;
}
private void repaintFont() {
inText.setFont(inText.getFont().deriveFont(Font.PLAIN));
}
private void onSwitchTheme() {
if (isToggleColorSelected) {
btnSwitchThemes.setText("Toggle colors");
btnSwitchThemes.setBackground(Color.GREEN.darker());
btnSwitchThemes.setForeground(Color.WHITE);
btnC.setBackground(null);
btnBack.setBackground(null);
btnMod.setBackground(null);
btnDiv.setBackground(null);
btnMul.setBackground(null);
btnSub.setBackground(null);
btnAdd.setBackground(null);
btnRoot.setBackground(null);
btnLog.setBackground(null);
btnPower.setBackground(null);
btnEqual.setBackground(null);
btn0.setBackground(null);
btn1.setBackground(null);
btn2.setBackground(null);
btn3.setBackground(null);
btn4.setBackground(null);
btn5.setBackground(null);
btn6.setBackground(null);
btn7.setBackground(null);
btn8.setBackground(null);
btn9.setBackground(null);
btnPoint.setBackground(null);
btnC.setForeground(Color.BLACK);
btnBack.setForeground(Color.BLACK);
btnMod.setForeground(Color.BLACK);
btnDiv.setForeground(Color.BLACK);
btnMul.setForeground(Color.BLACK);
btnSub.setForeground(Color.BLACK);
btnAdd.setForeground(Color.BLACK);
btnEqual.setForeground(Color.BLACK);
btnLog.setForeground(Color.BLACK);
btnPower.setForeground(Color.BLACK);
btnRoot.setForeground(Color.BLACK);
isToggleColorSelected = false;
} else {
btnSwitchThemes.setText("Untoggle colors");
btnSwitchThemes.setBackground(null);
btnSwitchThemes.setForeground(Color.BLACK);
btnC.setBackground(Color.RED);
btnBack.setBackground(Color.ORANGE);
btnMod.setBackground(Color.GREEN);
btnDiv.setBackground(Color.PINK);
btnMul.setBackground(Color.PINK);
btnSub.setBackground(Color.PINK);
btnAdd.setBackground(Color.PINK);
btnRoot.setBackground(Color.PINK);
btnLog.setBackground(Color.PINK);
btnPower.setBackground(Color.PINK);
btnEqual.setBackground(Color.BLUE);
btn0.setBackground(Color.WHITE);
btn1.setBackground(Color.WHITE);
btn2.setBackground(Color.WHITE);
btn3.setBackground(Color.WHITE);
btn4.setBackground(Color.WHITE);
btn5.setBackground(Color.WHITE);
btn6.setBackground(Color.WHITE);
btn7.setBackground(Color.WHITE);
btn8.setBackground(Color.WHITE);
btn9.setBackground(Color.WHITE);
btnPoint.setBackground(Color.WHITE);
btnC.setForeground(Color.WHITE);
btnBack.setForeground(Color.WHITE);
btnMod.setForeground(Color.WHITE);
btnDiv.setForeground(Color.WHITE);
btnMul.setForeground(Color.WHITE);
btnSub.setForeground(Color.WHITE);
btnAdd.setForeground(Color.WHITE);
btnEqual.setForeground(Color.WHITE);
btnLog.setForeground(Color.WHITE);
btnPower.setForeground(Color.WHITE);
btnRoot.setForeground(Color.WHITE);
isToggleColorSelected = true;
}
}
private void onShowScientificMode() {
if (isScientificMode) {
window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
btnRoot.setVisible(false);
btnPower.setVisible(false);
btnLog.setVisible(false);
isScientificMode = false;
} else {
window.setSize(WINDOW_WIDTH + 80, WINDOW_HEIGHT);
btnRoot.setVisible(true);
btnPower.setVisible(true);
btnLog.setVisible(true);
isScientificMode = true;
}
}
public static void main(String[] args) {
new calculator();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment