Java graphics not updating when they should
Posted on 2004-08-04
Greetings. I am writing a graph editor in Java Swing, and seem to be having some graphics updating issues. I have a JPanel on which I draw the graph using the Graphics class. I've written a function called update(void) that creates a BufferedImage object, does all the drawing to this, and then draws the BufferedImage on the JPanel's graphics (I did it this way so that when dragging items, I wouldn't get a flicker-effect). When the user right-clicks on certain things, a JPopupMenu displays, and when the user double-clicks on certain things, a modal JDialog appears. However when these menus go away, I get strange behavior: if i begin moving the mouse immediately after they go away (incidentally, my mouseMoved() function calls update()), the underlying graphics all seem as if they've been refreshed, so all is good. However, the moment I stop moving my mouse, the silhouette of the JDialog/JPopupMenu becomes visible, and not until I move my mouse again (thus calling update()) does it properly refresh. In my code, after the user clicks on a menu item from the JPopupMenu and after the user closes the JDialog, I explicitly make a call to update(), which I would think should draw any of the graphics that the other window covered/erased. Also, my paintComponent() is overridden to:
void paintComponent(Graphics g)
update(); // my user-defined method to draw the graphics
Is there another function that is getting called that I'm not aware of after the JDialogs/JPopupMenus are closed? This is all very frustrating because it seems like it should be working properly. One other thing to mention: when the modal JDialog is open, I can drag it around and the graphics underneath seem to update; I imagine that JDialog's have built in handlers for this, so I'm wondering if one of these built-in handlers is what's giving me the strange behavior when they are closed.