2017-05-01 12 views

Répondre

1

Vous pouvez appliquer une transformation Rotate à votre Box, car elle permet de régler l'angle, le point de pivot et l'axe de rotation. Voir javadoc.

Cela appliquera une transformation sur le centre de la boîte:

Rotate rotate = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS); 

Box box = new Box(100, 100, 100); 
box.getTransforms().add(rotate); 

alors que cela s'applique sur un de ses bords:

Rotate rotate = new Rotate(0, -50, -50, 0, Rotate.Z_AXIS); 

Box box = new Box(100, 100, 100); 
box.getTransforms().add(rotate); 

Ceci est un test rapide avec une animation à la fois cas:

private long time; 
private final IntegerProperty counter = new SimpleIntegerProperty(); 

@Override 
public void start(Stage primaryStage) { 
    Rotate rotate1 = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS); 
    rotate1.angleProperty().bind(counter); 

    Box box1 = new Box(100, 100, 100); 
    box1.setMaterial(new PhongMaterial(Color.LIMEGREEN)); 
    box1.getTransforms().add(rotate1); 

    Cylinder axis1 = new Cylinder(2, 200); 
    axis1.setRotationAxis(Rotate.X_AXIS); 
    axis1.setRotate(90); 
    axis1.setMaterial(new PhongMaterial(Color.RED)); 

    Group group1 = new Group(box1, axis1); 
    group1.setTranslateX(-100); 

    Rotate rotate2 = new Rotate(0, -50, -50, 0, Rotate.Z_AXIS); 
    rotate2.angleProperty().bind(counter); 

    Box box2 = new Box(100, 100, 100); 
    box2.setMaterial(new PhongMaterial(Color.LAVENDER)); 
    box2.getTransforms().add(rotate2); 

    Cylinder axis2 = new Cylinder(2, 200); 
    axis2.setRotationAxis(Rotate.X_AXIS); 
    axis2.setRotate(90); 
    axis2.setTranslateX(-50); 
    axis2.setTranslateY(-50); 
    axis2.setMaterial(new PhongMaterial(Color.RED)); 

    Group group2 = new Group(box2, axis2); 
    group2.setTranslateX(200); 

    Group root = new Group(group1, group2); 

    SubScene subScene = new SubScene(root, 600, 400, true, SceneAntialiasing.BALANCED); 
    PerspectiveCamera camera = new PerspectiveCamera(); 
    camera.setTranslateX(-200); 
    camera.setTranslateY(-200); 
    camera.setTranslateZ(-100); 
    camera.setRotationAxis(new Point3D(0, 0.5, 0.5)); 
    camera.setRotate(10); 
    subScene.setCamera(camera); 
    Scene scene = new Scene(new StackPane(subScene), 600, 400, true, SceneAntialiasing.BALANCED); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 

    AnimationTimer timer = new AnimationTimer() { 
     @Override 
     public void handle(long now) { 
      if (now - time > 30_000_000) { 
       counter.set((counter.get() + 1) % 360); 
       time = now; 
      } 
     } 
    }; 
    timer.start(); 
} 

rotating boxes 1

rotating boxes 2

+0

merci. Ces lignes rouges ont vraiment aidé à le rendre beaucoup plus clair. Appréciez-le !! – saladsrock

+0

Heureux qu'il vous a aidé. Envisagez de marquer la réponse comme acceptée afin qu'elle puisse être utile aux autres aussi. –