Drawing Sin and Cosine graphs in Javafx

2019-09-02 09:38发布

问题:

I need to draw a Sine wave and a Cosine wave separately, which I will then use as class objects for a larger program. (these lines are just for plotting a path) The problem I am running into is simply that - while I know how to draw basic shapes like a circle, an arc, or a polygon - I don't know how to arrange these into a graph.

I tried looking up tutorials and examples, but the only javafx examples I can find use depreciated features or don't use javafx at all.

I thought about just drawing partial arcs and manually adjusting the coordinates, but since the coordinates correspond with a graph I figure there must be a simple way to plot it.


EDIT:

I have here a simple program: It displays 6 circles and has a red ball in orbit around the first circle. What I ultimately would like to do is have the ball give the appearance of moving in either a sin or cosine wave (at which point the circle paths will be invisible) the problem is that I don't know how to get the ball to jump to the next circle and change directions.

Ideally, if I could find some way to detect the coordinate of where the ball is located and have the ball appear somewhere else instead that would allow me to correct the above problem, allow me to make sure the ball starts from the left most side, and also allow me to have to ball start over when it reaches the end.

 package orbitingCircle;

 import javafx.animation.Interpolator;
 import javafx.animation.PathTransition;
 import javafx.animation.Timeline;
 import javafx.application.Application;
 import javafx.scene.Scene;
 import javafx.scene.layout.Pane;
 import javafx.scene.shape.Circle;
 import javafx.stage.Stage;
 import javafx.util.Duration;

 public class Main extends Application 
 {

@Override
public void start(Stage primaryStage) {
    Pane p = new Pane();

    Circle circlePath = new Circle(50, 100, 50);
    Circle circlePath2 = new Circle(150, 100, 50);
    Circle circlePath3 = new Circle(250, 100, 50);
    Circle circlePath4 = new Circle(350, 100, 50);
    Circle circlePath5 = new Circle(450, 100, 50);
    Circle circlePath6 = new Circle(550, 100, 50);
    Circle orbitingDot = new Circle(100, 50, 5);

    circlePath.setStyle("-fx-stroke: black; -fx-fill: white");
    circlePath2.setStyle("-fx-stroke: black; -fx-fill: white");
    circlePath3.setStyle("-fx-stroke: black; -fx-fill: white");
    circlePath4.setStyle("-fx-stroke: black; -fx-fill: white");
    circlePath5.setStyle("-fx-stroke: black; -fx-fill: white");
    circlePath6.setStyle("-fx-stroke: black; -fx-fill: white");
    orbitingDot.setStyle("-fx-stroke: red; -fx-fill: red");

    // Add nodes to the pane
    p.getChildren().addAll(circlePath, circlePath2, circlePath3, circlePath4, circlePath5, circlePath6, orbitingDot);

    // Create the path transition
    PathTransition pt = new PathTransition(Duration.millis(4000), circlePath, orbitingDot);
    pt.setInterpolator(Interpolator.LINEAR);
    pt.setOrientation(PathTransition.OrientationType.NONE);
    pt.setCycleCount(Timeline.INDEFINITE);
    pt.setAutoReverse(false);
    pt.play();

    p.setOnMousePressed(e -> pt.pause());
    p.setOnMouseReleased(e -> pt.play());

    primaryStage.setTitle(" ");
    primaryStage.setScene(new Scene(p, 600, 175));
    primaryStage.show();
}

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

}