Browse Source

Updates

master
Micah Halter 4 years ago
parent
commit
dd5422cdc4
17 changed files with 328 additions and 198 deletions
  1. BIN
      Calculator-v1.1.jar
  2. +3
    -2
      src/main/java/com/mehalter/calculator/Calculator.java
  3. +1
    -1
      src/main/java/com/mehalter/calculator/CalculatorRunner.java
  4. +12
    -12
      src/main/java/com/mehalter/calculator/model/CalcState.java
  5. +58
    -31
      src/main/java/com/mehalter/calculator/model/TextFields.java
  6. +14
    -17
      src/main/java/com/mehalter/calculator/model/Utils.java
  7. +13
    -5
      src/main/java/com/mehalter/calculator/stack/EmptyStackException.java
  8. +16
    -7
      src/main/java/com/mehalter/calculator/stack/IStack.java
  9. +13
    -15
      src/main/java/com/mehalter/calculator/stack/LStack.java
  10. +1
    -1
      src/main/java/com/mehalter/calculator/ui/BottomPanel.java
  11. +16
    -10
      src/main/java/com/mehalter/calculator/ui/ConstantsPanel.java
  12. +19
    -11
      src/main/java/com/mehalter/calculator/ui/LeftPanel.java
  13. +14
    -10
      src/main/java/com/mehalter/calculator/ui/NumPanel.java
  14. +59
    -25
      src/main/java/com/mehalter/calculator/ui/OpPanel.java
  15. +21
    -9
      src/main/java/com/mehalter/calculator/ui/SimpleButton.java
  16. +2
    -2
      src/main/java/com/mehalter/calculator/ui/StackPanel.java
  17. +66
    -40
      src/main/java/com/mehalter/calculator/ui/TrigPanel.java

BIN
Calculator-v1.1.jar View File


+ 3
- 2
src/main/java/com/mehalter/calculator/Calculator.java View File

@@ -42,14 +42,15 @@ public class Calculator extends JFrame implements Runnable {
stacks.setKeyboardShortcuts(opPanel.getKeys(), leftPanel.getKeys());
addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent e) {
stacks.getFocus();
stacks.getFocus();
}
});
getContentPane().add(new NumPanel(stacks, utils), BorderLayout.CENTER);
getContentPane().add(opPanel, BorderLayout.EAST);
getContentPane().add(leftPanel, BorderLayout.WEST);
getContentPane().add(new StackPanel(stacks), BorderLayout.NORTH);
getContentPane().add(new BottomPanel(stacks, utils, state), BorderLayout.SOUTH);
getContentPane().add(new BottomPanel(stacks, utils, state),
BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}


+ 1
- 1
src/main/java/com/mehalter/calculator/CalculatorRunner.java View File

@@ -5,5 +5,5 @@ public class CalculatorRunner {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Calculator());
}
}

+ 12
- 12
src/main/java/com/mehalter/calculator/model/CalcState.java View File

