Skip to content

Commit 18649b7

Browse files
committed
Added Circle Obstacle (Solid) (#6)
Signed-off-by: Harsh Kumar <harsh19043@iiitd.ac.in>
1 parent 1b06f61 commit 18649b7

File tree

5 files changed

+93
-34
lines changed

5 files changed

+93
-34
lines changed

src/Controller.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import javafx.animation.Interpolator;
22
import javafx.animation.RotateTransition;
33
import javafx.fxml.FXML;
4+
import javafx.fxml.FXMLLoader;
45
import javafx.fxml.Initializable;
56
import javafx.scene.Node;
67
import javafx.scene.Scene;
@@ -11,11 +12,13 @@
1112
import javafx.stage.Stage;
1213
import javafx.util.Duration;
1314

15+
import java.io.IOException;
1416
import java.net.URL;
17+
import java.util.List;
1518
import java.util.ResourceBundle;
1619

1720
public class Controller implements Initializable {
18-
@FXML private Pane ring_1, ring_2, ring_3, icon_1, produced_btn;
21+
@FXML private Pane ring_1, ring_2, ring_3, icon_1, produced_btn, box;
1922
@FXML private ImageView ring_4, ring_5;
2023

2124
@FXML private ImageView backButton;
@@ -53,6 +56,17 @@ public void initialize(URL url, ResourceBundle rb) {
5356

5457
for(int i=0; i<items; ++i)
5558
r[i].play();
59+
60+
// For loading sample obstacles for testing purposes
61+
Pane newPane;
62+
try {
63+
newPane = FXMLLoader.load(getClass().getResource("/obstacles/circle.fxml"));
64+
List<Node> parentChildren = ((Pane)box.getParent()).getChildren();
65+
parentChildren.set(parentChildren.indexOf(box), newPane);
66+
box = newPane;
67+
} catch (IOException e) {
68+
e.printStackTrace();
69+
}
5670
}
5771

5872
@FXML

src/Main.java

+9-25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import javafx.animation.*;
21
import javafx.application.Application;
32
import javafx.fxml.FXMLLoader;
4-
import javafx.scene.*;
3+
import javafx.scene.Parent;
4+
import javafx.scene.Scene;
55
import javafx.scene.image.Image;
6-
import javafx.scene.layout.*;
7-
import javafx.scene.transform.*;
8-
import javafx.stage.*;
9-
import javafx.util.Duration;
6+
import javafx.stage.Stage;
107

118
public class Main extends Application {
129

@@ -15,24 +12,8 @@ public static void main(String[] args) {
1512
}
1613

1714
private Scene loadSampleObstacle() throws Exception {
18-
Node obstacle = FXMLLoader.load(getClass().getResource("scenes/sample.fxml"));
19-
BorderPane box = new BorderPane();
20-
box.setCenter(obstacle);
21-
Scene scene = new Scene(box, 500, 500);
22-
23-
scene.getStylesheets().add(getClass().getResource("style/style.css").toExternalForm());
24-
25-
RotateTransition rotateTransition = new RotateTransition();
26-
rotateTransition.setAxis(Rotate.Z_AXIS);
27-
rotateTransition.setByAngle(360);
28-
rotateTransition.setCycleCount(1000);
29-
rotateTransition.setDuration(Duration.millis(2000));
30-
rotateTransition.setInterpolator(Interpolator.LINEAR);
31-
rotateTransition.setNode(obstacle);
32-
rotateTransition.play();
33-
34-
scene.setCursor(Cursor.DEFAULT);
35-
return scene;
15+
Parent root = FXMLLoader.load(getClass().getResource("scenes/sample.fxml"));
16+
return new Scene(root, 500, 500);
3617
}
3718

3819
protected static Scene loadSettings() throws Exception {
@@ -55,7 +36,10 @@ public void start(Stage primaryStage) throws Exception{
5536
primaryStage.getIcons().add(new Image("/assets/color-switch-icon.png"));
5637
primaryStage.setTitle("Color Switch");
5738

58-
Scene scene = getHome();
39+
Scene scene = loadSampleObstacle();
40+
41+
42+
5943
primaryStage.setScene(scene);
6044
primaryStage.setHeight(1024);
6145
primaryStage.setWidth(768);
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package obstacles;
2+
3+
import javafx.animation.Interpolator;
4+
import javafx.animation.RotateTransition;
5+
import javafx.fxml.FXML;
6+
import javafx.fxml.Initializable;
7+
import javafx.scene.Node;
8+
import javafx.scene.layout.Pane;
9+
import javafx.scene.transform.Rotate;
10+
import javafx.util.Duration;
11+
12+
import java.net.URL;
13+
import java.util.ArrayList;
14+
import java.util.ResourceBundle;
15+
16+
public class ObstaclesController implements Initializable {
17+
static int defaultRotatingDuration = 4000;
18+
19+
ArrayList<RotateTransition> rotatingElements;
20+
@FXML
21+
Pane circle;
22+
23+
private void addRotatingNode(Node node, int timeInMillis, boolean clockwise) {
24+
RotateTransition rt = new RotateTransition();
25+
rt.setAxis(Rotate.Z_AXIS);
26+
rt.setByAngle((clockwise) ? 360 : -360);
27+
rt.setCycleCount(1000);
28+
rt.setInterpolator(Interpolator.LINEAR);
29+
rt.setDuration(Duration.millis(timeInMillis));
30+
rt.setNode(node);
31+
rt.play();
32+
}
33+
34+
private void addRotatingNode(Node node) {
35+
addRotatingNode(node, defaultRotatingDuration, true);
36+
}
37+
38+
@Override
39+
public void initialize(URL url, ResourceBundle resourceBundle) {
40+
rotatingElements = new ArrayList<>();
41+
addRotatingNode(circle);
42+
}
43+
}

src/obstacles/circle.fxml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import java.net.*?>
4+
<?import javafx.scene.layout.*?>
5+
<?import javafx.scene.shape.*?>
6+
7+
<Pane xmlns:fx="http://javafx.com/fxml/1" fx:id="circle" prefHeight="350" prefWidth="350"
8+
xmlns="http://javafx.com/javafx/11.0.1" fx:controller="obstacles.ObstaclesController">
9+
10+
<stylesheets>
11+
<URL value="@../style/style.css"/>
12+
</stylesheets>
13+
<Arc focusTraversable="true" layoutX="175.0" layoutY="175.0" length="90.0" radiusX="175.0" radiusY="175.0"
14+
startAngle="0.0" styleClass="fill-yellow" type="ROUND"/>
15+
<Arc focusTraversable="true" layoutX="175.0" layoutY="175.0" length="90.0" radiusX="175.0" radiusY="175.0"
16+
startAngle="90.0" styleClass="fill-blue" type="ROUND"/>
17+
<Arc focusTraversable="true" layoutX="175.0" layoutY="175.0" length="90.0" radiusX="175.0" radiusY="175.0"
18+
startAngle="180.0" styleClass="fill-pink" type="ROUND"/>
19+
<Arc focusTraversable="true" layoutX="175.0" layoutY="175.0" length="90.0" radiusX="175.0" radiusY="175.0"
20+
startAngle="270.0" styleClass="fill-purple" type="ROUND"/>
21+
<Circle fill="WHITE" layoutX="175.0" layoutY="175.0" radius="147.0" stroke="BLACK" strokeType="INSIDE"
22+
styleClass="fill-soft-black"/>
23+
</Pane>

src/scenes/sample.fxml

+3-8
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,17 @@
44
<?import javafx.scene.layout.GridPane?>
55
<?import javafx.scene.layout.Pane?>
66
<?import javafx.scene.layout.RowConstraints?>
7-
<?import javafx.scene.shape.*?>
87
<?import java.net.URL?>
8+
99
<GridPane xmlns:fx="http://javafx.com/fxml/1" alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/15.0.1" fx:controller="Controller">
1010
<columnConstraints>
1111
<ColumnConstraints />
1212
</columnConstraints>
1313
<rowConstraints>
1414
<RowConstraints />
1515
</rowConstraints>
16-
<Pane mouseTransparent="true" opacity="0.71" prefHeight="340.0" prefWidth="340.0">
17-
<Circle fill="WHITE" layoutX="170.0" layoutY="170.0" radius="169.0" stroke="BLACK" strokeType="INSIDE" />
18-
<Arc focusTraversable="true" layoutX="169.0" layoutY="172.0" length="88.52" mouseTransparent="true" radiusX="169.0" radiusY="169.0" stroke="#e3e857" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="0.0" styleClass="fill-yellow" type="ROUND" />
19-
<Arc focusTraversable="true" layoutX="171.0" layoutY="171.0" length="88.52" mouseTransparent="true" radiusX="169.0" radiusY="169.0" startAngle="90.0" stroke="#e3e857" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="0.0" styleClass="fill-blue" type="ROUND" />
20-
<Arc focusTraversable="true" layoutX="171.0" layoutY="170.0" length="88.52" mouseTransparent="true" radiusX="169.0" radiusY="169.0" startAngle="180.0" stroke="#e3e857" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="0.0" styleClass="fill-pink" type="ROUND" />
21-
<Arc focusTraversable="true" layoutX="169.0" layoutY="171.0" length="88.52" mouseTransparent="true" radiusX="169.0" radiusY="169.0" startAngle="270.0" stroke="#e3e857" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="0.0" styleClass="fill-purple" type="ROUND" />
22-
<Circle fill="WHITE" layoutX="170.0" layoutY="169.0" radius="139.0" stroke="BLACK" strokeType="INSIDE" styleClass="fill-soft-black" />
16+
<Pane fx:id="box" mouseTransparent="true" opacity="0.71" prefHeight="340.0" prefWidth="340.0">
17+
2318
</Pane>
2419
<stylesheets>
2520
<URL value="@../style/style.css" />

0 commit comments

Comments
 (0)