|
@@ -18,16 +18,16 @@ import java.util.HashMap;
|
18
|
18
|
import java.util.Map;
|
19
|
19
|
|
20
|
20
|
// a simple JavaFX calculator.
|
21
|
|
-public class CalcSkin extends Application {
|
|
21
|
+public class CalcSkin extends Application{
|
22
|
22
|
|
23
|
23
|
public static void main(String[] args){
|
24
|
24
|
launch(args);
|
25
|
25
|
}
|
26
|
26
|
private static final String[][] template = {
|
27
|
|
- { "7", "8", "9", "/" },
|
28
|
|
- { "4", "5", "6", "*" },
|
29
|
|
- { "1", "2", "3", "-" },
|
30
|
|
- { "0", "c", "=", "+" }
|
|
27
|
+ { "7", "8", "9", "/", "S" },
|
|
28
|
+ { "4", "5", "6", "*", "SR" },
|
|
29
|
+ { "1", "2", "3", "-", "INV" },
|
|
30
|
+ { "0", "c", "=", "+", "EXP" }
|
31
|
31
|
};
|
32
|
32
|
|
33
|
33
|
private final Map<String, Button> accelerators = new HashMap<>();
|
|
@@ -36,7 +36,7 @@ public class CalcSkin extends Application {
|
36
|
36
|
private DoubleProperty currentValue = new SimpleDoubleProperty();
|
37
|
37
|
private CalcEngine calcEngine = new CalcEngine();
|
38
|
38
|
|
39
|
|
- private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE }
|
|
39
|
+ private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE, SQUARE, SQUARE_ROOT, EXPONENT, INVERT }
|
40
|
40
|
|
41
|
41
|
private Op curOp = Op.NOOP;
|
42
|
42
|
private Op stackOp = Op.NOOP;
|
|
@@ -51,16 +51,16 @@ public class CalcSkin extends Application {
|
51
|
51
|
final TilePane buttons = createButtons();
|
52
|
52
|
|
53
|
53
|
stage.setTitle("Calc");
|
54
|
|
- stage.initStyle(StageStyle.UTILITY);
|
|
54
|
+ stage.initStyle(StageStyle.DECORATED);
|
55
|
55
|
stage.setResizable(false);
|
56
|
56
|
stage.setScene(new Scene(createLayout(screen, buttons)));
|
57
|
57
|
stage.show();
|
58
|
58
|
}
|
59
|
59
|
|
60
|
60
|
private VBox createLayout(TextField screen, TilePane buttons) {
|
61
|
|
- final VBox layout = new VBox(20);
|
|
61
|
+ final VBox layout = new VBox(70);
|
62
|
62
|
layout.setAlignment(Pos.CENTER);
|
63
|
|
- layout.setStyle("-fx-background-color: silver; -fx-padding: 20; -fx-font-size: 20;");
|
|
63
|
+ layout.setStyle("-fx-background-color: lightBlue; -fx-padding: 20; -fx-font-size: 20;");
|
64
|
64
|
layout.getChildren().setAll(screen, buttons);
|
65
|
65
|
handleAccelerators(layout);
|
66
|
66
|
screen.prefWidthProperty().bind(buttons.widthProperty());
|
|
@@ -127,6 +127,10 @@ public class CalcSkin extends Application {
|
127
|
127
|
case "-": triggerOp.set(Op.SUBTRACT); break;
|
128
|
128
|
case "*": triggerOp.set(Op.MULTIPLY); break;
|
129
|
129
|
case "/": triggerOp.set(Op.DIVIDE); break;
|
|
130
|
+ case "S": triggerOp.set(Op.SQUARE); break;
|
|
131
|
+ case "SR" : triggerOp.set(Op.SQUARE_ROOT); break;
|
|
132
|
+ case "EXP" : triggerOp.set(Op.EXPONENT); break;
|
|
133
|
+ case "INV" : triggerOp.set(Op.INVERT); break;
|
130
|
134
|
}
|
131
|
135
|
return triggerOp;
|
132
|
136
|
}
|
|
@@ -185,6 +189,10 @@ public class CalcSkin extends Application {
|
185
|
189
|
case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
|
186
|
190
|
case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
|
187
|
191
|
case DIVIDE: currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
|
|
192
|
+ case SQUARE: currentValue.set(calcEngine.square(previousValue.get(), currentValue.get())); break;
|
|
193
|
+ case SQUARE_ROOT: currentValue.set(calcEngine.squareRoot(previousValue.get(), currentValue.get())); break;
|
|
194
|
+ case EXPONENT: currentValue.set(calcEngine.exponent(previousValue.get(), currentValue.get())); break;
|
|
195
|
+ case INVERT: currentValue.set(calcEngine.invert(previousValue.get(), currentValue.get())); break;
|
188
|
196
|
}
|
189
|
197
|
}
|
190
|
198
|
});
|