- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject2.java
More file actions
Latest commit
138 lines (93 loc) · 4.66 KB
/
Copy pathProject2.java
File metadata and controls
138 lines (93 loc) · 4.66 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
publicclassExercise_16_09extendsApplication{
protectedRectanglerectangle = getRectangle();
protectedPanepaneForRectangles = newPane(rectangle);
protectedTablePanetable = newTablePane();
@Override
//Start method for application class overridden
publicvoidstart(StageprimaryStage){
//pane containing table for rectangle display made
paneForRectangles.getChildren().addAll(
table.getRectangle1(), table.getRectangle2());
//Buttons for user adjustments made
ButtonbtRedrawCircles = newButton("Redraw Rectangles");
//VBox made to stack child nodes of rectangles over each other
VBoxpane = newVBox();
pane.setAlignment(Pos.CENTER);
pane.getChildren().addAll(intersect(), paneForRectangles,
table, btRedrawCircles);
//Handlers for child nodes created and getters set
btRedrawCircles.setOnAction(e ->{
table.redraw();
pane.getChildren().remove(0);
pane.getChildren().add(0, intersect());
});
//Table getter with rectangle locations
table.getRectangle1().setOnMouseDragged(e ->{
if (table.getRectangle1().contains(e.getX(), e.getY())){
table.setRectangle1X(e.getX() - (table.getRectangle1Width() / 2));
table.setRectangle1Y(e.getY() - (table.getRectangle1Height() / 2));
table.setTfRectangle1X(String.valueOf(e.getX()
- (table.getRectangle1Width() / 2)));
table.setTfRectangle1Y(String.valueOf(e.getY()
- (table.getRectangle1Height() / 2)));
}
pane.getChildren().remove(0);
pane.getChildren().add(0, intersect());
});
//Table getter with rectangle 2 locations input
table.getRectangle2().setOnMouseDragged(e ->{
if (table.getRectangle2().contains(e.getX(), e.getY())){
table.setRectangle2X(e.getX() - (table.getRectangle2Width() / 2));
table.setRectangle2Y(e.getY() - (table.getRectangle2Height() / 2));
table.setTfRectangle2X(String.valueOf(e.getX()
- (table.getRectangle2Width() / 2)));
table.setTfRectangle2Y(String.valueOf(e.getY()
- (table.getRectangle2Height() / 2)));
}
pane.getChildren().remove(0);
pane.getChildren().add(0, intersect());
});
//Scene made in stage to display both rectangle 1 & 2
Scenescene = newScene(pane);
//Stage title/name setter
primaryStage.setTitle("Java 16.9");
//Scene placed into stage for final display
primaryStage.setScene(scene);
//Scene displayed
primaryStage.show();
}
//Detection of intersection noted and displayed to user
privateTextintersect(){
returnnewText("Two rectangles intersect? " +
(isIntersect() ? "yes" : "no"));
}
/* Main method describing whether or not there is an intersection
of rectangles in the scene */
privatebooleanisIntersect(){
return
(getDistance(table.getRectangle2X(), table.getRectangle1X() +
table.getRectangle1Width()) <
table.getRectangle1Width() + table.getRectangle2Width() &&
(getDistance(table.getRectangle2Y(), table.getRectangle1Y() +
table.getRectangle1Height()) <
table.getRectangle1Height() + table.getRectangle2Height())) &&
(getDistance(table.getRectangle1X(), table.getRectangle2X() +
table.getRectangle2Width()) <
table.getRectangle1Width() + table.getRectangle2Width() &&
(getDistance(table.getRectangle1Y(), table.getRectangle2Y() +
table.getRectangle2Height()) <
table.getRectangle1Height() + table.getRectangle2Height()));
}
//Distance then returned to user through p2-p1 function
privatedoublegetDistance(doublep1, doublep2){
returnMath.sqrt(Math.pow(p2 - p1, 2));
}
//Rectangle size and color displayed via JavaFX
privateRectanglegetRectangle(){
Rectangler = newRectangle(0, 0, 280, 110);
r.setStroke(Color.WHITE);
r.setFill(Color.WHITE);
//Program ended
returnr;
}
}