JavaFX、绘制正弦和余弦函数


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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值