0

我正在尝试为我的游戏编写一个方法,该方法将获取图像、旧颜色的十六进制值和新颜色的十六进制值,然后将旧颜色的所有像素转换为新颜色。现在,该方法将整个图像绘制为新颜色,就好像 if 语句根本不起作用一样。这个方法:

private void convertColors(BufferedImage img, int oldColor, int newColor)
{
    Graphics g = img.getGraphics();
    g.setColor(new Color(newColor));
    Color old = new Color(oldColor);

    for(int x = 0; x < img.getWidth(); x++)
    {
        for(int y = 0; y < img.getHeight(); y++)
        {
            Color tmp = new Color(img.getRGB(x, y));
            if(tmp.equals(old));
            {
                System.out.println("Temp=" + tmp.toString() + "Old=" + old.toString() + "New=" + g.getColor().toString());
                g.fillRect(x, y, 1, 1);
            }
        }
    }
    g.dispose();
}

*oldColor 的十六进制为0xFFFFFF(白色),newColor的十六进制为0xFF0000(红色)。

使用 println 方法,我得到以下结果:

    Temp=java.awt.Color[r=0,g=0,b=0]Old=java.awt.Color[r=255,g=255,b=255]New=java.awt.Color[r=255,g=0,b=0]
Temp=java.awt.Color[r=255,g=255,b=255]Old=java.awt.Color[r=255,g=255,b=255]New=java.awt.Color[r=255,g=0,b=0]

第二条线看起来是正确的,临时颜色和旧的相同,但显然第一条不是这种情况。我也尝试创建一个新的 BufferedImage 并复制像素,但结果相同...... equals 方法是否像我认为的那样不起作用,或者整个方法不起作用并且有更好的方法来做到这一点?感谢您提前提供帮助。

4

3 回答 3

4

只需删除;after if(tmp.equals(old))
否则,您比较颜色,比较后什么也不做,总是选择新颜色。

除此之外,您需要稍微重新组织您的代码以使其更高效:

Graphics g = img.getGraphics();
g.setColor(new Color(newColor));

for(int x = 0; x < img.getWidth(); x++) {
    for(int y = 0; y < img.getHeight(); y++) {
        if(img.getRGB(x, y) == oldColor) {//check if pixel color matches old color
            g.fillRect(x, y, 1, 1);//fill the pixel with the right color
        }
    }
}
g.dispose();



仅仅因为我对该主题感兴趣:依靠图像过滤器,您可以通过以下方式完成所有操作:

class ColorSwapFilter extends RGBImageFilter {
  int newColor, oldColor;
  public ColorSwapFilter(int newColor, int oldColor) {
    canFilterIndexColorModel = true;
    this.newColor = newColor;
    this.oldColor = oldColor;
  }

  @Override
  public int filterRGB(int x, int y, int rgb) {
    return rgb == oldColor ? newColor : oldColor;
  }
}

应该通过调用

BufferedImage img;//your image
ColorSwapFilter filter = new ColorSwapFilter(...,...);//your colors to be swapped.
ImageProducer producer = img.getSource();
producer = new FilteredImageSource(producer, filter);
Image im = Toolkit.getDefaultToolkit().createImage(producer);
于 2013-07-18T17:42:54.160 回答
3

你的 if 语句后面有一个分号if(tmp.equals(old));

这实质上是告诉 Java 您的 if 语句只有一个与之关联的命令,并且该命令是一个分号,实际上意味着“什么也不做”。如果您删除它,它将恢复其下方代码块的条件,无论条件如何,它现在每次都在运行。

于 2013-07-18T17:45:04.203 回答
-1

我让它工作了;这是有效的 convertColors 方法:

    private BufferedImage convertColors(BufferedImage img, int oldColor, int newColor)
{
    int [] pixels = new int [img.getWidth() * img.getHeight()];
    img.getRGB(0, 0, img.getWidth(), img.getHeight(), pixels, 0, img.getWidth());
    Color old = new Color(oldColor);
    Color newC = new Color(newColor);

    for(int x = 0; x < img.getWidth(); x++)
    {
        for(int y = 0; y < img.getHeight(); y++)
        {
            Color tmp = new Color(pixels[x + y * img.getWidth()]);
            if(tmp.equals(old))
            {
                pixels[x + y * img.getWidth()] = newC.getRGB();
            }
        }
    }
    img.setRGB(0, 0, img.getWidth(), img.getHeight(), pixels, 0, img.getWidth());

    return newImg;
}
于 2013-07-18T19:06:18.633 回答