I am currently developing a kiosk style Java program to display weather, time, forecast etc. Here is a shot of what the program should look like (screenshot taken before the time updated)
The various sections are created as JPanels with the paintComponent()
method overridden. They draw in a gradient header and a semitransparent background. However, when I change the content of one of these panels, the previous content is left behind, rather than being removed. An example is shown below:
As you can see, when the time changed from 11:51 to 11:52, the numbers overlapped, and the background became lighter (because the semitransparent background was redrawn over it)
What should I do to prevent this overlap? Ultimately, I would prefer to not have to redraw the entire screen when an individual panel updates it's contents. I would prefer to only redraw that particular section. It would be easy to repaint the background, and then all the components within.
Several answers involve clearing the area to be drawn. Whether I use g.clear()
or AlphaComposite.CLEAR
it erases the background image. this can be seen in the following image. Perhaps if I could copy the background before I clear the area, I could repaint that section of the background, then layer the other images appropriately. Thoughts?
An answer was submitted which shows a complete working example of a timer laid over a transparent panel and a background. This is very useful, however, I am not painting my sub-componenets manually. The time, weather icons and temperatures are all JLabels laid out over the semi-transparent panel. I would prefer to not have to paint the individual components manually.
A similar problem
is addressed here
Note also, "Swing programs should override paintComponent()
instead of overriding paint()
."—Painting in AWT and Swing: The Paint Methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With