简体   繁体   English

透明.png背景

[英]transparent .png background

I know Im not the first person to ask this type of question, but I think mines a little bit different. 我知道我不是第一个提出此类问题的人,但我认为我的问题有所不同。

I have a png image I drew on MS paint that is a player and I want the background of the image to be transparent when I use a graphics objects to draw the image. 我有一个在MS Paint上绘制的png图像,它是一个播放器,当我使用图形对象绘制图像时,我希望图像的背景是透明的。 I tried some stuff with magic pink but It doesn't seem to be working the same in java. 我尝试了一些带有魔术粉红色的东西,但是在Java中似乎无法正常工作。 Im not new to java, but Im inexperienced so could you explain any packages or methods that you use thanks! 我不是Java的新手,但是我没有经验,所以您能解释一下您使用的任何软件包或方法吗,谢谢!

  1. You will need to use AlphaComposite to have the transparency effect: 您将需要使用AlphaComposite来具有透明效果:
  2. Assuming that you already know Graphics2D and Graphics uses BufferedImage 假设您已经知道Graphics2DGraphics使用BufferedImage
  3. Creating temporary graphics object g.create() and then dispose the object for safely restore the state of graphics object changed after the object creation. 创建临时图形对象g.create() ,然后处置该对象,以安全地恢复创建对象后更改的图形对象的状态。

     protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f)); g2d.drawImage(tileImage, 0, 0, getWidth(), getHeight()); g2d.dispose(); // draw player image } 

With Java6, a PNG picture should be used for TrayIcon, but as mentioned in this SO question, check: 对于Java6,应将PNG图片用于TrayIcon,但如本SO问题所述,请检查:

the background color chosen to represent the transparent pixels
the transparency options
the resolution of the icon
alternate format like SVG (provided you are using external library like Batik, and     
conversion mechnism to java.awt.Image)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM