有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何剪裁线条?

以下是我的代码:

 public void start(Stage primaryStage) throws Exception {
    Pane pane = new Pane();
    Scene scene = new Scene(pane, 500, 500);
    Line line = new Line(0, 200, 500, 200);

    line.setStrokeWidth(2);
    line.setStroke(Color.RED);
    pane.getChildren().add(line);
    primaryStage.setScene(scene);
    primaryStage.show();

}

它输出一条线,但我想剪辑这条线。例如:如果我有一条从(0200)开始到(500200)结束的线,那么我想将它从(200200)剪辑到(400200)。 我有没有办法把线剪断?感谢您的帮助!多谢各位


共 (2) 个答案

  1. # 1 楼答案

    我在setOnMouseClicked侦听器中使用了一个setEndX来演示这一点。你可能需要做一些数学运算,同时使用setEndXsetEndY来获得你想要的结果

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Line;
    import javafx.stage.Stage;
    
    
    public class LineChartSample extends Application {
    
        @Override public void start(Stage stage) {
            Pane pane = new Pane();
            Scene scene = new Scene(pane, 500, 500);
            Line line = new Line(0, 200, 500, 200);
    
            line.setStrokeWidth(2);
            line.setStroke(Color.RED);
            pane.getChildren().add(line);
    
    //        Rectangle clipRect = new Rectangle(line.getBoundsInParent().getWidth(), line.getBoundsInParent().getHeight());
    //        line.setClip(clipRect);
    
            line.setOnMouseClicked((event)->{
                line.setEndX(line.getBoundsInLocal().getWidth() - 100);
            });
    
            stage.setWidth(700);
            stage.setScene(scene);
            stage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    
  2. # 2 楼答案

    如果剪辑真的是你想要做的(你没有告诉我们你真正的用例),我仍然倾向于使用一个解决方案,塞德里克已经在他的代码中展示了这个解决方案,但是出于某种原因注释掉了。每个形状都有一个setClip方法,为什么不使用它呢

    import javafx.application.Application;
    import javafx.geometry.Bounds;
    import javafx.scene.Scene;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Line;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;
    
    
    public class LineChartSample extends Application {
    
        int clickCount = 0;
    
        @Override public void start(Stage stage) {
            Pane pane = new Pane();
            Scene scene = new Scene(pane, 500, 500);
            Line line = new Line(0, 200, 500, 200);
    
            line.setStrokeWidth(2);
            line.setStroke(Color.RED);
    
            Bounds b = line.getBoundsInParent();
            System.out.println(b);
    
            pane.getChildren().add(line);
    
            pane.setOnMouseClicked((event)->{
                ++clickCount;
                double d = clickCount*20.0;
                Rectangle clipRect = new Rectangle(b.getMinX() + d, b.getMinY(), b.getWidth() - 2*d, b.getHeight());
                line.setClip(clipRect);
            });
    
            stage.setWidth(700);
            stage.setScene(scene);
            stage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }