If you are looking for JavaFX Example and landed here, you are at right place.Learn to create JavaFX Sine wave here.

This post covers an JavaFX example to Create Sine Wave.In order to understand this,Primary knowledge of JavaFX is must.In order to create sine wave,Cubic Curve is used.Its inbuilt library available with JavaFX. You just need  to import from package javafx.scene.shape. Once imported,create a curve with curve builder.Specify proper numeric values for curve and add then add that curve.When you build a curve,you need to specify start point,2 Control points,and End point.X and Y coordinate for all points.we increase X values for consecutive next points.Where as Y will be varying between fixed values(Here 50,150,250).

Here is source code for JavaFX Sine wave example :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package puretechy.javafxexample;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.CubicCurve;
import javafx.scene.shape.CubicCurveBuilder;
import javafx.util.Duration;
 
public class SineWaveUsingJavafx extends Application {
       @Override
       public void start(Stage primaryStage)
      {
              try
             {
                     BorderPane root = new BorderPane();
                     Scene scene = new Scene(root,200,300,Color.ROYALBLUE);
                     primaryStage.setTitle("www.puretechy.com");
                     primaryStage.setScene(scene);
                    Group g = new Group();
 
 
              CubicCurve cubicCurve = CubicCurveBuilder.create()
                       .startX(0).startY(150)          // start pt (x1,y1)
                       .controlX1(30).controlY1(50)   // control pt1
                       .controlX2(80).controlY2(250)  // control pt2
                       .endX(90).endY(150)             // end pt (x2,y2)
                       .fill(Color.WHITE)
                       .build();
 
              CubicCurve cubicCurve1 = CubicCurveBuilder.create()
                       .startX(90).startY(150)          // start pt (x1,y1)
                       .controlX1(120).controlY1(50)   // control pt1
                       .controlX2(170).controlY2(250)  // control pt2
                       .endX(200).endY(150)             // end pt (x2,y2)
                       .fill(Color.WHITE)
                       .build();
 
 
                     g.getChildren().add(cubicCurve);
                     g.getChildren().add(cubicCurve1);
 
                      root.getChildren().add(g);
                      primaryStage.show();
 
              } catch(Exception e) {
                     e.printStackTrace();
              }
       }      
       public static void main(String[] args) {
              launch(args);
       }
}

Hope this program  has helped you.!

Please Let us know your views about the Java Sine Wave program in comment section.and also let us know if any queries.Will try to resolve as soon as possible.:)

Stay tuned we are posting few more Javafx Example.

Hey wait,You might be interested in some more free learning.We promise completely free.We have listed so many free coupons for best Udemy courses.

Check out here.It may change your learning experience!

Keep Learning !

1
 

About

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website

%d bloggers like this: