Home > database >  Javafx Pendulum Animation - why can't i see my Circle on scene?
Javafx Pendulum Animation - why can't i see my Circle on scene?

Time:10-13

Java program. Using latest version of eclipse and java. Program works just as it should but I cant figure out why I cannot see the circle at the bottom of the pendulum. The program should show the pendulum as a line connecting a top and bottom circle. I've tried moving around the setFill() and setStroke() code and still can't figure this one out...probably something small and simple but I'm missing it. Let me know whats right in front of me please!

import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Ch15PA2 extends Application {
    
    public void start(Stage primaryStage) {
        
        BorderPane pane = new BorderPane();
        VBox vbox = new VBox();
        pane.getChildren().add(vbox);
        
        Pendulum pendulum = new Pendulum(500, 300);
        pane.setCenter(pendulum);   
        
        Scene scene = new Scene(pane,500,500);
        primaryStage.setTitle("Chapter 15 Programming Assignemnt 2");
        primaryStage.setScene(scene);
        pendulum.resume();
        primaryStage.show();
        
        scene.setOnKeyPressed(e -> {
            switch (e.getCode()) {
            case UP: pendulum.increase(); break;
            case DOWN: pendulum.decrease(); break;
            case R: pendulum.resume(); break;
            case S: pendulum.stop(); break;
            }
        });
    }

    public static void main(String[] args) {
        launch(args);
    }

}

class Pendulum extends Pane {
    private double w=300;
    private double h;
    
    PathTransition pt;
    Circle topCircle = new Circle();
    Circle bottomCircle = new Circle();
    Line line;
    Arc arc;
    
    public Pendulum(double width,double height) {
        w = width;
        h = height;
        
        arc = new Arc(w/2,h*.8,w*.15,w*.15,180,180);
        topCircle = new Circle(arc.getCenterX()-arc.getRadiusX(),arc.getCenterY(),10);
        bottomCircle = new Circle(arc.getCenterX(),arc.getCenterY()-h/2,bottomCircle.getRadius()/2);
        arc = new Arc(topCircle.getCenterX(),topCircle.getCenterY(),w/2,h/2,240,60);
        arc.setFill(Color.TRANSPARENT);
        line = new Line(topCircle.getCenterX(),topCircle.getCenterY(),bottomCircle.getCenterX(),bottomCircle.getCenterY());
        pt = new PathTransition();
        pt.setDuration(Duration.seconds(2.0));
        pt.setPath(arc);
        pt.setNode(bottomCircle);
        pt.setOrientation(PathTransition.OrientationType.NONE);
        pt.setCycleCount(PathTransition.INDEFINITE);
        pt.setAutoReverse(true);
        topCircle.setStroke(Color.BLACK);
        topCircle.setFill(Color.BLACK);
        bottomCircle.setStroke(Color.BLACK);
        bottomCircle.setFill(Color.BLACK);
        line.setStroke(Color.BLACK);
        line.endXProperty().bind(bottomCircle.translateXProperty().add(bottomCircle.getCenterX()));
        line.endYProperty().bind(bottomCircle.translateYProperty().add(bottomCircle.getCenterY()));

        getChildren().add(line);
        getChildren().add(bottomCircle);
        getChildren().add(topCircle);
        getChildren().add(arc);
        
    }
    
    public void increase() {
        pt.setRate(pt.getRate() 1);
    }
    
    public void decrease() {
        pt.setRate(pt.getRate()-1);
    }
    
    public void resume() {
        pt.play();
    }
    
    public void stop() {
        pt.stop();
    }
}

CodePudding user response:

You're setting the radius of bottomCircle to zero.

First you do:

Circle bottomCircle = new Circle();

which creates a circle with centerX=0, centerY=0, and (importantly) radius=0. Then in the constructor you do:

bottomCircle = new Circle(arc.getCenterX(),arc.getCenterY()-h/2,bottomCircle.getRadius()/2);

Since bottomCircle.getRadius() returns 0, this re-initializes bottomCircle to a circle with radius 0. Since the radius is zero, there is no actual circle to see.

Since your only constructor initializes both topCircle and bottomCircle, it really makes no sense to initialize them inline with their declarations (since you simply replace those objects immediately anyway). Just declare them as

Circle topCircle;
Circle bottomCircle;

This would give you a null pointer exception with your current code, alerting you to the fact that setting bottomCircle.radius to bottomCircle.radius/2 is almost certainly not what you want to do.

Generally, it's confusing to assign something a value and to then immediately assign it another value. You can just do something like

class Pendulum extends Pane {
    private double w;
    private double h;

    PathTransition pt;
    Circle topCircle;
    Circle bottomCircle;
    Line line;
    Arc arc;

    public Pendulum(double width,double height) {
        w = width;
        h = height;

//        arc = new Arc(w/2,h*.8,w*.15,w*.15,180,180);
        topCircle = new Circle(w/2-w*.15,h*.8,10);
        bottomCircle = new Circle(w/2,h*.8-h/2,5);
        arc = new Arc(topCircle.getCenterX(),topCircle.getCenterY(),w/2,h/2,240,60);
        arc.setFill(Color.TRANSPARENT);
        
        // ...

    }

    // ...
}
  • Related