|
@@ -24,10 +24,10 @@ public class CalcSkin extends Application {
|
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", "/", "^", "sin"},
|
|
28
|
+ { "4", "5", "6", "*", "√", "cos" },
|
|
29
|
+ { "1", "2", "3", "-", "x²", "tan" },
|
|
30
|
+ { "0", "c", "=", "+", "1/x" }
|
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, EXPONENT, SQUAREROOT, SQUARED, INVERSE, SINE, COSINE, TANGENT }
|
40
|
40
|
|
41
|
41
|
private Op curOp = Op.NOOP;
|
42
|
42
|
private Op stackOp = Op.NOOP;
|
|
@@ -60,7 +60,7 @@ public class CalcSkin extends Application {
|
60
|
60
|
private VBox createLayout(TextField screen, TilePane buttons) {
|
61
|
61
|
final VBox layout = new VBox(20);
|
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: #4e4e4e; -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());
|
|
@@ -81,10 +81,10 @@ public class CalcSkin extends Application {
|
81
|
81
|
|
82
|
82
|
private TextField createScreen() {
|
83
|
83
|
final TextField screen = new TextField();
|
84
|
|
- screen.setStyle("-fx-background-color: aquamarine;");
|
|
84
|
+ screen.setStyle("-fx-background-color: #faffd3;");
|
85
|
85
|
screen.setAlignment(Pos.CENTER_RIGHT);
|
86
|
86
|
screen.setEditable(false);
|
87
|
|
- screen.textProperty().bind(Bindings.format("%.0f", currentValue));
|
|
87
|
+ screen.textProperty().bind(Bindings.format("%.2f", currentValue));
|
88
|
88
|
return screen;
|
89
|
89
|
}
|
90
|
90
|
|
|
@@ -114,7 +114,19 @@ public class CalcSkin extends Application {
|
114
|
114
|
makeClearButton(button);
|
115
|
115
|
} else if ("=".equals(s)) {
|
116
|
116
|
makeEqualsButton(button);
|
117
|
|
- }
|
|
117
|
+ } else if ("√".equals(s)) {
|
|
118
|
+ squareRootButton(button);
|
|
119
|
+ } else if ("x²".equals(s)) {
|
|
120
|
+ squaredButton(button);
|
|
121
|
+ } else if ("1/x".equals(s)) {
|
|
122
|
+ inverseButton(button);
|
|
123
|
+ } else if ("sin".equals(s)) {
|
|
124
|
+ sinButton(button);
|
|
125
|
+ } else if ("cos".equals(s)) {
|
|
126
|
+ cosButton(button);
|
|
127
|
+ } else if ("tan".equals(s)) {
|
|
128
|
+ tanButton(button);
|
|
129
|
+ }
|
118
|
130
|
}
|
119
|
131
|
|
120
|
132
|
return button;
|
|
@@ -127,6 +139,13 @@ public class CalcSkin extends Application {
|
127
|
139
|
case "-": triggerOp.set(Op.SUBTRACT); break;
|
128
|
140
|
case "*": triggerOp.set(Op.MULTIPLY); break;
|
129
|
141
|
case "/": triggerOp.set(Op.DIVIDE); break;
|
|
142
|
+ case "^": triggerOp.set(Op.EXPONENT); break;
|
|
143
|
+// case "√": triggerOp.set(Op.SQUAREROOT); break;
|
|
144
|
+// case "x²": triggerOp.set(Op.SQUARED); break;
|
|
145
|
+// case "1/x": triggerOp.set(Op.INVERSE); break;
|
|
146
|
+// case "sin": triggerOp.set(Op.SINE); break;
|
|
147
|
+// case "cos": triggerOp.set(Op.COSINE); break;
|
|
148
|
+// case "tan": triggerOp.set(Op.TANGENT); break;
|
130
|
149
|
}
|
131
|
150
|
return triggerOp;
|
132
|
151
|
}
|
|
@@ -185,10 +204,80 @@ public class CalcSkin extends Application {
|
185
|
204
|
case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
|
186
|
205
|
case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
|
187
|
206
|
case DIVIDE: currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
|
|
207
|
+ case EXPONENT: currentValue.set(calcEngine.exponent(previousValue.get(), currentValue.get())); break;
|
188
|
208
|
}
|
189
|
209
|
}
|
190
|
210
|
});
|
191
|
211
|
}
|
|
212
|
+
|
|
213
|
+ private void squaredButton(Button button) {
|
|
214
|
+ button.setStyle("-fx-base: ghostwhite;");
|
|
215
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
216
|
+ @Override
|
|
217
|
+ public void handle(ActionEvent actionEvent) {
|
|
218
|
+ SQUARED:
|
|
219
|
+ currentValue.set(calcEngine.squared(currentValue.get()));
|
|
220
|
+
|
|
221
|
+ }
|
|
222
|
+ });
|
|
223
|
+ }
|
|
224
|
+
|
|
225
|
+ private void squareRootButton(Button button) {
|
|
226
|
+ button.setStyle("-fx-base: ghostwhite;");
|
|
227
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
228
|
+ @Override
|
|
229
|
+ public void handle(ActionEvent actionEvent) {
|
|
230
|
+ SQUAREROOT:
|
|
231
|
+ currentValue.set(calcEngine.squareRoot(currentValue.get()));
|
|
232
|
+
|
|
233
|
+ }
|
|
234
|
+ });
|
|
235
|
+ }
|
|
236
|
+
|
|
237
|
+ private void inverseButton(Button button) {
|
|
238
|
+ button.setStyle("-fx-base: ghostwhite;");
|
|
239
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
240
|
+ @Override
|
|
241
|
+ public void handle(ActionEvent actionEvent) {
|
|
242
|
+ INVERSE: currentValue.set(calcEngine.inverse(currentValue.get()));
|
|
243
|
+
|
|
244
|
+ }
|
|
245
|
+ });
|
|
246
|
+ }
|
|
247
|
+
|
|
248
|
+ private void sinButton(Button button) {
|
|
249
|
+ button.setStyle("-fx-base: ghostwhite;");
|
|
250
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
251
|
+ @Override
|
|
252
|
+ public void handle(ActionEvent actionEvent) {
|
|
253
|
+ SINE: currentValue.set(calcEngine.sin(currentValue.get()));
|
|
254
|
+
|
|
255
|
+ }
|
|
256
|
+ });
|
|
257
|
+ }
|
|
258
|
+
|
|
259
|
+ private void cosButton(Button button) {
|
|
260
|
+ button.setStyle("-fx-base: ghostwhite;");
|
|
261
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
262
|
+ @Override
|
|
263
|
+ public void handle(ActionEvent actionEvent) {
|
|
264
|
+ COSINE: currentValue.set(calcEngine.cos(currentValue.get()));
|
|
265
|
+
|
|
266
|
+ }
|
|
267
|
+ });
|
|
268
|
+ }
|
|
269
|
+
|
|
270
|
+ private void tanButton(Button button) {
|
|
271
|
+ button.setStyle("-fx-base: ghostwhite;");
|
|
272
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
273
|
+ @Override
|
|
274
|
+ public void handle(ActionEvent actionEvent) {
|
|
275
|
+ TANGENT: currentValue.set(calcEngine.tan(currentValue.get()));
|
|
276
|
+
|
|
277
|
+ }
|
|
278
|
+ });
|
|
279
|
+ }
|
|
280
|
+
|
192
|
281
|
}
|
193
|
282
|
|
194
|
283
|
|