First off, you guys are awesome. You have helped me before, and I appreciate it greatly.
This might actually be more appropriate for the threading and synchronization forum, but I thought I'd try this forum first, because I suspect my problem is in how I handle my canvas object:
I'm trying to make a fairly graphics intensive program (to be more blunt, a game) and I'm running into a problem with user input.
For some reason, my mouse events are not getting captured in my Swing JFrame. I'm trying to build a layer based application, where the mouse events captured by the frame get passed on up the line to objects that are supposed to handle them, but I think I'm lacking the conceptual understanding on how a mouseListener works, because it doesn't seem to be picked up by the frame itself.
Included is a SSCE. Run it, and click on the screen. It is intended that the console will print out at three lines per click, but it does not. In fact, in what is probably a related issue, the "X" button in the corner doesn't work either!
I've notice that when I comment out all the graphics code, my program (and SSCE) still fail to pick up mouse events. However, if I comment out all the graphics code as well as the thread.sleep call, then it works fine, up to and including the correct functionality of the "X" button. However, taking the thread.sleep out of the loop probably isn't a viable option, as my non-SSCE program is fairly graphics intensive and needs to be redrawn frequently, making a loop like this necessary.
In the SSCE, please forgive the abundance of code that doesn't really apply to the mouseListener problems. Lots of code is used to set the frame up, and another fair bit is used to set up the example "controller" layer that handles graphics and accepts mouseclicks. Please also forgive the cut-and-paste graphics code. I know how to handle a Graphics2D object once I have one, but figuring out how to get the right one to draw to is still somewhat of a dark art for me...
How can I make sure that my mouse events are getting captured, even while in a loop?
But not correctly. You are still accessing a GUI component (the canvas, although indirectly) from a non-EDT thread. Any access to GUI components, both querying and modifying, should be done from the EDT.
Why don't you use a javax.swing.Timer instead? The ActionListener's actionPerformed method will be executed on the EDT, and it won't cause any threading issues.