以下是代码:
import javafx.animation.Animation;
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Box;
import javafx.scene.shape.CullFace;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
public class CameraTest extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) {
Box box = new Box(100, 100, 100);
box.setCullFace(CullFace.NONE);
box.setTranslateX(0);
box.setTranslateY(0);
box.setTranslateZ(0);
PerspectiveCamera camera = new PerspectiveCamera(false);
camera.setTranslateX(0);
camera.setTranslateY(0);
camera.setTranslateZ(0);
// Add a Rotation animation to the camera
RotateTransition rt = new RotateTransition(Duration.seconds(2), box);
rt.setCycleCount(Animation.INDEFINITE);
rt.setFromAngle(0);
rt.setToAngle(360);
rt.setAutoReverse(true);
rt.setAxis(Rotate.Y_AXIS);
rt.play();
// PointLight redLight = new PointLight();
// redLight.setColor(Color.RED);
// redLight.setTranslateX(250);
// redLight.setTranslateY(-100);
// redLight.setTranslateZ(250);
PointLight greenLight = new PointLight();
greenLight.setColor(Color.GREEN);
greenLight.setTranslateX(250);
greenLight.setTranslateY(300);
greenLight.setTranslateZ(300);
Group root = new Group(box, greenLight);
root.setRotationAxis(Rotate.X_AXIS);
root.setRotate(30);
Scene scene = new Scene(root, 500, 300, true);
scene.setCamera(camera);
stage.setScene(scene);
stage.setTitle("Using camaras");
stage.show();
}
}发布于 2015-08-29 10:46:56
对于第一个问题:是的,正如你所描述的那样,有一个向上向下的运动。解释很简单:
您已经将旋转框添加到Group中,并且根据Javadoc的说法:
组节点包含一个子节点的ObservableList,该子节点在呈现该节点时按顺序呈现。一个集团将承担其子女的集体界限,不能直接调整大小。应用于组的任何转换、效果或状态都将应用于该组的所有子组。这样的转换和效果将不包括在这个组的布局边界中,但是如果转换和效果直接设置在这个组的子组中,那么这些转换和效果将包含在这个组的布局边界中。
最后一条语句指出,应用于框的旋转正在影响组布局界限。
由于场景根是组,其布局更改反映在场景上。
如果您跟踪组的Y中心:
root.boundsInLocalProperty().addListener((obs, oldBounds, newBounds)->{
double yCenterLocal = newBounds.getWidth()/2;
double yCenterScene = root.localToScene(new Point2D(0,yCenterLocal)).getY();
System.out.println(yCenterScene);
});您将看到位置的变化:
212.89169311523438
209.2910614013672
209.34730529785156
209.4747772216797
209.52439880371094
209.576171875这是经过两次完全轮调后的图表(720):

为了避免这个问题,您可以使用另一个容器,如StackPane
StackPane root = new StackPane(box, greenLight);在你的情况下,Y中心将一直在237.03555297851562。
你也会解决你的第二个问题,因为盒子会出现在场景的中心。

注意,我已经将光线移到了负Z坐标(从屏幕中移出)。
greenLight.setTranslateZ(-300);此外,您还应该使用场景反别名:
Scene scene = new Scene(root, 500, 300, true, SceneAntialiasing.BALANCED);https://stackoverflow.com/questions/32273134
复制相似问题