简体   繁体   English

为什么我的消除这种颜色的方法无效?

[英]Why isn't my method for removing this color working?

I'm trying to make a Mario game clone, and right now, in my constructor, I have a method that is supposed to make a certain color transparent instead of the current pinkish (R: 255, G: 0, B: 254). 我正在尝试制作Mario游戏克隆,现在,在我的构造函数中,我有一种方法应该使某种颜色透明而不是当前的粉红色(R:255,G:0,B:254) 。 According to Photoshop, the hex value is ff00fe. 根据Photoshop,十六进制值为ff00fe。 My method is: 我的方法是:

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();
    }
  }

it runs and compiles, but sprite comes out exactly the same when I render it. 它可以运行和编译,但是在渲染时,精灵会完全一样。 (edit: perhaps of note I do not have super.paintComponent(g) in my paintComponent(g) method. The sprites are .bmps. (编辑:也许值得注意的是,我的paintComponent(g)方法中没有super.paintComponent(g)。这些图片是.bmps。 这就是精灵的样子

You are only retrieving the pixels using BufferedImage.getRGB . 您仅使用BufferedImage.getRGB检索像素。 That returns a copy of the data in a certain area of the BufferedImage. 这将返回BufferedImage特定区域中的数据副本

Any change you make to the int[] returned is not automatically reflected back into the image. 您对返回的int[]所做的任何更改都不会自动反映回图像中。

To update the image, you need to call BufferedImage.setRGB after you change the int[] : 要更新图像,您需要在更改int[]之后调用BufferedImage.setRGB

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

Another change you should probably make (and this involves a little guesswork as I don't have your bmp to test with) - the BufferedImage returned by ImageIO.read may have type BufferedImage.TYPE_INT_RGB - meaning that it doesn't have an alpha channel. 另一个变化你应该做(这涉及到一个小猜测,因为我没有你的BMP来测试) -返回的BufferedImage的ImageIO.read可能有型BufferedImage.TYPE_INT_RGB -这意味着它不具有alpha通道。 You can verify by printing sprite.getType() , if that prints 1 it's TYPE_INT_RGB without an alpha channel. 您可以通过打印sprite.getType()进行验证,如果打印出1 ,则为不带alpha通道的TYPE_INT_RGB。

To get an alpha channel, create a new BufferedImage of the right size and then set the converted int[] on that image, then use the new image from then on: 要获得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

BMP images don't provide an alpha channel, you have to set it manually (as you do in your code)... BMP图像不提供Alpha通道,您必须手动设置它(就像在代码中一样)...

when you check your pixel to have a certain color you have to check without alpha (BMP has no alpha it's always 0x0). 当您检查像素是否具有某种颜色时,您必须不使用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
}

so in short: you did all right but mixed it up a bit ^^ 简而言之:您没事,但把它混了一点^^

This works: 这有效:

在此处输入图片说明

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

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

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