parsecer
parsecer

Reputation: 5106

JavaFX: position text inside Group at the bottom of shape

I have simple code that draws a circle and prints Hello World! text. Currently the text is inside the circle, while I want it to be below it. Both circle and text are inside a Group.

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class CircleArc  extends Application {
    @Override
    public void start(Stage applicationStage) {
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setFitToWidth(true);

        FlowPane flowPane = new FlowPane();
        flowPane.setOrientation(Orientation.HORIZONTAL);

        Circle circle = new Circle(100);
        circle.setFill(Color.GREEN);
        double rate = 0.6;
        Arc arc = new Arc(0, 0, 100, 100,
                0, -360*rate);
        arc.setType(ArcType.ROUND);
        arc.setFill(Color.GRAY);

        Text text = new Text("Hello World!");

        text.setLayoutX(text.getLayoutX() / 2);
        text.setLayoutY(text.getLayoutY());

        Group group = new Group();
        group.getChildren().add(circle);
        group.getChildren().add(arc);
        group.getChildren().add(text);

        flowPane.getChildren().add(group);

        scrollPane.setContent(flowPane);
        Scene scene = new Scene(scrollPane);
        applicationStage.setScene(scene);
        applicationStage.show();
    }


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

It currently looks like this:

enter image description here

However I want it to look like this:

enter image description here

I tried using the setLayout methods but they didn't work.

EDIT:

Turned out the simple

text.setLayoutX(text.getLayoutX() - 50);
text.setLayoutY(text.getLayoutY() + 120);

worked well enough.

Upvotes: 0

Views: 523

Answers (1)

swpalmer
swpalmer

Reputation: 4371

Use a Label. Set the circle as the graphic, position the text underneath.

public class Main extends Application {

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

    @Override
    public void start(Stage stage) {
        stage.setTitle("Centered Text Under Graphic");

        Circle circle = new Circle(100);
        circle.setFill(Color.GREEN);
        double rate = 0.6;
        Arc arc = new Arc(0, 0, 100, 100,
            0, -360 * rate);
        arc.setType(ArcType.ROUND);
        arc.setFill(Color.GRAY);
        Group circles = new Group(circle, arc);

        Label label = new Label("Hello World!", circles);
        label.setContentDisplay(ContentDisplay.TOP);
        Pane p = new Pane(label);
        Scene scene = new Scene(p);
        stage.setScene(scene);
        stage.show();
    }
}

That will handle keeping the text centered if it changes.

Upvotes: 2

Related Questions