2013-07-02 53 views

回答

13

放在一個StackPane圓和文本,設置文本邊界類型的計算視覺:

Circle circle = new Circle(); 
Text text = new Text("42"); 
text.setBoundsType(TextBoundsType.VISUAL); 
StackPane stack = new StackPane(); 
stack.getChildren().add(circle, text); 

您可以使用一個時間表來改變文本的價值在圈子裏。

下面是一個完整的例子:

import javafx.animation.*; 
import javafx.application.Application; 
import javafx.event.*; 
import javafx.scene.*; 
import javafx.scene.layout.StackPane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.*; 
import javafx.scene.text.*; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class TextInCircle extends Application { 
    public static void main(String[] args) throws Exception { launch(args); } 

    private static final int R = 150; 
    private static final Color lineColor = Color.FIREBRICK.deriveColor(0, 1, 1, .6); 

    @Override 
    public void start(final Stage stage) throws Exception { 
     final Circle circle = createCircle(); 
     final Text text = createText(); 

     final Line l1 = createLine(lineColor, 0, R - 0.5, 2 * R, R - 0.5); 
     final Line l2 = createLine(lineColor, R - 0.5, 0, R - 0.5, 2 * R); 

//  Group group = new Group(circle, text, l1 , l2); 
     Group group = new Group(circle, l1 , l2); 

     StackPane stack = new StackPane(); 
     stack.getChildren().addAll(group, text); 

     stage.setScene(new Scene(stack)); 
     stage.show(); 

     animateText(text); 
    } 

    private Circle createCircle() { 
     final Circle circle = new Circle(R); 

     circle.setStroke(Color.FORESTGREEN); 
     circle.setStrokeWidth(10); 
     circle.setStrokeType(StrokeType.INSIDE); 
     circle.setFill(Color.AZURE); 
     circle.relocate(0, 0); 

     return circle; 
    } 

    private Line createLine(Color lineColor, double x1, double y1, double x2, double y2) { 
     Line l1 = new Line(x1, y1, x2, y2); 

     l1.setStroke(lineColor); 
     l1.setStrokeWidth(1); 

     return l1; 
    } 

    private Text createText() { 
     final Text text = new Text("A"); 

     text.setFont(new Font(30)); 
     text.setBoundsType(TextBoundsType.VISUAL); 
//  centerText(text); 

     return text; 
    } 

    private void centerText(Text text) { 
     double W = text.getBoundsInLocal().getWidth(); 
     double H = text.getBoundsInLocal().getHeight(); 
     text.relocate(R - W/2, R - H/2); 
    } 

    private void animateText(final Text text) { 
     Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() { 
      @Override public void handle(ActionEvent actionEvent) { 
       char newValue = (char) ((text.getText().toCharArray()[0] + 1) % 123); 
       if (newValue == 0) newValue = 'A'; 
       text.setText("" + newValue); 
//    centerText(text); 
      } 
     })); 
     timeline.setCycleCount(1000); 
     timeline.play(); 
    } 
} 

centeredtext

註釋掉的代碼還包括手動放置文字,如果你喜歡這樣做,而不是使用StackPane代碼。

+0

這正是我想要的!另一方面,我有42個圓圈,所以我必須創建42個堆疊窗格和文本對象。它看起來像一個愚蠢的問題,但我問,因爲我不想處理凌亂的代碼:)。但謝謝你的有用答案。 – quartaela

+0

好吧,我想我解決了我的最後一個問題。我將使用SceneBuilder手動將42個堆疊窗格放置在指定位置。 – quartaela