Reputation: 183
I am writing a mapping application in Java, using Swing for the UI (I've included a prototype drawing at the end of this post). The map is implemented using a custom MapPanel class that extends JPanel. The map is fetched from the server as a series of 300x300 images (tiles) that are painted on the MapPanel whenever its paintMap() method is called. Due to the length and complexity (multiple classes, etc.) of the code I can't include all of it here but the paintMap() method basically works like this:
// Loop for each map tile (image, xPos, yPos)
getGraphics().drawImage(image, xPos, yPos, 300, 300, null);
I would like to have another JPanel (containing a JSlider for zoom control) overlayed on top of the map panel, but I'm having difficulties getting this to work. Whenever I paint the map, the overlayed JPanel disappears. If I repaint the overlayed JPanel in the paintMap() method, it flickers badly when the map is being dragged (and thus repainted continuously).
Any suggestions for how I could implement my UI without flickering?
Upvotes: 1
Views: 378
Reputation: 10069
This article describes using JLayeredPane and an abstract base class to overpaint arbitrary effects on a given Swing component.
Upvotes: 0
Reputation: 168825
getGraphics().drawImage(image, xPos, yPos, 300, 300, null); // WRONG!!
getGraphics()
- instead override the paintComponent(Graphics)
of a JPanel
or JComponent
and paint when requested to do so.this
as the ImageObserver
Upvotes: 3