0

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

}
Elliander
  • 503
  • 1
  • 9
  • 19
  • Actual sine curves have no circular parts. If you want a facsimile of one, you'll need to use `Math.sin()` and have some coordinate system for reference and plot individual points `(x, Math.sin(x))` for x from at least 0 to 2pi. [Here](http://www.coderanch.com/how-to/java/PlotSineCurve) is a place to start. [Another](http://stackoverflow.com/questions/19914476/plot-the-sine-and-cosine-functions) – DSlomer64 Oct 23 '15 at 23:47
  • Quite similar to: [Draw Cartesian Plane Graphi with canvas in JavaFX](http://stackoverflow.com/questions/24005247/draw-cartesian-plane-graphi-with-canvas-in-javafx). – jewelsea Oct 23 '15 at 23:49
  • I need to use Path transition though. Have several smaller shapes won't really help, and one of those examples don't even use javafx shapes. – Elliander Oct 24 '15 at 00:06
  • I thought of a potential work around: I could build a series of connected circles. I would just need to find a way to get the moving object to change from one circle to the next moving the opposite direction. – Elliander Oct 24 '15 at 00:23
  • I took a look at your example, but Sinusoid cannot be converted to shape. – Elliander Oct 24 '15 at 01:41
  • Why do you need to use a `PathTransition`? Wouldn't another `Animation` work? (I'm not sure I understand what you're trying to do though.) – James_D Oct 25 '15 at 00:11
  • See if https://gist.github.com/james-d/d6611c60c25a4f078dfd helps. – James_D Oct 25 '15 at 00:53

0 Answers0