JavaFX、绘制平方函数


package com.example.javafxproject;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Point2D;
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 Function1 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() {
        Polyline polyline = new Polyline(); //函数曲线
        ObservableList<Double> list = polyline.getPoints();
        double scaleFactor = 0.0125;

        for (int x = -100; x <= 100; x++) {
            list.add(x + 200.0);    //函数添加点
            list.add(scaleFactor * x * x);
        }
        polyline.setRotate(180);    //倒转180°

        Line horizon = new Line(10, 125, 400, 125); //x轴
        Polyline horArrow = new Polyline(380, 115, 400, 125, 380, 135); //x轴箭头
        Text x = new Text(390, 110, "X");

        Line vertical = new Line(200, -30, 200, 170);   //y轴
        Polyline verArrow = new Polyline(190, -10, 200, -30, 210, -10); //y轴箭头
        Text y = new Text(210, -25, "Y");

        return new BorderPane(new Group(polyline, horizon, horArrow, x, vertical, verArrow, y));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值