From a99a53868b7f0469c5eb1ae249fad37edc5155e8 Mon Sep 17 00:00:00 2001 From: Eddy Date: Thu, 30 Mar 2023 11:47:23 +0200 Subject: [PATCH] progress --- Piece/src/application/Piece.java | 244 ++++++++++++++++++++++--------- 1 file changed, 176 insertions(+), 68 deletions(-) diff --git a/Piece/src/application/Piece.java b/Piece/src/application/Piece.java index 357320f..c5eadb8 100644 --- a/Piece/src/application/Piece.java +++ b/Piece/src/application/Piece.java @@ -3,6 +3,7 @@ import javafx.application.Application; import javafx.scene.Node; import javafx.event.Event; import javafx.event.EventHandler; +import javafx.scene.layout.Pane; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.Cursor; @@ -12,6 +13,7 @@ import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; import javafx.scene.shape.Polygon; import javafx.scene.transform.Rotate; +import javafx.scene.transform.Scale; import javafx.animation.RotateTransition; import javafx.stage.Stage; import javafx.util.Duration; @@ -19,9 +21,21 @@ public class Piece extends Application{ double x; double y; + + + public void start(Stage primaryStage) throws Exception { Polygon piece1 = new Polygon(); + Polygon piece2 = new Polygon(); + Polygon piece3 = new Polygon(); + Polygon piece4 = new Polygon(); + Polygon piece5 = new Polygon(); + Polygon piece6 = new Polygon(); + Polygon piece7 = new Polygon(); + + + piece1.getPoints().addAll(new Double [] { 0.0,0.0, 200.0,0.0, @@ -29,90 +43,184 @@ public class Piece extends Application{ 100.0,200.0, 100.0,100.0, 0.0,100.0}); + + piece2.getPoints().addAll(new Double [] { + 100.0,0.0, + 200.0,0.0, + 200.0,300.0, + 100.0,300.0, + }); + + piece3.getPoints().addAll(new Double [] { + 0.0,0.0, + 200.0,0.0, + 200.0,200.0, + 0.0,200.0, + }); + + piece4.getPoints().addAll(new Double [] { + 0.0,0.0, + 200.0,0.0, + 200.0,300.0, + 0.0,300.0, + + }); + + piece5.getPoints().addAll(new Double [] { + 0.0,0.0, + 100.0,0.0, + 100.0,100.0, + 0.0,100.0, + }); + + piece6.getPoints().addAll(new Double [] { + 0.0,0.0, + 200.0,0.0, + 200.0,300.0, + 100.0,300.0, + 100.0,100.0, + 0.0,100.0}); + + piece7.getPoints().addAll(new Double [] { + 100.0,0.0, + 200.0,0.0, + 200.0,200.0, + 100.0,200.0, + }); - Group root = new Group(); - root.getChildren().addAll(piece1); - Scene scene = new Scene(root,490,450,Color.WHEAT); - primaryStage.setScene(scene); - primaryStage.setTitle("piece 1"); - primaryStage.show(); + /*for(int i ; i makeDraggable((Polygon) n)); + root.getChildren().forEach(n -> rotation((Polygon) n, scene)); + primaryStage.setScene(scene); + primaryStage.setTitle("piece 1"); + + primaryStage.show(); piece1.setFill(Color.LIMEGREEN); piece1.setStroke(Color.BLACK); piece1.setStrokeWidth(3); + + piece2.setFill(Color.LIMEGREEN); + piece2.setStroke(Color.BLACK); + piece2.setStrokeWidth(3); + + piece3.setFill(Color.LIMEGREEN); + piece3.setStroke(Color.BLACK); + piece3.setStrokeWidth(3); + + piece4.setFill(Color.LIMEGREEN); + piece4.setStroke(Color.BLACK); + piece4.setStrokeWidth(3); + + piece5.setFill(Color.LIMEGREEN); + piece5.setStroke(Color.BLACK); + piece5.setStrokeWidth(3); + + piece6.setFill(Color.LIMEGREEN); + piece6.setStroke(Color.BLACK); + piece6.setStrokeWidth(3); + + piece7.setFill(Color.LIMEGREEN); + piece7.setStroke(Color.BLACK); + piece7.setStrokeWidth(3); - - //Instantiating RotateTransition class to create the animation (rotation to the right) - RotateTransition rotaterght = new RotateTransition(); - //setting attributes for the RotateTransition - rotaterght.setByAngle(90); - rotaterght.autoReverseProperty(); - rotaterght.setDuration(Duration.millis(1000)); - rotaterght.setNode(piece1); - rotaterght.setAxis(Rotate.Z_AXIS); - - //Instantiating RotateTransition class to create the animation (rotation to the left) - RotateTransition rotatelft = new RotateTransition(); - //setting attributes for the RotateTransition - rotatelft.setByAngle(-90);// minus to rotate to the left - rotatelft.autoReverseProperty(); - rotatelft.setDuration(Duration.millis(1000)); - rotatelft.setNode(piece1); - rotatelft.setAxis(Rotate.Z_AXIS); - - //Mouse events handling - piece1.setOnMousePressed(new EventHandler() { - @Override public void handle(MouseEvent mouseEvent) { - // record a (x,y) distance for the drag and drop operation. - x = piece1.getLayoutX() - mouseEvent.getSceneX(); - y = piece1.getLayoutY() - mouseEvent.getSceneY(); - piece1.setCursor(Cursor.CLOSED_HAND); - piece1.setFill(Color.AZURE); - } - }); - - piece1.setOnMouseReleased(new EventHandler() { - @Override public void handle(MouseEvent mouseEvent) { - piece1.setCursor(Cursor.HAND); - piece1.setFill(Color.LIMEGREEN); - } - }); - - piece1.setOnMouseDragged(new EventHandler() { - @Override public void handle(MouseEvent mouseEvent) { - piece1.setLayoutX(mouseEvent.getSceneX() + x); - piece1.setLayoutY(mouseEvent.getSceneY() + y); - } - }); - - piece1.setOnMouseEntered(new EventHandler() { - @Override public void handle(MouseEvent mouseEvent) { - piece1.setCursor(Cursor.HAND); - } - }); - - - - scene.setOnKeyPressed(new EventHandler() { - + } + //double click delayed bug to fix + public static void rotaterght (Node node) { + RotateTransition rotaterght = new RotateTransition(); + //setting attributes for the RotateTransition + rotaterght.setByAngle(90); + rotaterght.autoReverseProperty(); + rotaterght.setDuration(Duration.millis(1000)); + rotaterght.setNode(node); + rotaterght.setAxis(Rotate.Z_AXIS); + rotaterght.play();} + + public static void rotatelft (Node node) { + //Instantiating RotateTransition class to create the animation (rotation to the left) + RotateTransition rotatelft = new RotateTransition(); + //setting attributes for the RotateTransition + rotatelft.setByAngle(-90);// minus to rotate to the left + rotatelft.autoReverseProperty(); + rotatelft.setDuration(Duration.millis(1000)); + rotatelft.setNode(node); + rotatelft.setAxis(Rotate.Z_AXIS); + rotatelft.play();} + + public void rotation (Polygon node, Scene scene) { + scene.setOnKeyPressed(new EventHandler() { public void handle(KeyEvent event) {// 'event' means corresponds to clicking on the clipboard switch (event.getCode()) {// 'getCode' gets the code of the key pressed on the clipboard - case RIGHT: rotaterght.play(); break; - case LEFT: rotatelft.play(); break; + case RIGHT: rotaterght(node); break; + //args to set + case LEFT: rotatelft(node); break; default: System.out.println("this case hasn't been taken in charge yet"); } } - }); + }); + } + + + + public void makeDraggable(Polygon node) { + node.setOnMousePressed(new EventHandler() { + @Override public void handle(MouseEvent mouseEvent) { + // record a (x,y) distance for the drag and drop operation. + x = node.getLayoutX() - mouseEvent.getSceneX(); + y = node.getLayoutY() - mouseEvent.getSceneY(); + node.requestFocus(); + node.setFocusTraversable(true); + node.focusedProperty(); + node.focusVisibleProperty(); + /*node.setFocused(true);*/ + node.setCursor(Cursor.CLOSED_HAND); + node.setFill(Color.AZURE); + } + }); - - } - + node.setOnMouseReleased(new EventHandler() { + @Override public void handle(MouseEvent mouseEvent) { + node.setCursor(Cursor.HAND); + node.setFill(Color.LIMEGREEN); + } + }); + + node.setOnMouseDragged(new EventHandler() { + @Override public void handle(MouseEvent mouseEvent) { + node.setLayoutX(mouseEvent.getSceneX() + x); + node.setLayoutY(mouseEvent.getSceneY() + y); + } + }); + + node.setOnMouseEntered(new EventHandler() { + @Override public void handle(MouseEvent mouseEvent) { + node.setCursor(Cursor.HAND); + } + }); + + } + public static void main(String[] args) { launch(args); - } -} \ No newline at end of file + } +}