java橡皮绳画线基础上的改进

不知道怎么改了,题目要求在原先橡皮绳的基础上,让颜色选择器显示在窗口的左上角。用颜色选择器的值确定要绘制的下一行的颜色。增加清除功能,可以清除所画的线,再接着重新画线

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.layout.VBox;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.StackPane;

public class RubberLines2 extends Application
{
    private Line currentLine;
    private ColorPicker colorPicker;
    private Group root;
    private Group group2;
    private RadioButton clearButton;
    public Scene scene;

    public void start(Stage primaryStage)
    {
        colorPicker = new ColorPicker(Color.BLACK);
        colorPicker.setOnAction(this::processColorChoice);
        
        Scene scene = new Scene(root, 500, 300, Color.BLACK);       
        scene.setOnMousePressed(this::processMousePress);
        scene.setOnMouseDragged(this::processMouseDrag);
      
        ToggleGroup group = new ToggleGroup();
        clearButton = new RadioButton("clear");
        clearButton.setSelected();
        clearButton.setToggleGroup(group);
        clearButton.setOnAction(this::processRadioButtonAction);       
        
        VBox pickers = new VBox(colorPicker, clearButton);
        pickers.setSpacing(20);
        pickers.setAlignment(Pos.BASELINE_LEFT);//左上角
        
            
        root = new Group();
        group2 = new Group();
        root.getChildren().add(currentLine);
        group2.getChildren().addAll(root, pickers);

        primaryStage.setTitle("Rubber Lines");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    public void processMousePress(MouseEvent event)
    {
        currentLine = new Line(event.getX(), event.getY(), event.getX(), event.getY());
        currentLine.setStroke(Color.CYAN);
        currentLine.setStrokeWidth(3);
    }

    public void processMouseDrag(MouseEvent event)
    {
        currentLine.setEndX(event.getX());
        currentLine.setEndY(event.getY());
    }
    
     public void processColorChoice(ActionEvent event)
    {
        currentLine.setFill(colorPicker.getValue());
    }
    
    public void processRadioButtonAction(ActionEvent event)
    {
        if (clearButton.isSelected())
            root.getChildren().clear();
    }

    public static void main(String[] args)
    {
        launch(args);
    }
}

以下答案由GPT-4.0Plus大模型与博主A努力中.......共同编写:
橡皮绳画线(Catmull-Rom spline)是一种平滑的曲线绘制算法,常用于生成自然的曲线路径。在Java中,我们可以这样实现基本的橡皮绳画线:

java
public class CatmullRomSpline {
    private Point[] points;  // 控制点数组
    
    public CatmullRomSpline(Point... points) {
        this.points = points; 
    }
    
    public List<Point> getCurvePoints() {
        List<Point> curvePoints = new ArrayList<>();
        for (int i = 0; i < points.length - 3; i++) {
            // 4个控制点
            Point p0 = points[i];
            Point p1 = points[i + 1];
            Point p2 = points[i + 2];
            Point p3 = points[i + 3];
            
            // 计算中间点
            curvePoints.add(calculateMiddlePoint(p0, p1, p2, p3)); 
        }
        return curvePoints;
    }
    
    private Point calculateMiddlePoint(Point p0, Point p1, Point p2, Point p3) {
        // 知识点:橡皮绳曲线的中点计算公式
        return new Point(...);  
    }
}

在这种基本实现的基础上,我们可以进行以下几点改进:

  1. 增加控制点数量
    我们可以允许用户传入更多的控制点,而不仅限于4个,生成更加平滑和自由的曲线。
  2. 调整拉力
    添加一个tension参数来控制曲线的张力,值越大曲线越平直,值越小曲线越富有变化。
  3. 添加闭合选项
    新增一个closed参数,当值为true时,会自动将首尾两个控制点相连,生成闭合的曲线。
  4. 支持3D空间中的曲线
    除了2D平面上的点之外,我们也可以传入3D空间中的点(使用Point3D类),生成3D曲线。
  5. 优化中间点计算
    我们可以使用更高级的算法优化中间点的计算,使得曲线看上去更加平滑流畅。