2017-08-02 98 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! –

相关问题