J'ai écrit un objet javafx "Ball" pour créer une sphère. J'essaie maintenant de faire apparaître l'objet dans ma classe principale. Idéalement, j'utiliserais un écouteur clé pour créer/détruire des balles. Mais je ne peux même pas faire apparaître les boules sur l'écran, ni même faire apparaître mon écran 1500x900.Comment afficher un objet 3D javafx?
Voici mon code pour la balle:
// 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;
}
}
Voici mon code pour ma classe principale, il faut créer des objets de balle et de les afficher d'animation. L'animation doit suivre l'objet Polygon poly pour simuler une balle rebondissante.
//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);
}
}