Animation 如何显示JavaFX3D对象?

Animation 如何显示JavaFX3D对象?,animation,javafx,3d,shapes,Animation,Javafx,3d,Shapes,我编写了一个javafx对象“Ball”来创建一个球体。我现在试图使对象出现在我的主类中。 理想情况下,我会使用一个键侦听器来创建/销毁球。但我甚至不能让球出现在屏幕上,甚至不能让我的1500x900屏幕出现 下面是我的球代码: // ball object package bouncingballs; import javafx.animation.Interpolator; import javafx.animation.PathTransition; import javafx.anim

我编写了一个javafx对象“Ball”来创建一个球体。我现在试图使对象出现在我的主类中。 理想情况下,我会使用一个键侦听器来创建/销毁球。但我甚至不能让球出现在屏幕上,甚至不能让我的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;
    }

}
这是我的主类代码,它应该创建球对象并显示动画。动画应跟随多边形对象多边形以模拟反弹球

//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);
    }

}

你有一大堆错误或奇怪的事情:

  • 您的Ball类创建了一个球体,但您从未将该球体添加到场景图中(因此永远无法看到它)
  • 您的Ball类扩展了Pane,这很奇怪,因为Ball并不是真正的窗格。若你们要扩展任何东西,那个么球体可能是最好的
  • 您可以对根目录使用StackPane。对于3D图形来说,这样做可能不是最好的,因为Pane子类实际上是为布局2D系统而设计的。对于3D,您可能只希望将基本组作为容器
  • 当您有一个3D场景时,您可能希望使用打开深度缓冲的构造函数
  • 对于三维工作,需要在场景中设置透视摄影机
  • 您可能需要在场景中使用一些照明。JavaFX将添加一些默认照明,但它可能与您需要的不匹配
  • 您应该检查Scene3D条件功能,以查看您的平台是否支持3D
  • 您可能希望适当设置球体的Z坐标,并确保它位于透视摄影机的视野内
  • 您可以在下面找到一些显示球体(地球)的示例代码:

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