Peter Penzov
Peter Penzov

Reputation: 1584

Insert text at the center of a image

I have this code which inserts Image into the center of a borderpane:

private static final ImageView iv;    
    static {
        iv = new ImageView(StartPanel.class.getResource("/com/dx57dc/images/6.jpg").toExternalForm());
    }

bpi.setCenter(iv);

And now I have this problem. I have this insert text as a label at the center of the image.

Text inftx = new Text("Infrastructure");

How I can do this?

Upvotes: 3

Views: 8611

Answers (1)

Crferreira
Crferreira

Reputation: 1238

You can use a StackPane to performed the desired image overlap. The following code can be used on your example:

private static final ImageView iv;    

static {
    iv = new ImageView(StartPanel.class.getResource("/com/dx57dc/images/6.jpg").toExternalForm());
    Text inftx = new Text("Infrastructure");
    StackPane pane = new StackPane();

    pane.getChildren().add(iv);
    pane.getChildren().add(inftx);

    pane.setAlignment(Pos.CENTER);
}

As informed on StackPane tutorial:

The StackPane layout pane places all of the nodes within a single stack with each new node added on top of the previous node. This layout model provides an easy way to overlay text on a shape or image or to overlap common shapes to create a complex shape.

Upvotes: 6

Related Questions