-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElementView.java
More file actions
72 lines (68 loc) · 2.12 KB
/
ElementView.java
File metadata and controls
72 lines (68 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main.java;
import javafx.geometry.Pos;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
public class ElementView {
StackPane rootNode;
TextField acceptor;
Rectangle base;
Position pos;
public ElementView(Position pos) {
rootNode = new StackPane();
acceptor = new TextField();
acceptor.setMaxWidth(45);
acceptor.setMaxHeight(45);
acceptor.setAlignment(Pos.CENTER);
this.pos = pos;
}
public StackPane getRootNode() {
rootNode.getChildren().clear();
base = new Rectangle(75, 75, Color.TRANSPARENT);
base.setFill(Color.LIGHTGRAY);
if ((pos.getRow() + pos.getCol()) % 2 == 0) {
base.setFill(Color.LIGHTBLUE);
}
rootNode.getChildren().addAll(base, acceptor);
return rootNode;
}
public double getValue() {
double value;
try {
value = Double.parseDouble(acceptor.getText());
} catch (Exception e) {
if (acceptor.getText().contains("/")) {
double numerator = 0;
double denominator = 1;
String answer = acceptor.getText();
int index = answer.indexOf("/");
String numeratorString = answer.substring(0,index);
String denominatorString = answer.substring(index + 1);
try {
numerator = Double.parseDouble(numeratorString);
denominator = Double.parseDouble(denominatorString);
return Math.floor((numerator/denominator) * 100) / 100;
} catch (Exception f) {
return 0;
}
}
return 0;
}
return value;
}
public StackPane getRootNode(double value) {
rootNode.getChildren().clear();
base = new Rectangle(46, 46, Color.TRANSPARENT);
base.setFill(Color.BLACK);
Text text = new Text(value + "");
Rectangle backer = new Rectangle(45, 45, Color.WHITE);
if ((pos.getRow() + pos.getCol()) % 2 == 0) {
base.setFill(Color.BLACK);
}
rootNode.getChildren().addAll(base, backer, text);
return rootNode;
}
}