2017-10-21 66 views
2

我写了一个javafx对象“Ball”来创建一个Sphere。我现在试图让对象出现在我的主类中。 理想情况下,我会使用一个关键侦听器来创建/销毁球。但我甚至无法让球出现在屏幕上,甚至无法让我的1500x900屏幕出现。如何显示javafx 3D对象?

这里我为球代码:

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

} 

这是我为我的主类的代码,它应该创建球对象,并显示它们的动画。动画应该遵循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); 
    } 

} 

回答

3

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

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

你可以找到它显示一个球(地球)一些示例代码,在这里:

的示例演示了一些对上述观点的。