From 18649b7e7954e0c0bd19d36f51e12bd68a9f241c Mon Sep 17 00:00:00 2001 From: Harsh Kumar Date: Sat, 21 Nov 2020 13:36:03 +0530 Subject: [PATCH] Added Circle Obstacle (Solid) (#6) Signed-off-by: Harsh Kumar --- src/Controller.java | 16 +++++++++- src/Main.java | 34 ++++++-------------- src/obstacles/ObstaclesController.java | 43 ++++++++++++++++++++++++++ src/obstacles/circle.fxml | 23 ++++++++++++++ src/scenes/sample.fxml | 11 ++----- 5 files changed, 93 insertions(+), 34 deletions(-) create mode 100644 src/obstacles/ObstaclesController.java create mode 100644 src/obstacles/circle.fxml diff --git a/src/Controller.java b/src/Controller.java index 565e5e2..5990f9a 100644 --- a/src/Controller.java +++ b/src/Controller.java @@ -1,6 +1,7 @@ import javafx.animation.Interpolator; import javafx.animation.RotateTransition; import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.scene.Node; import javafx.scene.Scene; @@ -11,11 +12,13 @@ import javafx.stage.Stage; import javafx.util.Duration; +import java.io.IOException; import java.net.URL; +import java.util.List; import java.util.ResourceBundle; public class Controller implements Initializable { - @FXML private Pane ring_1, ring_2, ring_3, icon_1, produced_btn; + @FXML private Pane ring_1, ring_2, ring_3, icon_1, produced_btn, box; @FXML private ImageView ring_4, ring_5; @FXML private ImageView backButton; @@ -53,6 +56,17 @@ public void initialize(URL url, ResourceBundle rb) { for(int i=0; i parentChildren = ((Pane)box.getParent()).getChildren(); + parentChildren.set(parentChildren.indexOf(box), newPane); + box = newPane; + } catch (IOException e) { + e.printStackTrace(); + } } @FXML diff --git a/src/Main.java b/src/Main.java index 52436a9..de6946b 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,12 +1,9 @@ -import javafx.animation.*; import javafx.application.Application; import javafx.fxml.FXMLLoader; -import javafx.scene.*; +import javafx.scene.Parent; +import javafx.scene.Scene; import javafx.scene.image.Image; -import javafx.scene.layout.*; -import javafx.scene.transform.*; -import javafx.stage.*; -import javafx.util.Duration; +import javafx.stage.Stage; public class Main extends Application { @@ -15,24 +12,8 @@ public static void main(String[] args) { } private Scene loadSampleObstacle() throws Exception { - Node obstacle = FXMLLoader.load(getClass().getResource("scenes/sample.fxml")); - BorderPane box = new BorderPane(); - box.setCenter(obstacle); - Scene scene = new Scene(box, 500, 500); - - scene.getStylesheets().add(getClass().getResource("style/style.css").toExternalForm()); - - RotateTransition rotateTransition = new RotateTransition(); - rotateTransition.setAxis(Rotate.Z_AXIS); - rotateTransition.setByAngle(360); - rotateTransition.setCycleCount(1000); - rotateTransition.setDuration(Duration.millis(2000)); - rotateTransition.setInterpolator(Interpolator.LINEAR); - rotateTransition.setNode(obstacle); - rotateTransition.play(); - - scene.setCursor(Cursor.DEFAULT); - return scene; + Parent root = FXMLLoader.load(getClass().getResource("scenes/sample.fxml")); + return new Scene(root, 500, 500); } protected static Scene loadSettings() throws Exception { @@ -55,7 +36,10 @@ public void start(Stage primaryStage) throws Exception{ primaryStage.getIcons().add(new Image("/assets/color-switch-icon.png")); primaryStage.setTitle("Color Switch"); - Scene scene = getHome(); + Scene scene = loadSampleObstacle(); + + + primaryStage.setScene(scene); primaryStage.setHeight(1024); primaryStage.setWidth(768); diff --git a/src/obstacles/ObstaclesController.java b/src/obstacles/ObstaclesController.java new file mode 100644 index 0000000..7fcc08b --- /dev/null +++ b/src/obstacles/ObstaclesController.java @@ -0,0 +1,43 @@ +package obstacles; + +import javafx.animation.Interpolator; +import javafx.animation.RotateTransition; +import javafx.fxml.FXML; +import javafx.fxml.Initializable; +import javafx.scene.Node; +import javafx.scene.layout.Pane; +import javafx.scene.transform.Rotate; +import javafx.util.Duration; + +import java.net.URL; +import java.util.ArrayList; +import java.util.ResourceBundle; + +public class ObstaclesController implements Initializable { + static int defaultRotatingDuration = 4000; + + ArrayList rotatingElements; + @FXML + Pane circle; + + private void addRotatingNode(Node node, int timeInMillis, boolean clockwise) { + RotateTransition rt = new RotateTransition(); + rt.setAxis(Rotate.Z_AXIS); + rt.setByAngle((clockwise) ? 360 : -360); + rt.setCycleCount(1000); + rt.setInterpolator(Interpolator.LINEAR); + rt.setDuration(Duration.millis(timeInMillis)); + rt.setNode(node); + rt.play(); + } + + private void addRotatingNode(Node node) { + addRotatingNode(node, defaultRotatingDuration, true); + } + + @Override + public void initialize(URL url, ResourceBundle resourceBundle) { + rotatingElements = new ArrayList<>(); + addRotatingNode(circle); + } +} diff --git a/src/obstacles/circle.fxml b/src/obstacles/circle.fxml new file mode 100644 index 0000000..21f130b --- /dev/null +++ b/src/obstacles/circle.fxml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + diff --git a/src/scenes/sample.fxml b/src/scenes/sample.fxml index a3b8e96..b492e94 100644 --- a/src/scenes/sample.fxml +++ b/src/scenes/sample.fxml @@ -4,8 +4,8 @@ - + @@ -13,13 +13,8 @@ - - - - - - - + +