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 134
| package rotate3d;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.RotateTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.SubScene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Cylinder;
import javafx.scene.shape.Sphere;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
public final class Main extends Application {
private ComboBox<Point3D> combo;
private final Sphere sphere = new Sphere(50);
private final Cylinder cylinder = new Cylinder(25, 100);
@Override
public void start(Stage primaryStage) {
sphere.setTranslateX(400);
sphere.setTranslateY(400);
// Centre sphere à (400, 400, 0);
sphere.setMaterial(new PhongMaterial(Color.RED));
cylinder.setMaterial(new PhongMaterial(Color.CYAN));
cylinder.setTranslateX(600);
cylinder.setTranslateY(400);
// Centre sphere à (600, 400, 0);
final Group root3D = new Group();
root3D.getChildren().addAll(sphere, cylinder);
final SubScene subScene = new SubScene(root3D, 500, 500, true, SceneAntialiasing.BALANCED);
final Pane pane3D = new Pane() {
{
getChildren().add(subScene);
}
@Override
protected void layoutChildren() {
subScene.setWidth(getWidth());
subScene.setHeight(getHeight());
}
};
subScene.setFill(Color.BLACK);
subScene.setCamera(new PerspectiveCamera());
final Button button = new Button("Test");
button.setOnAction(actionEvent -> toggleAnimation());
combo = new ComboBox(FXCollections.observableArrayList(Rotate.X_AXIS, Rotate.Y_AXIS, Rotate.Z_AXIS));
combo.getSelectionModel().select(Rotate.Y_AXIS);
combo.setButtonCell(new AxisListCell());
combo.setCellFactory(listView -> new AxisListCell());
combo.valueProperty().addListener(observable -> {
if (animation != null) {
boolean wasRunning = (animation.getStatus() == Timeline.Status.RUNNING);
animation.stop();
animation = null;
cylinder.getTransforms().clear();
if (wasRunning) {
toggleAnimation();
}
}
});
final ToolBar toolBar = new ToolBar(button, combo);
final BorderPane borderPane = new BorderPane();
borderPane.setTop(toolBar);
borderPane.setCenter(pane3D);
final Scene scene = new Scene(borderPane, 800, 900);
primaryStage.setTitle("Test Rotate 3D");
primaryStage.setScene(scene);
primaryStage.show();
}
private Timeline animation;
private void toggleAnimation() {
if (animation == null) {
final Point3D axis = combo.getValue();
final Rotate rotate = new Rotate(0, axis);
// Centre rotation = centre sphere - centre cylindre
rotate.setPivotX(400 - 600);
rotate.setPivotY(400 - 400);
rotate.setPivotZ(0 - 0);
animation = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(rotate.angleProperty(), 0)),
new KeyFrame(Duration.seconds(5), new KeyValue(rotate.angleProperty(), 360))
);
animation.setCycleCount(RotateTransition.INDEFINITE);
cylinder.getTransforms().add(rotate);
}
if (animation.getStatus() == Timeline.Status.RUNNING) {
animation.pause();
} else {
animation.play();
}
}
public static void main(String[] args) {
launch(args);
}
private static class AxisListCell extends ListCell<Point3D> {
@Override
protected void updateItem(Point3D item, boolean empty) {
super.updateItem(item, empty);
String text = null;
if (!empty && item != null) {
if (Rotate.X_AXIS.equals(item)) {
text = "Axe X (" + item + ")";
} else if (Rotate.Y_AXIS.equals(item)) {
text = "Axe Y (" + item + ")";
} else if (Rotate.Z_AXIS.equals(item)) {
text = "Axe Z (" + item + ")";
}
}
setText(text);
}
}
} |
Partager