2016-09-16 266 views
0

您好我想旋转文本(-40)/我有问题,当一个文本短这个开始对不同势hegh我想要的文本,当我开始标记。这PUNKT我喜欢这个了JavaFx旋转文本

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我这样做

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); 

当文本较长 enter image description here

+0

您使用画布绘制图片,但使用一些其他的窗格来添加名字???什么是您使用的布局(高达'Canvas'的共同祖先和你的文本元素的父) – fabian

+0

@fabian文本我有窗格和帆布我有不同势窗格,窗格都我添加到堆栈窗格 –

+0

什么你使用'StackPane'对齐吗? – fabian

回答

1

您可以使用Canvas绘制琴弦太:

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(); 
} 

或者不使用StackPane该中心的孩子们。也不要使用rotate属性,因为它周围Node的中心旋转。使用替代Rotate变换成围绕(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(); 
} 
+0

哇谢谢你 :-) !!现在我明白我什么时候犯错了 –