Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Java graphics not updating when they should

Posted on 2004-08-04
5
281 Views
Last Modified: 2008-01-16
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)
{
     super.paintComponent(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.
0
Comment
Question by:slikrik98
  • 2
  • 2
5 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 11720621
>> after the user closes the JDialog, I explicitly make a call to update()
Try adding the following after update() :

   yourPanel.revalidate();
   yourPanel.repaint();

with yourPanel =  JPanel on which you draw the graph
0
 

Author Comment

by:slikrik98
ID: 11720684
This makes everything flicker very badly.  Sometimes to the point where you can't even see the graphics.
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 11721969
>     update(); // my user-defined method to draw the graphics

WHat Graphics context is this call using to paint to the screen?
I'd suggest passing the context passed to paint and use it.
0
 

Author Comment

by:slikrik98
ID: 11722453
That was it!  I was calling getGraphics() within my update method, but now I pass in the same Graphics that automatically gets passed into paintComponent();

Any clue why it wasn't working when calling getGraphics()?

Thanks!
0
 
LVL 92

Expert Comment

by:objects
ID: 11722697
Not sure on the specific reasons, I always just use the context provided.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 62
factorial example 4 47
servlet example 17 51
Notify sent to other threads in Java 9 33
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question