Thanks VernonDozier.
The code as I showed it runs. After you answered I just noticed there are dozens of nullPointerExceptions showing on command line as in
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
The loop however finishes.
You were also wright, I could do without two of the lines: If I comment out lines 44 and 45, every number goes on top of each other and in the end all you have on the window is just a black rectangle.
Following your cue, commenting out lines 18 and 44 does exactly what I was looking for: drawing the string, refreshing the previous panel and
without any of the NullPointerException.
But my problem remains, do I still have this 100000 layers one on top of each other, or does the garbage collector deletes the previous ones?
Also, when is this paintComponent() method called?
I coded like this simply because I can't think of any other method. Is there a way to delete the previous panel, or do people just don't bother to handle that because it's not necessary?
I really appreciate your answers.