2017-11-09 23 views
0

矩形内をクリックするたびにSphereを追加したいと思います。基本的に、私はRectangleを使ってこの9X6グリッドを作った。アタッチされたコードは、私はActionEventHandlerの中に何を追加するのか分からない。私はこのコードを使用している場合scene.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent me)マウスで球を追加する矩形のグリッド内をクリック

public void Settings(ActionEvent event) throws Exception 
{ 
    Stage primaryStage = new Stage(); 
    Parent root = FXMLLoader.load(getClass().getResource("/application/Settings.fxml")); 
    Scene scene = new Scene(root,400,400); 
    scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 

} 
@FXML 
ObservableList<Integer> comboList = FXCollections.observableArrayList(3,4,5,6,7,8); 
ObservableList<String> gridList = FXCollections.observableArrayList("9 X 6","15 X 10"); 
@Override 
public void initialize(URL arg0, ResourceBundle arg1) { 
    // TODO Auto-generated method stub 
    combo.setItems(comboList); 
    gridb.setItems(gridList); 
} 


public void Grid() throws Exception { 
    Stage primaryStage=new Stage(); 
    //AnchorPane root = new AnchorPane(); 
    Group root = new Group(); 
    Scene scene = new Scene(root); 
    //scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 
    primaryStage.setScene(scene); 

    Rectangle r = null; 
    for(int i=0;i<9;i++) { 
     for(int j=0;j<6;j++) { 

    r = new Rectangle(70*j,70*i,70,70); 
    r.setStroke(Color.BLUE); 
    root.getChildren().add(r); 
    } 
    } 
    scene.setRoot(root); 
    primaryStage.show(); 




    scene.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent me) -> { 
     if(me.getButton().equals(MouseButton.PRIMARY)) { 
      Circle circle = new Circle(me.getX(), me.getY(), 10, Color.BLUE); 
      addEventHandler(root, circle); 
      root.getChildren().add(circle); 
     } 
    });} 
    private void addEventHandler(Group parent, Node node) { 
    // TODO Auto-generated method stub 
     node.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent me) -> { 
       if(me.getButton().equals(MouseButton.SECONDARY)) { 
        parent.getChildren().remove(node); 
       } 
      }); 
} 

、その後私は、私もグリッド線をクリックしてどこどこでもランダム円(または球)を追加することができます。私はちょうど特定の四角形のために単一の球を持っていると思う。

答えて

1

あなたは、個々のRectangle秒にイベントハンドラを追加したり、Rectangleがクリックされた場合は、チェックするMouseEventpickResultプロパティを使用するか:

public static Rectangle getIntersectedRect(MouseEvent event) { 
    Node n = event.getPickResult().getIntersectedNode(); 
    return (n instanceof Rectangle) ? (Rectangle) n : null; 
} 
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent me) -> { 
    if(me.getButton().equals(MouseButton.PRIMARY)) { 
     Rectangle rect = getIntersectedRect(me); 
     if (rect != null) { 
      Circle circle = new Circle(rect.getX()+35, rect.getY()+35, 10, Color.BLUE); 
      addEventHandler(root, circle); 
      root.getChildren().add(circle); 
     } 
    } 
});} 
+0

おかげで、これは私をたくさん助けました。しかし最後の四角形(最後の行と最後の列)にクリックすると、ちょうど1つを追加するのではなく、複数の円が追加されます。 – Shubham

関連する問題