School_Project/Piece/src/application/Piece.java

81 lines
2.4 KiB
Java
Raw Normal View History

2023-03-23 14:54:45 +01:00
package application;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
2023-03-23 14:54:45 +01:00
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.KeyCode;
2023-03-23 14:54:45 +01:00
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.transform.Rotate;
import javafx.animation.RotateTransition;
2023-03-23 14:54:45 +01:00
import javafx.stage.Stage;
import javafx.util.Duration;
2023-03-23 14:54:45 +01:00
public class Piece extends Application{
public void start(Stage primaryStage) throws Exception {
Polygon piece1 = new Polygon();
piece1.getPoints().addAll(new Double [] {
0.0,0.0,
200.0,0.0,
200.0,200.0,
100.0,200.0,
100.0,100.0,
0.0,100.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();
2023-03-23 14:54:45 +01:00
piece1.setFill(Color.LIMEGREEN);
piece1.setStroke(Color.BLACK);
piece1.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);
rotatelft.autoReverseProperty();
rotatelft.setDuration(Duration.millis(1000));
rotatelft.setNode(piece1);
rotatelft.setAxis(Rotate.Z_AXIS);
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
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;
default: System.out.println("this case hasn't been taken in charge yet");
}
}
});
2023-03-23 14:54:45 +01:00
}
public static void main(String[] args) {
launch(args);
}
}