School_Project/Piece/src/application/Piece.java

139 lines
4.0 KiB
Java
Raw Normal View History

2023-03-23 14:54:45 +01:00
package application;
import javafx.application.Application;
import javafx.scene.Node;
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.Cursor;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseEvent;
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{
double x;
double y;
2023-03-23 14:54:45 +01:00
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);
/* */
piece1.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
// record a delta 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<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
piece1.setCursor(Cursor.HAND);
piece1.setFill(Color.LIMEGREEN);
}
});
piece1.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
piece1.setLayoutX(mouseEvent.getSceneX() + x);
piece1.setLayoutY(mouseEvent.getSceneY() + y);
}
});
piece1.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
piece1.setCursor(Cursor.HAND);
}
});
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 void pressed(MouseEvent event, Polygon piece1) {
piece1.setFill(Color.AZURE);
int x = piece1.getLayoutX() - MouseEvent.getSceneX();
int y = piece1.getLayoutY() - MouseEvent.getSceneY();
piece1.setCursor(Cursor.MOVE);
}
public void dragged(MouseEvent event, Polygon piece1) {
piece1.setLayoutX(event.getSceneX()+ x);
piece1.setLayoutY(event.getSceneY()+ y);
piece1.draw();
}
public void released(MouseEvent event, Polygon piece1) {
piece1.setCursor(Cursor.HAND);
}
*/
2023-03-23 14:54:45 +01:00
public static void main(String[] args) {
launch(args);
}
}