2012-12-31 80 views
4

我在我的Snake Clone Game中獲得了動畫。但是基於圖片的問題是圖片沒有透明度(請注意圓圈圖片的白色背景,編程方面,是否有修復以便能夠將這些繪製圖片的透明度包括在內?)如何在Java中使繪製的圖像透明

這是一個包含我的代碼和程序的輸出畫面。

enter image description here

PS在一個側面說明,我決定粘貼的直接聯繫,而不是IMG代碼,因爲我似乎無法得到它顯示在計算器上。我在IMG代碼前面放了一個感嘆號,但它沒有起作用,所以這裏是直接鏈接。

+0

您的圖片背景是否透明? – ntalbs

+0

圖像背景沒有透明度。背景是簡單地將JPanel的背景設置爲Color.BLACK,如下所示:setBackground(Color.black) – Nicholas

回答

7

作爲提到的其他答案,最簡單的方法可能是簡單地使用具有透明背景的PNG圖像(您可以使用GIMP等圖像編輯器創建這些圖像)。另外,如果你被限制爲PNG圖片了堅實的背景,這裏有一個如何在PNG一個給定的顏色(例如白色)變爲透明的例子:

enter image description here

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); 
     } 
     }); 
    } 
} 
+0

我注意到你使用單個或兩個字母作爲你的變量名稱。這種做法是否僅爲這個給定的例子命名? – Nicholas

+0

我的變量命名在這裏不太好,因爲這只是一個例子。可以選擇更多的描述性變量名稱。 – 808sound

+0

我看到謝謝!它看起來像一個非常優雅的例子。就我個人而言,我所介紹的Java類從未深入研究過您在示例或圖像透明中使用的Java類。我會看看你的例子。 =]欣賞給定的例子,808sound! – Nicholas

4

請勿使用油漆繪製圖像。使用其他一些使用像Paint.net或Photoshop一樣的程序...如果你要永遠使用圓,那麼你可以使用g.drawOval(x,y,w,h)。

+0

但問題出現了,因爲我需要爲每個圖像添加Alpha通道。這很好,如果我只用了3張照片。但是如果我的項目使用了很多照片,比如1000張照片,會發生什麼? – Nicholas

1
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; 
} 
-2

簡單使用類型ARGB像這樣

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

我希望它能工作。

相關問題