如何显示 javafx 3D 对象?

How to show a javafx 3D object?

我写了一个 javafx 对象 "Ball" 来创建一个 Sphere。我现在正试图让该对象出现在我的主 class 中。 理想情况下,我会使用 create/destroy 球的关键监听器。但是我什至无法让球出现在屏幕上,甚至根本无法让我的 1500x900 屏幕出现。

这是我的球代码:

// ball object
package bouncingballs;

import javafx.animation.Interpolator;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.scene.layout.Pane;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Sphere;
import javafx.util.Duration;
import static javafx.util.Duration.seconds;

public class Ball extends Pane {
    //Create 3D ball
    private Sphere ball;
    private Double radius;
    private PhongMaterial color;
    private Polygon poly;

    private PathTransition path;
    private Integer speed;
    //Create path and animate ball in constructor
    public Ball(Double radius, PhongMaterial color, Polygon poly) {
        this.radius = radius;
        this.color = color;
        ball.setRadius(radius);
        ball.setMaterial(color);
        this.poly = poly;
        speed = 10;
        path.setPath(poly);
        path.setNode(ball);             
        path.setInterpolator(Interpolator.LINEAR);
        path.setDuration(Duration.seconds(speed));
        path.setCycleCount(Timeline.INDEFINITE);
        path.play();

    }

    //some test accessors/mutators
    public void setRadius(Double radius) {
        this.radius = radius;
    }

    public Double getRadius() {
        return radius;
    }

}

这是我的主要代码 class,它应该创建 Ball 对象并以动画方式显示它们。动画应该跟随多边形对象多边形来模拟弹跳球。

//main object to show Balls to screen
package bouncingballs;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;

public class BouncingBalls extends Application {
    @Override
    public void start(Stage primaryStage) {

        //create path to simulate bouncing ball
        Polygon poly = new Polygon(750, 850, 50, 675, 500, 50, 750, 850, 1000, 50, 1450, 675);//creates path to simulate bouncing ball on 1500x900 screen
        Double radius = 50.0;
        PhongMaterial color = new PhongMaterial();
        color.setDiffuseColor(Color.OLIVE);
        Ball ball = new Ball(radius, color, poly);
        StackPane root = new StackPane();
        root.getChildren().add(ball);
        Scene scene = new Scene(root, 1500, 900);
        primaryStage.setTitle("Bouncing Balls");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

 public static void main(String[] args) 
    {launch(args);
    }

}

您有一堆错误或其他奇怪的事情:

  1. 您的 Ball class 创建了一个 Sphere,但您从未将该 Sphere 添加到场景图中(因此它永远不会被看到)。
  2. 您的 Ball class 扩展了 Pane,这很奇怪,因为 Ball 并不是真正的 Pane。如果你要扩展任何东西,可能 Sphere 是最好的。
  3. 您使用 StackPane 作为您的根目录。对于 3D 图形,这样做可能不是最好的,因为 Pane subclasses 确实是为布置 2D 系统而设计的。对于 3D,您可能只想坚持使用基本组作为容器。
  4. 当你有一个 3D 场景时,你可能想使用打开深度缓冲的构造函数。
  5. 对于 3D 作品,您想在场景中设置一个 PerspectiveCamera。
  6. 您可能需要在场景中使用一些照明。 JavaFX 将添加一些默认照明,但它可能不符合您的需要。
  7. 您应该检查 Scene3D ConditionalFeature 以查看您的平台是否支持 3D。
  8. 您可能想要适当地设置球体的 Z 坐标,并确保它位于透视相机的视野范围内。

您可以在此处找到一些显示 Sphere(地球)的示例代码:

  • JavaFX material's bump and spec maps

该示例演示了上面提出的一些要点。