Kkk.
Kkk.

Reputation: 1

JavaFx rotate text

Hi I want to rotate text (-40)/ And I Have problem when a text is short this start on diffrent hegh I Want to that text start when I mark. This punkt I do like this

for(int i=0; i<=Config.busSizeInView; i++){
            gc.drawImage(busStop, 10+i*Config.xSize/Config.busSizeInView, Config.ySize -100, 20, 20);
        }

enter image description here I do this

for(int i =0 ;i<Config.busSizeInView;i++){
            nameBusStop.add(new Text("Konstytucji 3 Maja - Dworzec PKS 02"));
        }
for(int i=0 ; i<nameBusStop.size(); i++){
        nameBusStop.get(i).setRotate(-40);
        nameBusStop.get(i).setText(Main3.listBusStops.get(i).getName());
        }
for(int i =0 ; i<nameBusStop.size(); i++){
            nameBusStop.get(i).relocate(i*Config.xSize/Config.busSizeInView-Config.padding-10, Config.ySize - Config.ySize/6  - Config.padding*3);
        }   





line.getChildren().addAll(canvas,txtPane);
Pane txtPane = new Pane();
        for(Text text : nameBusStop){
            text.setFont(Font.font ("Verdana", 20));
            txtPane.getChildren().add(text);
        }
  line.getChildren().addAll(canvas,txtPane);

When a text is longer enter image description here

Upvotes: 0

Views: 3965

Answers (1)

fabian
fabian

Reputation: 82461

You could use the Canvas to draw the strings too:

public static void drawStop(double x, double y, String text, GraphicsContext gc) {
    gc.save();

    gc.translate(x, y);
    gc.fillRect(-5, 0, 10, 10);
    gc.rotate(-40);
    gc.fillText(text, 5, 0);

    gc.restore();
}

@Override
public void start(Stage primaryStage) {
    Canvas canvas = new Canvas(900, 400);
    GraphicsContext gc = canvas.getGraphicsContext2D();

    gc.setFont(Font.font ("Verdana", 20));

    drawStop(100, 380, "Stop 1", gc);
    drawStop(200, 380, "Stop 2", gc);
    drawStop(500, 380, "Stop 3 Stop 3 Stop 3 Stop 3 Stop 3 Stop 3", gc);

    Scene scene = new Scene(new Group(canvas));

    primaryStage.setScene(scene);
    primaryStage.show();
}

Alternatively don't use a StackPane which centers the children. Also don't use the rotate property, since it rotates around the center of a Node. Use a Rotate transform instead to rotate around (0, 0):

public static void drawStop(double x, double y, String text, GraphicsContext gc, Pane pane) {
    gc.fillRect(x-5, y, 10, 10);
    Text textNode = new Text(text);
    textNode.setFont(Font.font ("Verdana", 20));
    textNode.setBoundsType(TextBoundsType.VISUAL);
    textNode.relocate(x, y-15);
    textNode.getTransforms().add(new Rotate(-40));
    pane.getChildren().add(textNode);
}

@Override
public void start(Stage primaryStage) {
    Pane pane = new Pane();
    Canvas canvas = new Canvas(900, 400);
    pane.getChildren().add(canvas);

    GraphicsContext gc = canvas.getGraphicsContext2D();

    drawStop(100, 380, "Stop 1", gc, pane);
    drawStop(200, 380, "Stop 2", gc, pane);
    drawStop(500, 380, "Stop 3 Stop 3 Stop 3 Stop 3 Stop 3 Stop 3", gc, pane);

    Scene scene = new Scene(pane);

    primaryStage.setScene(scene);
    primaryStage.show();
}

Upvotes: 1

Related Questions