@@ -9,38 +9,38 @@ public class CalcState {
private boolean edit;
private boolean oped;
private boolean rad;
public CalcState() {
stack = new LStack<Double>();
edit = false;
oped = false;
rad = true;
}
public IStack<Double> getStack() {
return stack;
}
public int stackSize() {
return stack.size();
}
public Double popStack() {
return stack.pop();
}
public Double peekStack() {
return stack.peek();
}
public void clearStack() {
stack.clear();
}
public void pushStack(Double i) {
stack.push(i);
}
public boolean getEdit() {
return edit;
}
@@ -48,19 +48,19 @@ public class CalcState {
public boolean getOped() {
return oped;
}
public void setEdit(boolean edit) {
this.edit = edit;
}
public void setOped(boolean oped) {
this.oped = oped;
}
public boolean getRad() {
return rad;
}
public void setRad(boolean rad) {
this.rad = rad;
}


+ 58
- 31
src/main/java/com/mehalter/calculator/model/TextFields.java View File

@@ -12,58 +12,84 @@ import javax.swing.text.JTextComponent;

public class TextFields {
private JTextField[] stacks;
public TextFields(int i) {
stacks = new JTextField[i];
for (int k = 0; k < i; k++) {
stacks[k] = createTextField();
}
}
}
public void setLastListener(Utils utils) {
int i = stacks.length;
stacks[i - 1].setEditable(true);
stacks[i - 1].setBackground(Color.LIGHT_GRAY);
stacks[i - 1].addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
public void keyTyped(KeyEvent e) {
if (!Utils.validTyped(e.getKeyChar()))
e.consume();
e.consume();
else {
utils.ifEditifOp();
if (e.getKeyChar() == '-') {
if (((JTextComponent) e.getSource()).getText().length() != 0)
e.consume();
else if (((JTextComponent) e.getSource()).getText().length() == 1 &&
((JTextComponent) e.getSource()).getText() == "-")
else if (((JTextComponent) e.getSource()).getText()
.length() == 1
&& ((JTextComponent) e.getSource()).getText() == "-")
e.consume();
}
else if (e.getKeyChar() == '.')
if (((JTextComponent) e.getSource()).getText().contains("."))
} else if (e.getKeyChar() == '.')
if (((JTextComponent) e.getSource()).getText()
.contains("."))
e.consume();
}
}
});
}
public void setKeyboardShortcuts(JButton[] opKeys, JButton[] leftKeys) {
stacks[stacks.length - 1].addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
public void keyTyped(KeyEvent e) {
switch (e.getKeyChar()) {
case '-': if (((JTextComponent) e.getSource()).getText().length() != 0) opKeys[11].doClick(); break;
case '+': opKeys[10].doClick(); break;
case '*': opKeys[7].doClick(); break;
case '/': opKeys[8].doClick(); break;
case 'e': opKeys[6].doClick(); break;
case '^': opKeys[4].doClick(); break;
case 'r': leftKeys[0].doClick(); break;
case 's': leftKeys[1].doClick(); break;
case 'd': leftKeys[2].doClick(); break;
case 'a': leftKeys[3].doClick(); break;
case 'c': leftKeys[4].doClick(); break;
case 'q': System.exit(0); break;
case '-':
if (((JTextComponent) e.getSource()).getText().length() != 0)
opKeys[11].doClick();
break;
case '+':
opKeys[10].doClick();
break;
case '*':
opKeys[7].doClick();
break;
case '/':
opKeys[8].doClick();
break;
case 'e':
opKeys[6].doClick();
break;
case '^':
opKeys[4].doClick();
break;
case 'r':
leftKeys[0].doClick();
break;
case 's':
leftKeys[1].doClick();
break;
case 'd':
leftKeys[2].doClick();
break;
case 'a':
leftKeys[3].doClick();
break;
case 'c':
leftKeys[4].doClick();
break;
case 'q':
System.exit(0);
break;
}
}
}
});
}

@@ -76,19 +102,20 @@ public class TextFields {
return text;

}
public void setText(int i, String x) {
stacks[i].setText(x);;
stacks[i].setText(x);
;
}
public void getFocus() {
stacks[stacks.length - 1].requestFocus();
}
public String get(int i) {
return stacks[i].getText();
}
public JTextField getField(int i) {
return stacks[i];
}


+ 14
- 17
src/main/java/com/mehalter/calculator/model/Utils.java View File

@@ -3,29 +3,30 @@ package com.mehalter.calculator.model;
import java.awt.event.KeyEvent;

