简体   繁体   English

如何使绘制的图像在Java中透明

[英]How to make drawn images transparent in Java

I got the animation to work in my Snake Clone Game. 我让动画在Snake Clone Game中运行。 But the problem based on the picture is that the images do not have transparency(notice the white background of the circle pictures. Programming-wise, is there a fix to be able to include transparency to these drawn images? 但基于图片的问题是图像没有透明度(注意圆形图片的白色背景。编程方面,是否有一个修复,能够包括这些绘制图像的透明度?

Here's a picture containing my code and the output of the program. 这是包含我的代码和程序输出的图片。

在此输入图像描述

PS On a side note, I decided to paste the direct link instead of the IMG code because I cannot seem to get it to display on StackOverFlow. PS在旁注中,我决定粘贴直接链接而不是IMG代码,因为我似乎无法在StackOverFlow上显示它。 I put an exclamation point in the front of the IMG code but it did not work so here's the direct link. 我在IMG代码的前面加了一个感叹号,但它没有用,所以这里是直接链接。

As the other answer mentioned, the easiest way would probably be to simply use PNG images which have a transparent background (you can create these with an image editor like GIMP). 正如另一个提到的答案,最简单的方法可能是简单地使用具有透明背景的PNG图像(您可以使用像GIMP这样的图像编辑器创建这些图像)。 Alternatively, if you are limited to PNG images with a solid background, here's an example of how to change a given color (eg white) in the PNG to transparent: 或者,如果您仅限于具有纯色背景的PNG图像,以下是如何将PNG中的给定颜色(例如白色)更改为透明的示例:

在此输入图像描述

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class SimpleFrame extends JFrame {
   JPanel mainPanel = new JPanel() {
      ImageIcon originalIcon = new ImageIcon("~/Pictures/apple.png");

      ImageFilter filter = new RGBImageFilter() {
         int transparentColor = Color.white.getRGB() | 0xFF000000;

         public final int filterRGB(int x, int y, int rgb) {
            if ((rgb | 0xFF000000) == transparentColor) {
               return 0x00FFFFFF & rgb;
            } else {
               return rgb;
            }
         }
      };

      ImageProducer filteredImgProd = new FilteredImageSource(originalIcon.getImage().getSource(), filter);
      Image transparentImg = Toolkit.getDefaultToolkit().createImage(filteredImgProd);

      public void paintComponent(Graphics g) {
         g.setColor(getBackground());
         g.fillRect(0, 0, getSize().width, getSize().height);

         // draw the original icon
         g.drawImage(originalIcon.getImage(), 100, 10, this);
         // draw the transparent icon
         g.drawImage(transparentImg, 140, 10, this);
      }
   };

   public SimpleFrame() {
      super("Transparency Example");

      JPanel content = (JPanel)getContentPane();
      mainPanel.setBackground(Color.black);
      content.add("Center", mainPanel);
   }

   public static void main(String[] argv) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            SimpleFrame c = new SimpleFrame();
            c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            c.setSize(280,100);
            c.setVisible(true);
         }
      });
   }
}

Don't use paint to draw your images. 不要使用油漆来绘制图像。 Use some other program that uses alpha like Paint.net or Photoshop... If your going to use circles forever then you can use g.drawOval(x, y, w, h). 使用其他一些使用像Paint.net或Photoshop这样的alpha的程序......如果你要永远使用圆圈,那么你可以使用g.drawOval(x,y,w,h)。

  public BufferedImage makeTransparentImage(BufferedImage br) {
    for (int i = 0; i < br.getHeight(); i++) {
        for (int j = 0; j < br.getWidth(); j++) {
            Color c = new Color(br.getRGB(j, i));
            int r = c.getRed();
            int b = c.getBlue();
            int g = c.getGreen();
            if ((r == 255 && b == 255 && g == 255)) {
                System.out.println("r g b " + r + g + b);
                br.setRGB(j, i, 0xFF000000);
            }
        }
    }
    return br;
}

If you draw a simple picture, The easiest and fastest way I know... Draw a picture in Macrosoft PowerPoint and click "Save as Picture" to get a transparent background. 如果您绘制一张简单的图片,我知道的最简单,最快捷的方式...在Macrosoft PowerPoint中绘制图片并单击“另存为图片”以获得透明背景。 Next... 下一个...

public class Node {
Image nodeImage[] = new Image[3];


public Node() {
    try {
        String address = "C:\\Users\\Desktop\\practice\\Simulation\\img\\";
        nodeImage[0] = ImageIO.read(new File(address + "Node_noVehicle.png"));
        nodeImage[1] = ImageIO.read(new File(address + "Node_setVehicle.png"));
        nodeImage[2] = ImageIO.read(new File(address + "Node_inVehicle.png"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
protected void paintComponent(Graphics g) {
  super.paintComponent(g);  
  Graphics2D g2dtemp = (Graphics2D) g.create();
  g2dtemp.drawImage(Node.nodeImage[0],(int)x,(int)y,width,height,this);
}

} }

Draw a picture in Macrosoft PowerPoint and click "Save as Picture" to get a transparent background. 在Macrosoft PowerPoint中绘制图片,然后单击“另存为图片”以获得透明背景。

Simple use type to ARGB like this 像这样简单使用ARGB类型

BufferedImage image = new BufferedImage(
                 width, height,
                 BufferedImage.TYPE_INT_ARGB);

I hope it should work. 我希望它应该有效。

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

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