[英]Adding multiple Images on top of each other using transparency
是否可以将多个图像添加到单个JPanel
并使用顶部Image
的透明通道查看底层Image
?
You can, but is easier if you create your own version of JPanel
extending the JPanel
class.您可以,但如果您创建自己的
JPanel
版本扩展JPanel
类, JPanel
容易。 Create a subclass of JPanel
and override the paint method.创建
JPanel
的子类并覆盖paint方法。 In the paint
method, just paint one image over another image.在
paint
方法中,只需将一个图像绘制在另一个图像上。
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.创建一个
List
来存储图像和一个方法来将图像添加到列表中。 Then, in the paint
method, paint all the images.然后,在
paint
方法中,绘制所有图像。 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.如果您即时创建图像,请记住使用带有 Alpha 通道的
Color
。 If you load images from files, I recomend you to use PNGs.如果您从文件加载图像,我建议您使用 PNG。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.