public class Utils {
private CalcState state;
private TextFields stacks;
public Utils(CalcState state, TextFields stacks) {
this.state = state;
this.stacks = stacks;
}
public void refreshStack() {
int m = 0;
for (Double i: state.getStack()) {
for (Double i : state.getStack()) {
if (m < 4) {
stacks.setText(3 - m, i.toString());
m++;
} else break;
} else
break;
}
for (int i = 3-state.stackSize(); i >= 0; i-=1)
for (int i = 3 - state.stackSize(); i >= 0; i -= 1)
stacks.setText(i, "");
state.setEdit(true);

}
public void ifEditifOp() {

if (state.getEdit()) {
@@ -38,18 +39,14 @@ public class Utils {
}

}
public static boolean validTyped(char e) {
return (Character.isDigit(e) ||
(e == KeyEvent.VK_BACK_SPACE) ||
(e == KeyEvent.VK_DELETE) ||
(e == '.') ||
(e == '-'));
return (Character.isDigit(e) || (e == KeyEvent.VK_BACK_SPACE)
|| (e == KeyEvent.VK_DELETE) || (e == '.') || (e == '-'));
}

public static boolean validEnter(String text) {
return (text.length() == 1 &&
(text.contains("-") || text.contains("."))) ||
(text.length() == 2 && text.contains("-."));
return (text.length() == 1 && (text.contains("-") || text.contains(".")))
|| (text.length() == 2 && text.contains("-."));
}
}

+ 13
- 5
src/main/java/com/mehalter/calculator/stack/EmptyStackException.java View File

@@ -2,13 +2,21 @@ package com.mehalter.calculator.stack;

@SuppressWarnings("serial")
public class EmptyStackException extends RuntimeException {
public EmptyStackException() { super(); }

public EmptyStackException(String s) { super(s); }
public EmptyStackException() {
super();
}

public EmptyStackException(String s, Throwable t) { super(s, t); }
public EmptyStackException(String s) {
super(s);
}

public EmptyStackException(Throwable t) { super(t); }
public EmptyStackException(String s, Throwable t) {
super(s, t);
}

public EmptyStackException(Throwable t) {
super(t);
}

}

+ 16
- 7
src/main/java/com/mehalter/calculator/stack/IStack.java View File

@@ -1,10 +1,12 @@
package com.mehalter.calculator.stack;

import java.util.Iterator;

public interface IStack<E> extends Iterable<E> {

/**
* This will tell if the stack is empty
*
* @return true if the stack is empty and false otherwise
*/
public boolean isEmpty();
@@ -16,32 +18,39 @@ public interface IStack<E> extends Iterable<E> {

/**
* This puts <code>newItem</code> on the stack
* @param newItem the new object we are placing on the stack
*
* @param newItem
* the new object we are placing on the stack
*/
public void push(E newItem);

/**
* This removes and returns the item on the top of the stack
*
* @return item on the top of the stack
* @throws an <code>EmpyStackException</code> if you try to
* pop from an empty stack.
* @throws an
* <code>EmpyStackException</code> if you try to pop from an
* empty stack.
*/
public E pop();
/**
* This returns the item on the top of the stack
*
* @return item on the top of the stack
* @throws an <code>EmpyStackException</code> if you try to
* peek from an empty stack.
* @throws an
* <code>EmpyStackException</code> if you try to peek from an
* empty stack.
*/
public E peek();

/**
* This will let the user know how many elements are in the stack
*
* @return an int representing the stack's size.
*/
public int size();
public Iterator<E> iterator();

}

+ 13
- 15
src/main/java/com/mehalter/calculator/stack/LStack.java View File

@@ -4,13 +4,13 @@ import java.util.Iterator;
import java.util.NoSuchElementException;

public class LStack<E> implements IStack<E> {
private Link<E> top;

public LStack() {
top = null;
}
@Override
public String toString() {
if (isEmpty())
@@ -18,7 +18,7 @@ public class LStack<E> implements IStack<E> {
Link<E> nav = top;
StringBuffer out = new StringBuffer();
out.append("[");
while(nav.getSize() > 1) {
while (nav.getSize() > 1) {
out.append(nav.getE());
out.append(", ");
nav = nav.getLink();
@@ -26,11 +26,11 @@ public class LStack<E> implements IStack<E> {
out.append(nav.getE() + "]");
return out.toString();
}
public int size() {
return isEmpty() ? 0 : top.getSize();
}
public boolean isEmpty() {
return top == null;
}
@@ -63,12 +63,11 @@ public class LStack<E> implements IStack<E> {
}

}

class LTraveler<E> implements Iterator<E> {
private Link<E> iTop;
public LTraveler(Link<E> iTop) {
this.iTop = iTop;
}
@@ -86,35 +85,34 @@ class LTraveler<E> implements Iterator<E> {
iTop = iTop.getLink();
return out;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}

class Link<E> {

private final E item;
private final Link<E> next;
private final int size;
public Link(E item, Link<E> next) {
this.item = item;
this.next = next;
this.size = next == null ? 1 : next.getSize() + 1;
}
public E getE() {
return this.item;
}
public Link<E> getLink() {
return this.next;
}
public int getSize() {
return this.size;
}


+ 1
- 1
src/main/java/com/mehalter/calculator/ui/BottomPanel.java View File

@@ -9,7 +9,7 @@ import com.mehalter.calculator.model.TextFields;
import com.mehalter.calculator.model.Utils;

public class BottomPanel extends JPanel {
private static final long serialVersionUID = 5087704112598170173L;

public BottomPanel(TextFields stacks, Utils utils, CalcState state) {


+ 16
- 10
src/main/java/com/mehalter/calculator/ui/ConstantsPanel.java View File

@@ -14,29 +14,34 @@ import com.mehalter.calculator.model.TextFields;
import com.mehalter.calculator.model.Utils;

public class ConstantsPanel extends JPanel {
private static final long serialVersionUID = 8150202400626370885L;
private TextFields stacks;
private Utils utils;

public ConstantsPanel(TextFields stacks, CalcState state, Utils utils) {
this.stacks = stacks;
this.utils = utils;
setLayout(new GridLayout(1,5));
setLayout(new GridLayout(1, 5));
setBackground(Color.WHITE);
setPreferredSize(new Dimension(0,50));
setPreferredSize(new Dimension(0, 50));
JButton[] knownKeys = new JButton[5];
knownKeys[0] = new SimpleButton("\u03C0");
knownKeys[0].addActionListener(new constantListener(knownKeys[0], Double.toString(Math.PI)));
knownKeys[0].addActionListener(new constantListener(knownKeys[0],
Double.toString(Math.PI)));
knownKeys[1] = new SimpleButton("\u2107");
knownKeys[1].addActionListener(new constantListener(knownKeys[1], Double.toString(Math.E)));
knownKeys[1].addActionListener(new constantListener(knownKeys[1],
Double.toString(Math.E)));
knownKeys[2] = new SimpleButton("sin");
knownKeys[2].addActionListener(new trigListener(knownKeys[2], stacks, utils, state));
knownKeys[2].addActionListener(new trigListener(knownKeys[2], stacks,
utils, state));
knownKeys[3] = new SimpleButton("cos");
knownKeys[3].addActionListener(new trigListener(knownKeys[3], stacks, utils, state));
knownKeys[3].addActionListener(new trigListener(knownKeys[3], stacks,
utils, state));
knownKeys[4] = new SimpleButton("tan");
knownKeys[4].addActionListener(new trigListener(knownKeys[4], stacks, utils, state));
knownKeys[4].addActionListener(new trigListener(knownKeys[4], stacks,
utils, state));
for (int k = 0; k < knownKeys.length; k++)
add(knownKeys[k]);
}
@@ -49,8 +54,9 @@ public class ConstantsPanel extends JPanel {
public constantListener(JButton key, String constant) {
this.key = key;
this.constant = constant;
}

public void actionPerformed(ActionEvent e) {
utils.ifEditifOp();
stacks.setText(4, constant);


+ 19
- 11
src/main/java/com/mehalter/calculator/ui/LeftPanel.java View File

@@ -14,15 +14,15 @@ import com.mehalter.calculator.model.TextFields;
import com.mehalter.calculator.model.Utils;

public class LeftPanel extends JPanel {
private static final long serialVersionUID = 6304604086038359864L;
private JButton[] leftKeys;

public LeftPanel(CalcState state, Utils utils, TextFields stacks) {
setLayout(new GridLayout(6,1));
setLayout(new GridLayout(6, 1));
setBackground(Color.WHITE);
setPreferredSize(new Dimension(100,0));
setPreferredSize(new Dimension(100, 0));
leftKeys = new JButton[6];
leftKeys[0] = new SimpleButton("roll");
leftKeys[0].addActionListener(new ActionListener() {
@@ -35,11 +35,12 @@ public class LeftPanel extends JPanel {
state.pushStack(Double.parseDouble(stacks.get(4)));
Double[] oldStack = new Double[state.stackSize()];
int m = 0;
for (double i: state.getStack()) {
for (double i : state.getStack()) {
oldStack[m] = i;
m++;
}
state.clearStack();;
state.clearStack();
;
state.pushStack(oldStack[0]);
for (int i = oldStack.length - 1; i > 0; i--)
state.pushStack(oldStack[i]);
@@ -68,7 +69,8 @@ public class LeftPanel extends JPanel {
public void actionPerformed(ActionEvent e) {
state.setEdit(false);
state.setOped(false);
stacks.setText(4, state.stackSize() == 0 ? "" : state.popStack().toString());
stacks.setText(4, state.stackSize() == 0 ? "" : state
.popStack().toString());
utils.refreshStack();
}
});
@@ -96,15 +98,21 @@ public class LeftPanel extends JPanel {
state.setEdit(false);
state.setOped(false);
if (stacks.get(4).length() == 2 && stacks.get(4).contains("-"))
stacks.setText(4, stacks.get(4).substring(0, stacks.get(4).length()-2));
stacks.setText(
4,
stacks.get(4).substring(0,
stacks.get(4).length() - 2));
else if (stacks.get(4).length() > 0)
stacks.setText(4, stacks.get(4).substring(0, stacks.get(4).length()-1));
stacks.setText(
4,
stacks.get(4).substring(0,
stacks.get(4).length() - 1));
}
});
for(int k = 0; k < leftKeys.length; k++)
for (int k = 0; k < leftKeys.length; k++)
add(leftKeys[k]);
}
public JButton[] getKeys() {
return leftKeys;
}


+ 14
- 10
src/main/java/com/mehalter/calculator/ui/NumPanel.java View File

@@ -14,21 +14,21 @@ import com.mehalter.calculator.model.Utils;
public class NumPanel extends JPanel {

private static final long serialVersionUID = 7601340040994175016L;
private TextFields stacks;
private Utils utils;
public NumPanel(TextFields stacks, Utils utils) {
this.stacks = stacks;
this.utils = utils;
Color numColor = new Color(-1118482);

setLayout(new GridLayout(4,3));
setLayout(new GridLayout(4, 3));
setBackground(Color.WHITE);
JButton[][] numKeys = new JButton[4][3];
for (int k = 2; k >= 0; k--) {
for (int l = 0; l < 3; l++) {
numKeys[k][l] = new SimpleButton("" + (3*k + l + 1), numColor);
numKeys[k][l] = new SimpleButton("" + (3 * k + l + 1), numColor);
numKeys[k][l].addActionListener(new numListener(numKeys[k][l]));
add(numKeys[k][l]);
}
@@ -37,19 +37,22 @@ public class NumPanel extends JPanel {
numKeys[3][0].addActionListener(new numListener(numKeys[3][0]));
add(numKeys[3][0]);
numKeys[3][1] = new SimpleButton(".", numColor);
numKeys[3][1].addActionListener(e -> {
if (!stacks.get(4).contains("."))
stacks.setText(4, (stacks.get(4).equals("")) ? "0." : stacks.get(4) + ".");
});
numKeys[3][1]
.addActionListener(e -> {
if (!stacks.get(4).contains("."))
stacks.setText(4, (stacks.get(4).equals("")) ? "0."
: stacks.get(4) + ".");
});
add(numKeys[3][1]);
numKeys[3][2] = new SimpleButton("-", numColor);
numKeys[3][2].addActionListener(e -> {
if (stacks.get(4).length() != 0)
stacks.setText(4, (stacks.get(4).charAt(0) == '-') ? stacks.get(4).substring(1) : ('-' + stacks.get(4)));
stacks.setText(4, (stacks.get(4).charAt(0) == '-') ? stacks
.get(4).substring(1) : ('-' + stacks.get(4)));
});
add(numKeys[3][2]);
}
class numListener implements ActionListener {

JButton key;
@@ -57,6 +60,7 @@ public class NumPanel extends JPanel {
public numListener(JButton _key) {
key = _key;
}

public void actionPerformed(ActionEvent e) {
utils.ifEditifOp();
stacks.setText(4, stacks.get(4) + key.getText());


+ 59
- 25
src/main/java/com/mehalter/calculator/ui/OpPanel.java View File

@@ -16,19 +16,19 @@ import com.mehalter.calculator.model.Utils;
public class OpPanel extends JPanel {

private static final long serialVersionUID = 5888535726118050131L;
private CalcState state;
private Utils utils;
private TextFields stacks;
private JButton[] opKeys;
public OpPanel(CalcState state, Utils utils, TextFields stacks) {
this.state = state;
this.utils = utils;
this.stacks = stacks;
setLayout(new GridLayout(4,3));
setLayout(new GridLayout(4, 3));
setBackground(Color.WHITE);
setPreferredSize(new Dimension(265,0));
setPreferredSize(new Dimension(265, 0));
opKeys = new JButton[12];
opKeys[0] = new SimpleButton("<html>log<sub>y</sub></html>");
opKeys[0].addActionListener(new opListener(opKeys[0]));
@@ -43,14 +43,14 @@ public class OpPanel extends JPanel {
opKeys[5] = new SimpleButton("<html>e<sup>x</sup></html>");
opKeys[5].addActionListener(new opListener(opKeys[5]));
opKeys[6] = new SimpleButton("E");
opKeys[6].addActionListener(e -> {
opKeys[6].addActionListener(e -> {
if (!stacks.get(4).contains("E") && !stacks.get(4).isEmpty()) {
String editText = stacks.get(4);
utils.ifEditifOp();
stacks.setText(4, editText + "E");
}
});
opKeys[7] = new SimpleButton("*");
opKeys[7] = new SimpleButton("*");
opKeys[7].addActionListener(new opListener(opKeys[7]));
opKeys[8] = new SimpleButton("/");
opKeys[8].addActionListener(new opListener(opKeys[8]));
@@ -62,7 +62,7 @@ public class OpPanel extends JPanel {
state.pushStack(new Double(0));
stacks.setText(4, "0");
} else if (Utils.validEnter(stacks.get(4)))
stacks.setText(4, "");
stacks.setText(4, "");
else
state.pushStack(Double.parseDouble(stacks.get(4)));
for (int i = 0; i < 3; i++) {
@@ -82,14 +82,14 @@ public class OpPanel extends JPanel {
opKeys[10].addActionListener(new opListener(opKeys[10]));
opKeys[11] = new SimpleButton("-");
opKeys[11].addActionListener(new opListener(opKeys[11]));
for(int k = 0; k < opKeys.length; k++)
for (int k = 0; k < opKeys.length; k++)
add(opKeys[k]);
}
public JButton[] getKeys() {
return opKeys;
}
class opListener implements ActionListener {

JButton click;
@@ -108,32 +108,66 @@ public class OpPanel extends JPanel {
double x = state.popStack();
double y = state.popStack();
switch (click.getText()) {
case "+": ans = y + x; break;
case "-": ans = y - x; break;
case "*": ans = y * x; break;
case "/": ans = y / x; break;
case "<html>y<sup>x</sup></html>": ans = Math.pow(y, x); break;
case "<html>e<sup>x</sup></html>": ans = Math.pow(Math.E, x); state.pushStack(y); break;
case "\u2713" + "x": ans = Math.sqrt(x); state.pushStack(y); break;
case "<html>log<sub>y</sub></html>": ans = Math.log(x) / Math.log(y); break;
case "log": ans = Math.log10(x); state.pushStack(y); break;
case "ln": ans = Math.log(x); state.pushStack(y); break;
case "+":
ans = y + x;
break;
case "-":
ans = y - x;
break;
case "*":
ans = y * x;
break;
case "/":
ans = y / x;
break;
case "<html>y<sup>x</sup></html>":
ans = Math.pow(y, x);
break;
case "<html>e<sup>x</sup></html>":
ans = Math.pow(Math.E, x);
state.pushStack(y);
break;
case "\u2713" + "x":
ans = Math.sqrt(x);
state.pushStack(y);
break;
case "<html>log<sub>y</sub></html>":
ans = Math.log(x) / Math.log(y);
break;
case "log":
ans = Math.log10(x);
state.pushStack(y);
break;
case "ln":
ans = Math.log(x);
state.pushStack(y);
break;
}
stacks.setText(4, ans.toString());
utils.refreshStack();
state.setOped(true);
state.setEdit(true);
} else {
if (click.getText() == "\u2713" + "x" || click.getText() == "log" || click.getText() == "ln" || click.getText() == "<html>e<sup>x</sup></html>"
if (click.getText() == "\u2713" + "x"
|| click.getText() == "log" || click.getText() == "ln"
|| click.getText() == "<html>e<sup>x</sup></html>"
&& !stacks.get(4).endsWith("E")) {
if (stacks.get(4).equals(""))
stacks.setText(4, "0");
state.pushStack(Double.parseDouble(stacks.get(4)));
switch (click.getText()) {
case "\u2713" + "x": ans = Math.sqrt(state.popStack()); break;
case "log": ans = Math.log10(state.popStack()); break;
case "ln": ans = Math.log(state.popStack()); break;
case "<html>e<sup>x</sup></html>": ans = Math.pow(Math.E, state.popStack()); break;
case "\u2713" + "x":
ans = Math.sqrt(state.popStack());
break;
case "log":
ans = Math.log10(state.popStack());
break;
case "ln":
ans = Math.log(state.popStack());
break;
case "<html>e<sup>x</sup></html>":
ans = Math.pow(Math.E, state.popStack());
break;
}
stacks.setText(4, ans.toString());
utils.refreshStack();


+ 21
- 9
src/main/java/com/mehalter/calculator/ui/SimpleButton.java View File

@@ -11,7 +11,7 @@ import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class SimpleButton extends JButton {
private static final long serialVersionUID = 8140722804416108071L;

public SimpleButton(String text, Color color) {
@@ -21,17 +21,29 @@ public class SimpleButton extends JButton {
setBackground(color);

addMouseListener(new MouseListener() {
public void mousePressed(MouseEvent e) { setBackground(color.darker()); }
public void mouseReleased(MouseEvent e) { setBackground(color); }
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) {
setBackground(color.darker());
}

public void mouseReleased(MouseEvent e) {
setBackground(color);
}

public void mouseClicked(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}
});
setBorder(new CompoundBorder(new LineBorder(Color.WHITE, 2), new EmptyBorder(50, 20, 50, 20)));

setBorder(new CompoundBorder(new LineBorder(Color.WHITE, 2),
new EmptyBorder(50, 20, 50, 20)));
setFont(new Font("Helvetica", Font.PLAIN, 20));
}
public SimpleButton(String text) {
this(text, Color.LIGHT_GRAY);
}


+ 2
- 2
src/main/java/com/mehalter/calculator/ui/StackPanel.java View File

@@ -8,11 +8,11 @@ import javax.swing.JPanel;
import com.mehalter.calculator.model.TextFields;

public class StackPanel extends JPanel {
private static final long serialVersionUID = -8261785989286539516L;

public StackPanel(TextFields stacks) {
setLayout(new GridLayout(5,1));
setLayout(new GridLayout(5, 1));
setBackground(Color.WHITE);
for (int i = 0; i < 5; i++) {
add(stacks.getField(i));


+ 66
- 40
src/main/java/com/mehalter/calculator/ui/TrigPanel.java View File

@@ -14,26 +14,32 @@ import com.mehalter.calculator.model.TextFields;
import com.mehalter.calculator.model.Utils;

public class TrigPanel extends JPanel {
private static final long serialVersionUID = 8150202400626370885L;
public TrigPanel(TextFields stacks, CalcState state, Utils utils) {
setLayout(new GridLayout(1,7));
setLayout(new GridLayout(1, 7));
setBackground(Color.WHITE);
setPreferredSize(new Dimension(0,50));
setPreferredSize(new Dimension(0, 50));
JButton[] knownKeys = new JButton[7];
knownKeys[0] = new SimpleButton("sec");
knownKeys[0].addActionListener(new trigListener(knownKeys[0], stacks, utils, state));
knownKeys[0].addActionListener(new trigListener(knownKeys[0], stacks,
utils, state));
knownKeys[1] = new SimpleButton("csc");
knownKeys[1].addActionListener(new trigListener(knownKeys[1], stacks, utils, state));
knownKeys[1].addActionListener(new trigListener(knownKeys[1], stacks,
utils, state));
knownKeys[2] = new SimpleButton("cot");
knownKeys[2].addActionListener(new trigListener(knownKeys[2], stacks, utils, state));
knownKeys[2].addActionListener(new trigListener(knownKeys[2], stacks,
utils, state));
knownKeys[3] = new SimpleButton("asin");
knownKeys[3].addActionListener(new trigListener(knownKeys[3], stacks, utils, state));
knownKeys[3].addActionListener(new trigListener(knownKeys[3], stacks,
utils, state));
knownKeys[4] = new SimpleButton("acos");
knownKeys[4].addActionListener(new trigListener(knownKeys[4], stacks, utils, state));
knownKeys[4].addActionListener(new trigListener(knownKeys[4], stacks,
utils, state));
knownKeys[5] = new SimpleButton("atan");
knownKeys[5].addActionListener(new trigListener(knownKeys[5], stacks, utils, state));
knownKeys[5].addActionListener(new trigListener(knownKeys[5], stacks,
utils, state));
knownKeys[6] = new SimpleButton(state.getRad() ? "rad" : "deg");
knownKeys[6].addActionListener(e -> {
state.setRad(!state.getRad());
@@ -44,39 +50,59 @@ public class TrigPanel extends JPanel {
}
}

class trigListener implements ActionListener {
class trigListener implements ActionListener {

private JButton key;
private TextFields stacks;
private Utils utils;
private CalcState state;
private JButton key;
private TextFields stacks;
private Utils utils;
private CalcState state;

public trigListener(JButton key, TextFields stacks, Utils utils, CalcState state) {
this.key = key;
this.stacks = stacks;
this.utils = utils;
this.state = state;
}
public trigListener(JButton key, TextFields stacks, Utils utils,
CalcState state) {
this.key = key;
this.stacks = stacks;
this.utils = utils;
this.state = state;
}

public void actionPerformed(ActionEvent e) {
if (!stacks.get(4).endsWith("E")) {
Double x = Double.parseDouble(stacks.get(4));
switch (key.getText()) {
case "sin": x = Math.sin(x); break;
case "cos": x = Math.cos(x); break;
case "tan": x = Math.tan(x); break;
case "sec": x = 1.0 / Math.cos(x); break;
case "csc": x = 1.0 / Math.sin(x); break;
case "cot": x = 1.0 / Math.tan(x); break;
case "asin": x = Math.asin(x); break;
case "acos": x = Math.acos(x); break;
case "atan": x = Math.atan(x); break;
}
stacks.setText(4, String.valueOf(state.getRad() ? x : Math.toDegrees(x)));
utils.refreshStack();
state.setOped(true);
state.setEdit(true);
public void actionPerformed(ActionEvent e) {
if (!stacks.get(4).endsWith("E")) {
Double x = Double.parseDouble(stacks.get(4));
switch (key.getText()) {
case "sin":
x = Math.sin(x);
break;
case "cos":
x = Math.cos(x);
break;
case "tan":
x = Math.tan(x);
break;
case "sec":
x = 1.0 / Math.cos(x);
break;
case "csc":
x = 1.0 / Math.sin(x);
break;
case "cot":
x = 1.0 / Math.tan(x);
break;
case "asin":
x = Math.asin(x);
break;
case "acos":
x = Math.acos(x);
break;
case "atan":
x = Math.atan(x);
break;
}
stacks.setText(4,
String.valueOf(state.getRad() ? x : Math.toDegrees(x)));
utils.refreshStack();
state.setOped(true);
state.setEdit(true);
}

}

}

Loading…
Cancel
Save