Insert text at the center of a image


Question

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?

1
4
8/10/2015 11:42:35 AM

Accepted Answer

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.

5
8/12/2013 11:42:15 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon