I have a code like that:
// In MyPanel.java
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// Draw something
mypanel_count++;
As the other answers say: it's a problem of when AWT is calling paint().
If you do some work that needs information from painted/layouted Components, what helps is also to put this work into a worker thread that waits until the painting is done.
In your case that would be something like:
panel.repaint();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// do huge work
test_count++;
System.out.println("Test_count: " + test_count
+ ", MyPanel_count: " + mypanel_count);
}
});
Although I'm not sure how it would behave in your while loop.