Ulkurz
Ulkurz

Reputation: 83

Set a shape drawn on another shape as Invisible

I want to draw a line passing through a circle. However, I do not want the line to be shown while its inside the circle. How can I accomplish this? Note that I'm drawing the circle first and then the line.

I used a couple of things like:

  1. Circle.setOpacity to 1, which didn't help!
  2. Used line.toBack() after adding the circle and line in the same group. This didnt help either

Upvotes: 0

Views: 366

Answers (1)

jewelsea
jewelsea

Reputation: 159290

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);
    }
}

Upvotes: 1

Related Questions