How to update with mouseEnter() method in Swing without the mouse moving?
I use a swing interface in a game I am programming, and I implement ActionListener and MouseListener. For this particular issue, MouseListener is the issue.
for the mouseEntered method, I have a series of if statements which essentially decide whether the currently hovered tile is legal to build on or not and the cursor turns into an X (to designate an illegal target) until the mouseExited method is called. This system works just fine, except for one thing.
One of the criteria which could make the tiles illegal to build on is if an enemy is currently walking on that tile. If I move the cursor to a tile with an enemy on it, it will in fact change as I have intended. However, since this is a method which triggers when the mouse enters a component, once the mouse is in that component, it no longer updates until leaving that component.
Therefore, if my cursor is on a tile which is a legal target, it will display the default cursor as intended. However, if I wait and an enemy enters that tile, since the mouse has not moved, the cursor will remain default, as it if is still a legal target (of course, my methods which actually build on the tile still won't let me build, because they know it's an illegal target, but the user will not see this because of the cursor's inability to update). In the same regard, leaving my cursor on a tile which is only illegal because an enemy is there will leave the cursor as an X even after the enemy has moved on (although my build methods will again allow me to build).
How can I call the mo开发者_StackOverflow社区useEntered method explicitly, with the same event as an argument as if the mouse had just moved to the point where it currently is?
What you want to do as part of your enemy movement is check whether it is moving into the square the mouse is currently in. If the enemy moves into the same square the mouse is in then you just change the cursor to the illegal target version. You also want to make sure when the enemy moves out of that square that you run your check again to see if the cursor needs to change back to the default cursor.
It may be easiest to set a variable representing the square the mouse is in as it moves so you don't have to recalculate the mouse position during each enemy move.
精彩评论