将另一个形状上绘制的形状设置为Invisible

时间:2017-04-05 22:12:57

标签: javafx javafx-8

我想绘制一条穿过圆圈的线条。但是,我不希望在圆圈内显示该线条。我怎么能做到这一点?请注意,我首先绘制圆圈,然后绘制直线。

我使用了以下几个方面:

  1. Circle.setOpacity为1,没有帮助!
  2. 在同一组中添加圆和线后使用line.toBack()。这没有任何帮助

1 个答案:

答案 0 :(得分:1)

line.toBack()

back

line.toFront()

front

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;

public class LineUnderCircle extends Application {    
    @Override
    public void start(Stage stage) throws Exception {
        Line line = new Line(10, 10, 50, 50);
        line.setStrokeWidth(3);

        Circle left  = new Circle(10, 10, 8, Color.FORESTGREEN);
        Circle right = new Circle(50, 50, 8, Color.FIREBRICK);

        Button lineToBack = new Button("Line to back");
        lineToBack.setOnAction(e -> line.toBack());
        Button lineToFront = new Button("Line to front");
        lineToFront.setOnAction(e -> line.toFront());

        Pane shapePane = new Pane(line, left, right);

        HBox controlPane = new HBox(10, lineToBack, lineToFront);

        VBox layout = new VBox( 10, controlPane, shapePane);
        layout.setPadding(new Insets(10));

        stage.setScene(new Scene(layout));
        stage.show();
    }

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