1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| package test;
import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public final class Main extends Application {
@Override
public void start(final Stage stage) throws Exception {
final var board = new Region() {
final Rectangle cell0 = new Rectangle();
final Rectangle cell1 = new Rectangle();
final Rectangle cell2 = new Rectangle();
final Rectangle cell3 = new Rectangle();
final Circle peon = new Circle();
private boolean initialized = false;
private boolean animated = false;
private double mouseX;
private double mouseY;
private Timeline animation;
{
setId("board");
cell0.setId("cell0");
cell0.setFill(Color.WHITESMOKE);
cell1.setId("cell1");
cell1.setFill(Color.GAINSBORO);
cell2.setId("cell2");
cell2.setFill(Color.LIGHTGRAY);
cell3.setId("cell3");
cell3.setFill(Color.LIGHTGREEN);
getChildren().setAll(cell0, cell1, cell2, cell3);
peon.setId("peon");
peon.setRadius(20);
peon.setFill(Color.RED);
getChildren().add(peon);
//
peon.setOnMousePressed(event -> {
if (animation != null) {
return;
}
initialized = true;
mouseX = event.getSceneX();
mouseY = event.getSceneY();
event.consume();
});
peon.setOnMouseDragged(event -> {
if (animation != null) {
return;
}
final double deltaX = event.getSceneX() - mouseX;
final double deltaY = event.getSceneY() - mouseY;
peon.setCenterX(peon.getCenterX() + deltaX);
peon.setCenterY(peon.getCenterY() + deltaY);
mouseX = event.getSceneX();
mouseY = event.getSceneY();
event.consume();
});
peon.setOnMouseReleased(event -> {
if (animation != null) {
return;
}
final double srcX = peon.getCenterX();
final double srcY = peon.getCenterY();
final double destX = cell3.getLayoutX() + cell3.getWidth() / 2.0;
final double destY = cell3.getLayoutY() + cell3.getHeight() / 2.0;
animation = new Timeline();
animation.getKeyFrames().addAll(
new KeyFrame(Duration.ZERO,
new KeyValue(peon.centerXProperty(), srcX),
new KeyValue(peon.centerYProperty(), srcY)),
new KeyFrame(Duration.millis(750),
new KeyValue(peon.centerXProperty(), destX),
new KeyValue(peon.centerYProperty(), destY)));
animation.setOnFinished(e -> animation = null);
animation.play();
event.consume();
});
}
@Override
protected void layoutChildren() {
super.layoutChildren();
final double width = getWidth();
final double height = getHeight();
final double size = width / 2.0;
cell0.setLayoutX(0);
cell0.setLayoutY(0);
cell0.setWidth(size);
cell0.setHeight(size);
//
cell1.setLayoutX(size);
cell1.setLayoutY(0);
cell1.setWidth(size);
cell1.setHeight(size);
//
cell2.setLayoutX(0);
cell2.setLayoutY(size);
cell2.setWidth(size);
cell2.setHeight(size);
//
cell3.setLayoutX(size);
cell3.setLayoutY(size);
cell3.setWidth(size);
cell3.setHeight(size);
// Initial position.
if (!initialized) {
peon.setCenterX(cell3.getLayoutX() + cell3.getWidth() / 2.0);
peon.setCenterY(cell3.getLayoutY() + cell3.getHeight() / 2.0);
}
}
};
board.setStyle("-fx-border-color: red; -fx-border-width: 1px; -fx-border-insets: -1px;");
board.setMinSize(500, 500);
board.setPrefSize(500, 500);
board.setMaxSize(500, 500);
final var root = new StackPane(board);
final var scene = new Scene(root);
stage.setScene(scene);
stage.setMinWidth(500 + 30);
stage.setMinHeight(500 + 50);
stage.setWidth(600);
stage.setHeight(600);
stage.show();
}
} |
Partager