2017-08-02 97 views
0

我想做一個馬里奧遊戲克隆,現在,在我的構造函數中,我有一個方法,應該使某種顏色透明,而不是當前粉紅色(R:255,G:0,B: 254)。根據Photoshop,十六進制值是ff00fe。我的方法是:爲什麼我的方法不能去除這種顏色?

public Mario(){ 
    this.state = MarioState.SMALL; 
    this.x = 54; 
    this.y = 806; 
    URL spriteAtLoc = getClass().getResource("sprites/Mario/SmallStandFaceRight.bmp"); 

    try{ 
     sprite = ImageIO.read(spriteAtLoc); 
     int width = sprite.getWidth(); 
     int height = sprite.getHeight(); 
     int[] pixels = new int[width * height]; 
     sprite.getRGB(0, 0, width, height, pixels, 0, width); 

     for (int i = 0; i < pixels.length; i++) { 

     if (pixels[i] == 0xFFff00fe) { 

      pixels[i] = 0x00ff00fe; //this is supposed to set alpha value to 0 and make the target color transparent 
     } 

     } 
    } catch(IOException e){ 
     System.out.println("sprite not found"); 
     e.printStackTrace(); 
    } 
    } 

它運行和編譯,但精靈出來完全一樣,當我呈現它。 (編輯:也許注意到我沒有super.paintComponent方法(G)在我的paintComponent(G)方法中的精靈是.bmps this what the sprite looks like

+0

你能解釋一下什麼用確切的問題精靈更詳細嗎? – SteelToe

+0

我想,有時間做一些調試。 –

+0

它應該是一個超級馬里奧的精靈,它是以bmp格式;因爲它的全部都是正方形,所以不是馬里奧的像素是我想在繪製時變成透明的顏色。 – Derry

回答

3

您只使用BufferedImage.getRGB檢索像素,它返回一個副本。在BufferedImage的一個特定區域中的數據的。

任何改變您對返回不會自動反射回圖像int[]

要更新的圖像,你需要調用BufferedImage.setRGB更改後int[]

sprite.setRGB(0, 0, width, height, pixels, 0, width); 

另一項改變你應該做(這涉及到一個小猜測,因爲我沒有你的BMP來測試) - 這意味着它不」 - 由ImageIO.read返回可能BufferedImage.TYPE_INT_RGB類型的BufferedImage沒有alpha通道。你可以通過打印sprite.getType()進行驗證,如果打印1它是沒有alpha通道的TYPE_INT_RGB。

爲了得到一個alpha通道,創建大小合適的新BufferedImage然後設置圖像上的轉換int[],然後使用新的圖像從此:

BufferedImage newSprite = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
newSprite.setRGB(0, 0, width, height, pixels, 0, width); 
sprite = newSprite; // Swap the old sprite for the new one with an alpha channel 
+0

所以在for循環之後,放置你的代碼?我試過了,它沒有做 – Derry

+0

是的,在for循環之後。 –

+0

您的流程可能存在更多問題 - 上面是必要的步驟,但可能不是唯一的步驟。您是否使用過調試器或打印語句來查看是否有像素等於'0xFFff00fe'? –

1

BMP圖像不提供alpha通道,你必須手動設置(當你在你的代碼做)......

當您檢查您的像素有一定的顏色,你必須檢查沒有阿爾法(BMP沒有alpha它總是爲0x0) 。

if (pixels[i] == 0x00ff00fe) { //THIS is the color WITHOUT alpha 
    pixels[i] = 0xFFff00fe; //set alpha to 0xFF to make this pixel transparent 
} 

因此,在短期:你做了所有正確的,但混合起來有點^^

+0

對我來說,當我使用(像素[i] == -65282)但是使用你的代碼時,我可以改變像素的顏色,但是到目前爲止,沒有什麼會改變 – Derry

+0

,這個:if(pixels [i] == -65282){pixels [i] = 0xFF000000; }將粉紅色像素改變爲黑色;當我嘗試將0x00ff00fe放入我的if狀態時,它什麼也不做,是什麼 – Derry

1

這工作:

enter image description here

private BufferedImage switchColors(BufferedImage img) { 
    int w = img.getWidth(); 
    int h = img.getHeight(); 
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 
    // top left pixel is presumed to be BG color 
    int rgb = img.getRGB(0, 0); 
    for (int xx=0; xx<w; xx++) { 
     for (int yy=0; yy<h; yy++) { 
      int rgb2 = img.getRGB(xx, yy); 
      if (rgb2!=rgb) { 
       bi.setRGB(xx, yy, rgb2); 
      } 
     } 
    } 

    return bi; 
} 
+0

就是在JFrame上?因爲現在,在我從上面獲取的版本中,我可以改變粉色,但在JFrame上顯示時不能變透明,只能顯示爲白色。如果你是唯一的方法,那麼我會嘗試。 – Derry

+1

*「是在JFrame上?」*它是**不相關**它是什麼。無論頂級容器是什麼,該方法都可以工作。如果你懷疑,*嘗試*在不同的背景!碰巧,我從不直接添加東西到一個框架。這些標籤被添加到「JPanel」(然後顯示在「JFrame」中)。 *「如果你的做法是唯一的方法」*可能不是唯一的方法,但與問題中看到的方法明顯不同,因爲它按預期工作。我懷疑這種方法的問題是得到顏色的十六進制表示錯誤,但我不會檢查 - 沒有MCVE! –

相關問題