-2

I want to know if it is possible to add a text inside a JavaFX Polyline component that will follow the line when doing transformations such as rotations. I am opened to any ideas :)

enter image description here

Yasiz
  • 13
  • 4

1 Answers1

0

javafx.scene.shape.Shape is not a Parent. It cannot contain children(Text) within it. So if your question is about handling the shape and text together for doing some transformations (translate., rotate..etc), I think the easiest way would be to wrap the both in a Pane (preferbly StackPane) and do all transformations on Pane instead of Shape & Text individually.

Below is a quick sample demo of what I mean:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Polyline;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class PolylineExample extends Application {
    @Override
    public void start(Stage stage) {
        Polyline polyline = new Polyline();
        polyline.getPoints().addAll(new Double[]{
                200.0, 50.0,
                400.0, 50.0,
                450.0, 150.0,
                400.0, 250.0,
                200.0, 250.0,
                150.0, 150.0,
        });

        Text text = new Text("Some Text");
        StackPane container = new StackPane(polyline, text);

        // DOING ALL TRANSFORMATIONS ON CONTAINER
        container.setTranslateX(20);
        container.setTranslateY(50);
        container.setRotate(30);

        StackPane root = new StackPane(new Group(container));
        Scene scene = new Scene(root, 600, 300);
        stage.setTitle("Drawing a Polyline");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String args[]) {
        launch(args);
    }
}
Sai Dandem
  • 8,229
  • 11
  • 26
  • rather, I am looking for the text to be written next to polyline and for the text to be displayed as a polyline schema eg if we have a straight polyline the text will be straight, if polyline diagnole, the text will be diagonal – Yasiz Nov 19 '19 at 20:10
  • 1
    Can you provide the desired outcome screenshot? – Sai Dandem Nov 19 '19 at 21:27
  • i put an example in descrption of question. – Yasiz Nov 20 '19 at 22:13
  • 1
    Ok got that... did you gave a look at this [solution](https://stackoverflow.com/questions/17300457/how-to-write-text-along-a-bezier-curve)? – Sai Dandem Nov 21 '19 at 21:17
  • yes, but in my programm i work with polyline not curve – Yasiz Nov 21 '19 at 22:20