不知道怎么改了,题目要求在原先橡皮绳的基础上,让颜色选择器显示在窗口的左上角。用颜色选择器的值确定要绘制的下一行的颜色。增加清除功能,可以清除所画的线,再接着重新画线
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(...);
}
}
在这种基本实现的基础上,我们可以进行以下几点改进: