diff --git a/SimpleJavaCalculator.jar b/SimpleJavaCalculator.jar deleted file mode 100644 index 23f5337..0000000 Binary files a/SimpleJavaCalculator.jar and /dev/null differ diff --git a/icon/icon-notice.txt b/icon/icon-notice.txt new file mode 100644 index 0000000..d2577c2 --- /dev/null +++ b/icon/icon-notice.txt @@ -0,0 +1 @@ +`icon.png`, all rights reserved by xdvrx1 diff --git a/icon/icon.png b/icon/icon.png new file mode 100644 index 0000000..527af6f Binary files /dev/null and b/icon/icon.png differ diff --git a/src/simplejavacalculator/Calculator.java b/src/simplejavacalculator/Calculator.java index 73a40ad..9642b62 100644 --- a/src/simplejavacalculator/Calculator.java +++ b/src/simplejavacalculator/Calculator.java @@ -103,7 +103,7 @@ public Double calculateMono(MonoOperatorModes newMode, Double num) { if (num == 0 || num % 180 == 0) { return 0.0; } - if (num % 90 == 0 && num % 180 != 0) + if (num % 90 == 0 && num % 180 != 0) { return NaN; } diff --git a/src/simplejavacalculator/UI.java b/src/simplejavacalculator/UI.java index 2bad76c..b7a2633 100644 --- a/src/simplejavacalculator/UI.java +++ b/src/simplejavacalculator/UI.java @@ -26,219 +26,306 @@ import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; +import java.awt.Font; +import javax.swing.Box; +import javax.swing.BoxLayout; +import java.awt.Toolkit; +import java.awt.Image; +import javax.swing.ImageIcon; public class UI implements ActionListener { - - private final JFrame frame; - private final JPanel panel; - private final JTextArea text; - private final JButton but[], butAdd, butMinus, butMultiply, butDivide, - butEqual, butCancel, butSquareRoot, butSquare, butOneDevidedBy, - butCos, butSin, butTan, butxpowerofy, butlog, butrate, butabs, butBinary; - private final Calculator calc; - - private final String[] buttonValue = {"0", "1", "2", "3", "4", "5", "6", - "7", "8", "9"}; - - public UI() { - frame = new JFrame("Calculator PH"); - frame.setResizable(false); - panel = new JPanel(new FlowLayout()); - - text = new JTextArea(2, 25); - but = new JButton[10]; - for (int i = 0; i < 10; i++) { - but[i] = new JButton(String.valueOf(i)); - } - - butAdd = new JButton("+"); - butMinus = new JButton("-"); - butMultiply = new JButton("*"); - butDivide = new JButton("/"); - butEqual = new JButton("="); - butSquareRoot = new JButton("√"); - butSquare = new JButton("x*x"); - butOneDevidedBy = new JButton("1/x"); - butCos = new JButton("Cos"); - butSin = new JButton("Sin"); - butTan = new JButton("Tan"); - butxpowerofy = new JButton("x^y"); - butlog = new JButton("log10(x)"); - butrate = new JButton("x%"); - butabs = new JButton("abs(x)"); - - butCancel = new JButton("C"); - butBinary = new JButton("Bin"); - - calc = new Calculator(); - } - - public void init() { - frame.setVisible(true); - frame.setSize(330, 300); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.add(panel); - - panel.add(text); - - for (int i = 1; i < 10; i++) { - panel.add(but[i]); - but[i].addActionListener(this); - } - panel.add(but[0]); - but[0].addActionListener(this); - - panel.add(butAdd); - panel.add(butMinus); - panel.add(butMultiply); - panel.add(butDivide); - panel.add(butSquare); - panel.add(butSquareRoot); - panel.add(butOneDevidedBy); - panel.add(butCos); - panel.add(butSin); - panel.add(butTan); - panel.add(butxpowerofy); - panel.add(butlog); - panel.add(butrate); - panel.add(butabs); - panel.add(butabs); - panel.add(butBinary); - - panel.add(butEqual); - panel.add(butCancel); - - butAdd.addActionListener(this); - butMinus.addActionListener(this); - butMultiply.addActionListener(this); - butDivide.addActionListener(this); - butSquare.addActionListener(this); - butSquareRoot.addActionListener(this); - butOneDevidedBy.addActionListener(this); - butCos.addActionListener(this); - butSin.addActionListener(this); - butTan.addActionListener(this); - butxpowerofy.addActionListener(this); - butlog.addActionListener(this); - butrate.addActionListener(this); - butabs.addActionListener(this); - butBinary.addActionListener(this); - - butEqual.addActionListener(this); - butCancel.addActionListener(this); - } - - @Override - public void actionPerformed(ActionEvent e) { - final Object source = e.getSource(); - - for (int i = 0; i < 10; i++) { - if (source == but[i]) { - text.replaceSelection(buttonValue[i]); - return; - } - } - - if (source == butAdd) { - writer(calc.calculateBi(Calculator.BiOperatorModes.add, reader())); - } - - if (source == butMinus) { - writer(calc.calculateBi(Calculator.BiOperatorModes.minus, reader())); - } - - if (source == butMultiply) { - writer(calc.calculateBi(Calculator.BiOperatorModes.multiply, - reader())); - } - - if (source == butDivide) { - writer(calc - .calculateBi(Calculator.BiOperatorModes.divide, reader())); - } - if (source == butxpowerofy) { - writer(calc - .calculateBi(Calculator.BiOperatorModes.xpowerofy, reader())); - } - - if (source == butSquare) { - writer(calc.calculateMono(Calculator.MonoOperatorModes.square, - reader())); - } - - if (source == butSquareRoot) { - writer(calc.calculateMono(Calculator.MonoOperatorModes.squareRoot, - reader())); - } - - if (source == butOneDevidedBy) { - writer(calc.calculateMono( - Calculator.MonoOperatorModes.oneDevidedBy, reader())); - } - - if (source == butCos) { - writer(calc.calculateMono(Calculator.MonoOperatorModes.cos, - reader())); - } - - if (source == butSin) { - writer(calc.calculateMono(Calculator.MonoOperatorModes.sin, - reader())); - } - - if (source == butTan) { - writer(calc.calculateMono(Calculator.MonoOperatorModes.tan, - reader())); - } - if (source == butlog) { - writer(calc.calculateMono(Calculator.MonoOperatorModes.log, - reader())); - } - if (source == butrate) { - writer(calc.calculateMono(Calculator.MonoOperatorModes.rate, - reader())); - } - if(source == butabs){ - writer(calc.calculateMono(Calculator.MonoOperatorModes.abs, reader())); + + private final JFrame frame; + + private final JPanel panel; + private final JPanel panelSub1; + private final JPanel panelSub2; + private final JPanel panelSub3; + private final JPanel panelSub4; + private final JPanel panelSub5; + private final JPanel panelSub6; + private final JPanel panelSub7; + private final JPanel panelSub8; + + private final JTextArea text; + private final JButton but[], butAdd, butMinus, butMultiply, butDivide, + butEqual, butCancel, butSquareRoot, butSquare, butOneDevidedBy, + butCos, butSin, butTan, butxpowerofy, butlog, butrate, butabs, butBinary; + private final Calculator calc; + + private final String[] buttonValue = {"0", "1", "2", "3", "4", "5", "6", + "7", "8", "9"}; + + private final Font font; + private final Font textFont; + ImageIcon image; + + public UI() { + frame = new JFrame("Calculator PH"); + + image = new ImageIcon("icon/icon.png"); + + panel = new JPanel(); + panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); + panelSub1 = new JPanel(new FlowLayout()); + panelSub2 = new JPanel(new FlowLayout()); + panelSub3 = new JPanel(new FlowLayout()); + panelSub4 = new JPanel(new FlowLayout()); + panelSub5 = new JPanel(new FlowLayout()); + panelSub6 = new JPanel(new FlowLayout()); + panelSub7 = new JPanel(new FlowLayout()); + panelSub8 = new JPanel(new FlowLayout()); + + font = new Font("Consolas",Font.PLAIN, 18); + + text = new JTextArea(1, 30); + + textFont = new Font("Consolas",Font.BOLD, 24); + + but = new JButton[10]; + for (int i = 0; i < 10; i++) { + but[i] = new JButton(String.valueOf(i)); + } + butAdd = new JButton("+"); + butMinus = new JButton("-"); + butMultiply = new JButton("*"); + butDivide = new JButton("/"); + butEqual = new JButton("="); + butSquareRoot = new JButton("sqrt"); + butSquare = new JButton("x*x"); + butOneDevidedBy = new JButton("1/x"); + butCos = new JButton("Cos"); + butSin = new JButton("Sin"); + butTan = new JButton("Tan"); + butxpowerofy = new JButton("x^y"); + butlog = new JButton("log10(x)"); + butrate = new JButton("x%"); + butabs = new JButton("abs(x)"); + butCancel = new JButton("C"); + butBinary = new JButton("Bin"); + + calc = new Calculator(); + + } + + public void init() { + frame.setSize(450, 450); + frame.setVisible(true); + frame.setLocationRelativeTo(null); + frame.setResizable(false); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setIconImage(image.getImage()); + + text.setFont(textFont); + text.setEditable(false); + + for (int i = 0; i < 10; i++) { + but[i].setFont(font); + } + butAdd.setFont(font); + butMinus.setFont(font); + butMultiply.setFont(font); + butDivide.setFont(font); + butEqual.setFont(font); + butSquareRoot.setFont(font); + butSquare.setFont(font); + butOneDevidedBy.setFont(font); + butCos.setFont(font); + butSin.setFont(font); + butTan.setFont(font); + butxpowerofy.setFont(font); + butlog.setFont(font); + butrate.setFont(font); + butabs.setFont(font); + butCancel.setFont(font); + butBinary.setFont(font); + + panel.add(Box.createHorizontalStrut(100)); + panelSub1.add(text); + panel.add(panelSub1); + + panelSub2.add(but[1]); + panelSub2.add(but[2]); + panelSub2.add(but[3]); + panelSub2.add(Box.createHorizontalStrut(15)); + panelSub2.add(butAdd); + panelSub2.add(butMinus); + panel.add(panelSub2); + + panelSub3.add(but[4]); + panelSub3.add(but[5]); + panelSub3.add(but[6]); + panelSub3.add(Box.createHorizontalStrut(15)); + panelSub3.add(butMultiply); + panelSub3.add(butDivide); + panel.add(panelSub3); + + panelSub4.add(but[7]); + panelSub4.add(but[8]); + panelSub4.add(but[9]); + panelSub4.add(Box.createHorizontalStrut(15)); + panelSub4.add(butEqual); + panelSub4.add(butCancel); + panel.add(panelSub4); + + panelSub5.add(but[0]); + panelSub5.add(Box.createHorizontalStrut(210)); + panel.add(panelSub5); + + panelSub6.add(butSquare); + panelSub6.add(butSquareRoot); + panelSub6.add(butOneDevidedBy); + panelSub6.add(butxpowerofy); + panel.add(panelSub6); + + panelSub7.add(butCos); + panelSub7.add(butSin); + panelSub7.add(butTan); + panel.add(panelSub7); + + panelSub8.add(butlog); + panelSub8.add(butrate); + panelSub8.add(butabs); + panelSub8.add(butBinary); + panel.add(panelSub8); + + but[0].addActionListener(this); + for (int i = 1; i < 10; i++) { + but[i].addActionListener(this); + } + butAdd.addActionListener(this); + butMinus.addActionListener(this); + butMultiply.addActionListener(this); + butDivide.addActionListener(this); + butSquare.addActionListener(this); + butSquareRoot.addActionListener(this); + butOneDevidedBy.addActionListener(this); + butCos.addActionListener(this); + butSin.addActionListener(this); + butTan.addActionListener(this); + butxpowerofy.addActionListener(this); + butlog.addActionListener(this); + butrate.addActionListener(this); + butabs.addActionListener(this); + butBinary.addActionListener(this); + + butEqual.addActionListener(this); + butCancel.addActionListener(this); + + frame.add(panel); + } + + @Override + public void actionPerformed(ActionEvent e) { + final Object source = e.getSource(); + + for (int i = 0; i < 10; i++) { + if (source == but[i]) { + text.replaceSelection(buttonValue[i]); + return; } - - if (source == butEqual) { - writer(calc.calculateEqual(reader())); - } - - if (source == butCancel) { - writer(calc.reset()); - } - - if (source == butBinary) { - parsetoBinary(); - } - - text.selectAll(); - } - - private void parsetoBinary() { - try { - text.setText("" + Long.toBinaryString(Long.parseLong(text.getText()))); - } catch (NumberFormatException ex) { - System.err.println("Error while parse to binary." + ex.toString()); - } - } - - public Double reader() { - Double num; - String str; - str = text.getText(); - num = Double.valueOf(str); - - return num; - } - - public void writer(final Double num) { - if (Double.isNaN(num)) { - text.setText(""); - } else { - text.setText(Double.toString(num)); - } - } + } + + if (source == butAdd) { + writer(calc.calculateBi(Calculator.BiOperatorModes.add, reader())); + } + + if (source == butMinus) { + writer(calc.calculateBi(Calculator.BiOperatorModes.minus, reader())); + } + + if (source == butMultiply) { + writer(calc.calculateBi(Calculator.BiOperatorModes.multiply, + reader())); + } + + if (source == butDivide) { + writer(calc + .calculateBi(Calculator.BiOperatorModes.divide, reader())); + } + if (source == butxpowerofy) { + writer(calc + .calculateBi(Calculator.BiOperatorModes.xpowerofy, reader())); + } + + if (source == butSquare) { + writer(calc.calculateMono(Calculator.MonoOperatorModes.square, + reader())); + } + + if (source == butSquareRoot) { + writer(calc.calculateMono(Calculator.MonoOperatorModes.squareRoot, + reader())); + } + + if (source == butOneDevidedBy) { + writer(calc.calculateMono( + Calculator.MonoOperatorModes.oneDevidedBy, reader())); + } + + if (source == butCos) { + writer(calc.calculateMono(Calculator.MonoOperatorModes.cos, + reader())); + } + + if (source == butSin) { + writer(calc.calculateMono(Calculator.MonoOperatorModes.sin, + reader())); + } + + if (source == butTan) { + writer(calc.calculateMono(Calculator.MonoOperatorModes.tan, + reader())); + } + if (source == butlog) { + writer(calc.calculateMono(Calculator.MonoOperatorModes.log, + reader())); + } + if (source == butrate) { + writer(calc.calculateMono(Calculator.MonoOperatorModes.rate, + reader())); + } + if(source == butabs){ + writer(calc.calculateMono(Calculator.MonoOperatorModes.abs, reader())); + } + + if (source == butEqual) { + writer(calc.calculateEqual(reader())); + } + + if (source == butCancel) { + writer(calc.reset()); + } + + if (source == butBinary) { + parsetoBinary(); + } + + text.selectAll(); + } + + private void parsetoBinary() { + try { + text.setText("" + Long.toBinaryString(Long.parseLong(text.getText()))); + } catch (NumberFormatException ex) { + System.err.println("Error while parse to binary." + ex.toString()); + } + } + + public Double reader() { + Double num; + String str; + str = text.getText(); + num = Double.valueOf(str); + + return num; + } + + public void writer(final Double num) { + if (Double.isNaN(num)) { + text.setText(""); + } else { + text.setText(Double.toString(num)); + } + } }