2017-07-19 231 views
0

我想在javafx中繪製一個多邊形,將其添加到其鼠標座標點的數組中。我的問題是,我不知道如何使它停止,當我點擊鼠標左鍵點擊其他東西。在javafx中繪製多邊形

Polygon polygon = new Polygon(); 
rootPane.setOnMouseClicked((MouseEvent mouseEvent) -> { 
    do { 
     polygon.getPoints().addAll(mouseEvent.getX(),mouseEvent.getY()); 
    } while(mouseEvent.getButton().equals(mouseEvent.isSecondaryButtonDown()));  
}); 
rootPane.getChildren().add(polygon); 
+0

爲了澄清,每次發生事件時,您傳遞給事件處理程序(例如'onMouseClicked')的代碼都會執行*。這裏你不需要循環。 –

回答

2

您可以創建對多邊形的引用。如果是第一次點擊,那麼多邊形將爲null,因此請創建一個新的並將其添加到您的窗格。然後繼續添加點直到您點擊右鍵,此時您只需將多邊形設置回null,以便下一次左鍵單擊可再次開始一個新的多邊形。

SSCCE:

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.input.MouseButton; 
import javafx.scene.layout.Pane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Polygon; 
import javafx.stage.Stage; 

public class DrawPolygon extends Application { 

    private Polygon currentPolygon ; 

    @Override 
    public void start(Stage primaryStage) { 
     Pane rootPane = new Pane(); 
     rootPane.setMinSize(600, 600); 
     rootPane.setOnMouseClicked(e -> { 
      if (e.getButton() == MouseButton.PRIMARY) { 
       if (currentPolygon == null) { 
        currentPolygon = new Polygon(); 
        currentPolygon.setStroke(Color.BLACK); 
        rootPane.getChildren().add(currentPolygon); 
       } 
       currentPolygon.getPoints().addAll(e.getX(), e.getY()); 
      } else { 
       currentPolygon = null ; 
      } 
     }); 
     Scene scene = new Scene(rootPane); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

您可以解決此各種各樣的想法發揮獲得不同的用戶體驗,例如

rootPane.setOnMouseClicked(e -> { 
     if (e.getButton() == MouseButton.PRIMARY) { 
      if (currentPolygon == null) { 
       currentPolygon = new Polygon(); 
       currentPolygon.getPoints().addAll(e.getX(), e.getY()); 
       currentPolygon.setStroke(Color.BLACK); 
       rootPane.getChildren().add(currentPolygon); 
      } 
      currentPolygon.getPoints().addAll(e.getX(), e.getY()); 
     } else { 
      currentPolygon = null ; 
     } 
    }); 
    rootPane.setOnMouseMoved(e -> { 
     if (currentPolygon != null) { 
      currentPolygon.getPoints().set(currentPolygon.getPoints().size()-2, e.getX()); 
      currentPolygon.getPoints().set(currentPolygon.getPoints().size()-1, e.getY()); 
     } 
    }); 
1

問題是您的代碼卡在一個事件中。即使您移動鼠標或釋放鼠標按鈕,您正在使用的事件實例的值也不會改變。

將事件視爲單一狀態。當發生重要事情時(在你的情況下,點擊一個鼠標按鈕),javafx將在那個時刻使用鼠標狀態使用MouseEvent實例調用mouseEventHandler。當你再次點擊時,javafx會用新值創建一個新實例,然後再次調用eventHandler。

爲了使這項工作,你需要一個不同的鼠標事件(或稍微修改它,所以它只能設置一個點擊鼠標)。您需要失去(無限)while循環,因爲它既阻塞EventThread,也不會爲您需要的東西工作。所以這樣的事情可能會更好一些。

// this will add a point for every (secondary)mousebutton click 
rootPane.setOnMouseClicked((MouseEvent me) -> { 
    if(me.isSecondaryButtonDown()) 
     polygon.getPoints().addAll(me.getX(),me.getY()); 
    }); 

// this will add a point for every mousemovement while the secondary mousebutton is down. 
rootPane.setOnMouseMoved((MouseEvent) me -> { 
    if(me.isSecondaryButtonDown()) 
     polygon.getPoints().addAll(me.getX(),me.getY()); 
}); 

現在有一個MouseDragEvent,但主要是移動數據(如圖像和文件),但我wouldnt推薦它。在你的情況下它不是很有用,它的行爲仍然是退出車。

我希望這能幫助你朝正確的方向發展。