简而言之 - 我有 2 个旋转矩形。在旋转(按 Y 轴)时,它们应该一个接一个地重叠 - 不幸的是,尽管旋转了 180 度,但其中一个 ractangles“总是在前面”。如何解决这种行为?看起来它与添加到组中的矩形的顺序有关。最后添加的总是在前面。
场景:
package drawing.scene;
import javafx.application.Application;
import javafx.geometry.Point3D;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import drawing.objects.Clock;
import drawing.objects.Cube;
public class MyScene extends Application {
private int sceneEdgeSize = 800;
private int clolcSize = 400;
@Override
public void start(Stage primaryStage) throws Exception {
Group g = new Group();
g.setTranslateX((sceneEdgeSize - clolcSize) / 2f);
g.setTranslateY((sceneEdgeSize - clolcSize) / 2f);
final Cube c = new Cube(clolcSize);
g.getChildren().add(c);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Rotate r = rotate(0, Rotate.Y_AXIS);
c.getTransforms().add(r);
double angle = 0.0;
while (true) {
r.setAngle(angle += 2);
try {
Thread.sleep(25);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
t.setDaemon(true);
primaryStage.setScene(new Scene(g, sceneEdgeSize, sceneEdgeSize));
PerspectiveCamera camera = new PerspectiveCamera();
primaryStage.getScene().setCamera(camera);
primaryStage.show();
t.start();
}
public static void main(String[] args) {
launch(args);
}
public Rotate rotate(double angle, Point3D axis) {
return new Rotate(angle, clolcSize / 2f, clolcSize / 2f, 0, axis);
}
}
立方体类:
package drawing.objects;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.scene.transform.Rotate;
public class Cube extends Group {
private double edgeLength = 0;
public Cube(double edgeLength) {
super();
this.edgeLength = edgeLength;
create();
}
private void create() {
final Rotate rx = new Rotate(0, Rotate.X_AXIS);
final Rotate ry = new Rotate(0, Rotate.Y_AXIS);
final Rotate rz = new Rotate(0, Rotate.Z_AXIS);
this.getTransforms().addAll(rx, ry, rz);
ObservableList<Node> children = this.getChildren();
//@formatter:off
Rectangle rect;
rect = RectangleBuilder // face
.create()
.width(edgeLength-20)
.height(edgeLength-20)
.translateZ(edgeLength * 0.5)
// .translateY(edgeLength * 0.5)
// .translateX(-edgeLength * 0.5)
.fill(Color.LIGHTGREEN)
.build()
;
children.add(rect);
rect = RectangleBuilder // face
.create()
.width(edgeLength-20)
.height(edgeLength-20)
.translateZ(-edgeLength * 0.5)
// .translateY(-edgeLength * 0.5)
// .translateX(-edgeLength * 0.5)
.fill(Color.DARKGREEN)
.build()
;
children.add(rect);
//@formatter:on
}
}