2017-01-19 3 views
0

キャンバスの右中央にストロークサークル(d = 26)を作成しました。 デフォルトのフォントサイズは、グラフィックコンテキストの12pt(PIXCELではありません)です。 私は上記で作成した円の中心の1桁または2桁の数字を書きたいと思います。あなたは私に円の真ん中のテキストを書く数学的表現を教えてもらえますか?私はまた、ピクセルと12pt(POINTS)をどのようにマッチさせるか理解していないことに苦しんでいます。誰でも助けてくれますか?あなたの両方のためのjavaFXキャンバスに描画された円の中心をテキストにする方法は?

@Override 
public void start(Stage primaryStage) { 
    Pane root = new Pane(); 

    Scene scene = new Scene(root, 300, 300); 
    primaryStage.setTitle("My Canvas"); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 

    Canvas cvs = new Canvas(); 
    cvs.setWidth(300); 
    cvs.setHeight(300); 
    cvs.setLayoutX(0); 
    cvs.setLayoutY(0); 
    root.getChildren().add(cvs); 
    GraphicsContext gc = cvs.getGraphicsContext2D(); 

    gc.setFill(Color.BLACK); 
    gc.setStroke(Color.BLUE); 
    gc.setLineWidth(2); 

    // creating circle (diameter = 26) right center of the canvas(300 x 300) 
    // So, x = (widthOfCanvas-diameter)/2 , y = ((widthOfCanvas-diameter)/2 
    double d = 26.0d; 
    double x = (cvs.getWidth() - d)/2; 
    double y = (cvs.getHeight() - d)/2; 
    gc.strokeOval(x, y, d, d); 

    // default font size is 12pt (NOT PIXCEL) 
    gc.fillText("26", 150, 150); // How can I set this text right center of the circle? Please give me som mathematical expression 
    // I also don't know relationship betwwen pt and px 
    // What can I do? 
} 
+2

たとえば、 'Label'と適切なレイアウト(' StackPane')を使用してみませんか? –

+1

はい:本当にキャンバスが必要ですか?これはレイアウトの問題です。@BoHalimが正しくレイアウトされていることが、レイアウトを使って解決されていることが正しく指摘されています。 (実際には、ラベルとラベルのCSS設定をスタック・ペインとサークルの必要なしで行うこともできます)。*キャンバスを使用する*場合は、はるかに難しくなります。 –

+1

実際には、私は何らかの描画アプリケーションを構築しています。上記の丸数字を使用して図の中に部品を番号付けする方法を理解する必要があります。しかし、私は円のために使ったx、y変数を使ってfillTextのレイアウトx、yを関連付けることができませんでしたか?あなたは何か考えていますか? –

答えて

0

ありがとう:

は、ここに私のコードです!私はあなたの意見を次のように実装しています!今どこでもcircleledNumbersを描くことができます!

@Override 
public void start(Stage primaryStage) { 
     Pane root = new Pane(); 

     Scene scene = new Scene(root, 300, 300); 
     primaryStage.setTitle("My Canvas"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

     Canvas cvs = new Canvas(); 
     cvs.setWidth(300); 
     cvs.setHeight(300); 
     cvs.setLayoutX(0); 
     cvs.setLayoutY(0); 
     root.getChildren().add(cvs); 

     GraphicsContext gc = cvs.getGraphicsContext2D(); 
     double x = (cvs.getWidth() - 26)/2; 
     double y = (cvs.getHeight() - 26)/2; 
     gc.drawImage(createCircledNumber(7), x, y); 

     gc.drawImage(createCircledNumber(35), 50, 50); 
     gc.drawImage(createCircledNumber(75), 70, 105); 
    } 

    private WritableImage createCircledNumber(int number) { 
     //createCircledNumber() method always returns 26px X 26px sized image 
     StackPane sPane = new StackPane(); 
     sPane.setPrefSize(26, 26);   

     Circle c = new Circle(26/2.0); 
     c.setStroke(Color.BLACK); 
     c.setFill(Color.WHITE); 
     c.setStrokeWidth(3); 
     sPane.getChildren().add(c); 

     Text txtNum = new Text(number+""); 
     sPane.getChildren().add(txtNum); 
     SnapshotParameters parameters = new SnapshotParameters(); 
     parameters.setFill(Color.TRANSPARENT); 
     return sPane.snapshot(parameters, null); 
    } 
関連する問題