2

I wrote a javafx object "Ball" to create a Sphere. I'm now trying to make the object appear in my main class. Ideally, I would use a key listener to create/destroy balls. But I can't even get the balls to appear on the screen, or even make my 1500x900 screen appear at all.

Here my code for the ball:

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

}

Here is my code for my main class, it should create Ball objects and display them animated. The animation should follow the Polygon object poly to simulate a bouncing 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);
    }

}
jewelsea
  • 150,031
  • 14
  • 366
  • 406

1 Answers1

3

You have a bunch of errors or otherwise weird things:

  1. Your Ball class creates a Sphere, but you never add that Sphere to the scene graph (so it could never be seen).
  2. Your Ball class extends Pane, which is weird because a Ball isn't really a Pane. If you are to extend anything, probably Sphere would be best.
  3. You use a StackPane for your root. It is probably not best to do that for 3D graphics, as the Pane subclasses are really designed for laying out 2D systems. For 3D, you probably just want to stick to basic Groups as containers.
  4. When you have a 3D scene, you probably want to use the constructor which switches depth buffering on.
  5. For 3D work, you want to set a PerspectiveCamera on the scene.
  6. You probably want some lighting on your scene. JavaFX will add some default lighting, but it may not match what you need.
  7. You should check the Scene3D ConditionalFeature to see if 3D is supported on your platform.
  8. You probably want to set the Z co-ordinates of your Sphere appropriately and ensure that it is something which will lie within your perspective camera's field of view.

You can find some sample code which displays a Sphere (the earth), here:

The sample demonstrates some of the points made above.

jewelsea
  • 150,031
  • 14
  • 366
  • 406