Reputation:
I'm new to javaFx, and I want to create a griddles-like game. First thing I want to do is to understand how the event handling works.
So, I created a simple board of rectangles and attached each rectangle with MouseEvent and an event handler iv'e created.
What I wanted to do is : if the event is left mouse click, paint the rectangle in, else if the event is MouseEnter and the left mouse is already pressed, paint the current rectangle in red.
I thought it should be pretty basic but when i implemented this I get only mouse pressed and entered on the first rectangle I click and mouse enter on the last rectangle when I dismiss the left mouse click..
this is the event handler class :
public void handle(MouseEvent event) {
if(event.getEventType().equals(MouseEvent.MOUSE_PRESSED)) {
System.out.println("Pressed on " + row + ", " + col);
} else if (event.getEventType().equals(MouseEvent.MOUSE_ENTERED)) {
System.out.println("Entered " + row + ", " + col);
}
}
and this is where I attach the event :
private void addRectangles() {
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
Rectangle rectangle = new Rectangle(col * CELL_SIZE, row * CELL_SIZE, CELL_SIZE, CELL_SIZE);
rectangle.setStroke(Color.WHITE);
rectangle.addEventHandler(MouseEvent.ANY, new ShapeColorChanger(row, col, rectangle));
getChildren().add(rectangle);
}
}
}
So, if i'm pressing a rectangle in 0,0 and dragging the mouse to rectangle 0,3 (while going over rectangles at 0,1 and 0,2) I get this output :
Entered 0, 0
Pressed on 0, 0
Entered 0, 3
Upvotes: 1
Views: 5384
Reputation: 13859
This is a controller version of http://www.java2s.com/Code/Java/JavaFX/Listentoallmouseevents.htm
@Override
public void initialize(URL url, ResourceBundle rb)
{
//Handles mouse events
EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
System.out.println(mouseEvent.getEventType() + "\n"
+ "X : Y - " + mouseEvent.getX() + " : " + mouseEvent.getY() + "\n"
+ "SceneX : SceneY - " + mouseEvent.getSceneX() + " : " + mouseEvent.getSceneY() + "\n"
+ "ScreenX : ScreenY - " + mouseEvent.getScreenX() + " : " + mouseEvent.getScreenY());
}
};
//Creates 10 rectangles and set the Mouse events.
List<Rectangle> rContainer = new ArrayList();
for(int i = 0; i < 10; i++)
{
Rectangle rectangle = new Rectangle();
rectangle.setX(50);
rectangle.setY(50);
rectangle.setWidth(200);
rectangle.setHeight(100);
rectangle.setOnMouseClicked(mouseHandler);
rectangle.setOnMouseDragEntered(mouseHandler);
rectangle.setOnMouseEntered(mouseHandler);
rectangle.setFill(Color.BLUE);
rContainer.add(rectangle);
}
//You code will be different here. Here you need to add the arraylist to your root. My root is an AnchorPane with id apMain in FXML
apMain.getChildren().addAll(rContainer);
}
Upvotes: 1
Reputation: 82461
If a drag gesture is detected, the mouse events are only delivered to the source of the gesture. No other nodes receive the mouse events.
If's possible to deal with this behavior by initiating a full drag and listening for the MOUSE_DRAG_ENTERED
event:
@Override
public void start(Stage primaryStage) {
Rectangle rect = new Rectangle(100, 100);
Rectangle rect2 = new Rectangle(200, 200, 100, 100);
rect.setOnDragDetected(evt -> {
// start full drag
rect.startFullDrag();
});
rect2.setOnDragDetected(evt -> {
// start full drag
rect2.startFullDrag();
});
// print something when mouse enters the rects during a drag event.
rect.setOnMouseDragEntered(evt -> System.out.println("enter"));
rect2.setOnMouseDragEntered(evt -> System.out.println("enter"));
Pane root = new Pane(rect, rect2);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
Upvotes: 3