I’m writing a game in Java and this is the first one that I’m trying to make “pretty”. The game is called Bantumi – it’s a board game and right now I’m programming the animations for the movements. The problem is that, when the movement animation is running, the board still gets the Mouse Event and if the user selects a new movement, the running one will be discarded.
For the board I’m using a class extending JLayeredPane. This how I have my layers:
Layer 0: The Holes with the seeds, so the users selects one for the movement, each Hole being a JPanel with a MouseListener.
Layer 1: The Highlight that marks the currently selected Hole
Layer 2: The Animation of the Movement.
Layer 10: A custom notification system class that I wrote, it says things like “Your Turn”, “Repeat Turn”, “You win”, etc.
I want to prevent every possible mouse event in any of this layers while the animation is running, how do I do that? I thought that adding a panel covering the whole area in a top player was enough but it didn’t work. Any workaround?
If this is a Swing GUI, you’ve already got a JPanel that covers the top level window, the glassPane, but the only way to make this work is you have to add a MouseListener (or both MouseListener and MouseMotionListener), and you have to make it visible.
You can get the top window’s glasspane by calling
getGlassPane()on either a top level window or its root pane, add a MouseListener and MouseMotionListener to it, and then whenever you want to make the GUI unresponsive to mouse events, set the glass pane to visible by callingsetVisble(true)on it. You can toggle this effect off by doing the converse, by callingsetVisble(false).