Is it possible to add multiple images to a single JPanel
and using the transparency channel of the Image
on top to see the underlying Image
?
Asked
Active
Viewed 146 times
0

Andrew Thompson
- 168,117
- 40
- 217
- 433

Max
- 94
- 3
- 6
-
1When you draw multiple images (that have transparency in them) using a sequence of `graphics.drawImage(...)` calls, then the transparency should already be taken into account. Can you explain *what* the actual question is? – Marco13 May 04 '18 at 14:56
-
*"Is it possible to add multiple images to a single `JPanel` and using the **transparency** channel of the `Image` on top to see the underlying `Image`?"* **Yes.** General Tips: 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson May 05 '18 at 01:27
1 Answers
-2
You can, but is easier if you create your own version of JPanel
extending the JPanel
class. Create a subclass of JPanel
and override the paint method. In the paint
method, just paint one image over another image.
public class MiPanel extends JPanel {
List<Image> images;
...
void addIMage( Image im) {
images.add( im)
}
@Override
public void paint( Graphics g) {
Graphics2D g2d = (Graphics2D)g;
...
for ( Image im :images) {
g2d.drawImage( im, 0,0, null);
}
...
}
Create a List
to store the images and a method to add images to the list. Then, in the paint
method, paint all the images. Of course, if the images have transparency, you will be able to see the "under" images. If you create the images on the fly, remember to use Color
with alpha channel. If you load images from files, I recomend you to use PNGs.

Nil
- 127
- 3
-
(1-) custom painting is done by overriding `paintComponent(...)`, not paint(). And what is "iconImage.getDecoration()"? – camickr May 04 '18 at 14:10
-
iconIMage... sorry. Copy&paste from a real example. It's better in this case to use "paint" instead of "paintComponent" because overriding paintComponent you have a solid background (or set setOpaque(false)), but using `paint` you have total control about what you paint. And you can use the same method if you extend JFrame instead JPanel, which is the case of the example I have in my own app from I copied the code. – Nil May 04 '18 at 14:23
-
2No you should NOT override paint(). What you do in your code is up to you, but the point of the forum is to promote standard Java practices, which will prevent problems in the future. For example, in your approach you lose the ability to add Borders to the component. 2) Also the background SHOULD always be painted otherwise you can have painting artifacts. This is especially important when dealing with transparent painting. – camickr May 04 '18 at 14:43
-
well, if you paint the background, you will not see what's under the panel. You don't know if the user is creating something like a translucent messagebox, which is the most common use of whatever-transparent-thing in Swing apps. The `paintCompnent` should be invoked instead of `paint` because there's a lot of clipping work in `paint`, for example, that you should replicate in your overriden `paint`, but if the point is making something special, like transparent things, to override `paint` is ok. But OK, you win, use paintComponent and an opaque background to make transparent things... – Nil May 04 '18 at 15:08
-
1It is not about winning. It is about understanding how painting works in Swing. If you don't want to paint the background, then you use `setOpaque(false)`. If you have an opaque component and you don't paint the background you can cause painting artifacts. See [Backgrounds With Transparency](https://tips4java.wordpress.com/2009/05/31/backgrounds-with-transparency/) for more information on this subject. Also see [Swing Painting Guidelines](http://www.oracle.com/technetwork/java/painting-140037.html#swing_summary) for a more detailed discussion on painting in Swing.. – camickr May 04 '18 at 15:42