2017-05-04 26 views
0

ImageFrameで使用する測定ツールをJavaFXで作成しようとしていますが、ここで画像の2点をクリックして距離を取得します。パートアウト。しかし、私はまた、私がクリックした画像上のどこを見る/マークすることができるようにしたいが、そうするのが最善であるとは想像もできない。測定ツールのコードを添付して、私が扱っていることをよりよく理解できるようにします。私はそれが最初のifループの内側になければならないと思います。(secondposx、secondposy)でマークを設定できますが、私の質問はどのようにマークできますか?いいアイデアはありますか? uはそれを削除したい場合は、JavaFX、画像の座標をマウスでクリックすると

Circle c = new Circle(secondposx, secondposy, 5, Color.RED); 
anchorPane.getChildren().add(c); 

::-)

private void btnMeasureAction(ActionEvent event) { 
    if (btnMeasure.isSelected()) { 
     imgView.setCursor(Cursor.CROSSHAIR); 
     imgView.setPickOnBounds(true); 
     imgView.setOnMouseClicked(e -> { 
      secondposx = e.getX(); 
      secondposy = e.getY(); 
// I think the MARK should be set here. 
       //System.out.println(secondposx + ", " + secondposy); 


      if ((firstposx == 0)) { 
       firstposx = secondposx; 
       firstposy = secondposy; 
       //System.out.println(firstposx + ", " + firstposy); 
      } else { 
       double distance = Math.sqrt(Math.pow((secondposx - firstposx), 2) + Math.pow((secondposy - firstposy), 2)); 
       System.out.println("The distance is: " + distance); 
       btnMeasure.setSelected(false); 
       imgView.setOnMouseClicked(null); 
       imgView.setCursor(Cursor.DEFAULT); 
       firstposx = 0; 
       firstposy = 0; 
       secondposy = 0; 
       secondposx = 0; 
      } 
+0

アンカーパネル内のImageViewはありますか? – MeGoodGuy

+0

@MeGoodGuy AnchorPane-> BorderPane-> ScrollPane - > ImageView – Heidi

答えて

2

一つの解決策がラップすることですPane内に画像を表示し、Paneに適切な図形を追加します。私。代わりに

scrollPane.setContent(imgView); 

Pane imgContainer = new Pane(imgView); 
scrollPane.setContent(imgContainer); 

を行い、その後、あなたはの祖先である既存のAnchorPane(またはそれに他の容器に直接マーカーを追加したい場合は

Circle marker = new Circle(secondposx, secondposy, 2, Color.SALMON); 
imgContainer.getChildren().add(marker); 

を行いますイメージビュー)を作成し、追加のコンテナを作成しないようにすることもできますが、座標をイメージビューからそのコンテナに変更する必要があります。シーン内の座標を取得してからシーン座標をコンテナ座標に変更することができます。

Point2D sceneCoords = new Point2D(e.getSceneX(), e.getSceneY()); 
Point2D anchorPaneCoords = anchorPane.sceneToLocal(sceneCoords); 
Circle marker = new Circle(anchorPaneCoords.getX(), anchorPaneCoords.getY(), 2, Color.CORAL); 
anchorPane.getChildren().add(marker); 
+0

ありがとうございました!私はあなたがフォローアップの質問を気にしないことを願っています。後でマークを削除するにはどうしたらいいですか? imgContainer.getChildren()を書くと、remove(marker);後者のみを削除します。 – Heidi

+1

@Heidi複数のファイルを削除する場合は、複数のファイルを追跡する必要があります。例えばプライベートサークルfirstMarkerとプライベートサークルsecondMarkerの2つのインスタンスフィールドが必要な場合があります。必要に応じて、それらを作成するときに必要に応じてサークルを割り当て、両方のサークルを削除します。 (もっと一般的には、多くの人がいるなら 'List 'などが必要かもしれません)。 –

1

ことは、これを試してみてください

anchorPane.getChildren().remove(c); 

そして、はい、その、この場所で

関連する問題