如何显示javafx 3D对象?

时间:2017-10-21 07:06:54

标签: animation javafx 3d shapes

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

}

这是我的主类的代码,它应该创建Ball对象并显示它们的动画。动画应遵循Polygon对象poly来模拟弹跳球。

//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 个答案:

答案 0 :(得分:3)

你有很多错误或其他奇怪的事情:

  1. 您的Ball类会创建一个Sphere,但您永远不会将该Sphere添加到场景图中(因此永远不会看到它)。
  2. 你的Ball类扩展了Pane,这很奇怪,因为Ball并不是一个真正的Pane。如果你要扩展任何东西,可能Sphere会是最好的。
  3. 您为根使用StackPane。对于3D图形来说,这可能不是最好的,因为Pane子类实际上是为布局2D系统而设计的。对于3D,您可能只想坚持将基本组作为容器。
  4. 当您有3D场景时,您可能想要使用切换深度缓冲的构造函数。
  5. 对于3D工作,您希望在场景中设置PerspectiveCamera。
  6. 您可能想在场景中使用一些灯光。 JavaFX将添加一些默认光照,但它可能与您需要的不匹配。
  7. 您应该检查Scene3D ConditionalFeature以查看您的平台是否支持3D。
  8. 你可能想要适当地设置你的球体的Z坐标,并确保它在你的透视相机的视野范围内。
  9. 您可以在这里找到一些显示球体(地球)的示例代码:

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