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));
}
}
JavaFX、绘制平方函数
最新推荐文章于 2022-07-20 21:35:32 发布