![](https://i-blog.csdnimg.cn/blog_migrate/e21e384844dbc88e793dd302ad6e32c8.jpeg)
package com.example.javafxproject;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polyline;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Function2 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(getPane(), 500, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("正弦和余弦函数");
primaryStage.show();
}
private Pane getPane() {
double scaleFactor = 50;
Polyline sin = new Polyline(); //正弦
ObservableList<Double> sinList = sin.getPoints();
for (int x = -170; x <= 170; x++) {
sinList.add(x + 200.0); //正弦添加点
sinList.add(100 - 50 * Math.sin((x / 100.0) * 2 * Math.PI));
}
Polyline cos = new Polyline(); //余弦
ObservableList<Double> cosList = cos.getPoints();
for (int x = -170; x <= 170; x++) {
cosList.add(x + 200.0); //余弦添加点
cosList.add(100 - 50 * Math.cos((x / 100.0) * 2 * Math.PI));
}
Line horizon = new Line(-10, 100, 450, 100); //x轴
Polyline horPolyLine = new Polyline(430, 90, 450, 100, 430, 110); //x轴箭头
Text x = new Text(440, 80, "X");
Line vertical = new Line(200, -30, 200, 220); //y轴
Polyline verPolyLine = new Polyline(190, -10, 200, -30, 210, -10); //y轴箭头
Text y = new Text(220, -20, "Y");
Text _2pi = new Text(90, 110, "-2\u03c0"); //-2Π
Text _pi = new Text(140, 110, "-\u03c0"); //-Π
Text zero = new Text(200, 110, "0"); //0
Text pi = new Text(250, 110, "\u03c0"); //Π
Text pi2 = new Text(345, 110, "2\u03c0"); //2Π
return new BorderPane(new Group(sin, cos, horizon, horPolyLine, x,
vertical, verPolyLine, y, _2pi, _pi, zero, pi, pi2));
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/138814aab953917f7cbe2a1ca8d487ea.png)