Java 转换颜色

Java 转换颜色,java,colors,Java,Colors,我正在尝试为我的游戏编写一个方法,它将获取一个图像,旧颜色的十六进制值,新颜色的十六进制值,然后将旧颜色的所有像素转换为新颜色。现在,该方法将整个图像绘制为新颜色,就好像if语句根本不起作用一样。该方法如下: private void convertColors(BufferedImage img, int oldColor, int newColor) { Graphics g = img.getGraphics(); g.setColor(new Color(newColor

我正在尝试为我的游戏编写一个方法,它将获取一个图像,旧颜色的十六进制值,新颜色的十六进制值,然后将旧颜色的所有像素转换为新颜色。现在,该方法将整个图像绘制为新颜色,就好像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();
}

scond行看起来是正确的,temp颜色和old颜色是相同的,但第一行显然不是这样。我也尝试过创建一个新的BuffereImage并在像素上进行复制,但结果是一样的。。。equals方法是否不像我想的那样有效,或者整个方法是否都无效,还有更好的方法吗?提前感谢您的帮助。

只需删除
if(tmp.equals(old))之后的code>

否则,您将比较颜色,并且在比较后不执行任何操作,始终选择新颜色

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

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();
应该通过

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

只需删除
if(tmp.equals(old))之后的code>

否则,您将比较颜色,并且在比较后不执行任何操作,始终选择新颜色

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

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();
应该通过

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

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


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

If语句
If(tmp.equals(old))后面有一个分号


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

    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;
}
private BufferedImage convertColor(BufferedImage img、int-oldColor、int-newColor)
{
int[]像素=新的int[img.getWidth()*img.getHeight()];
getRGB(0,0,img.getWidth(),img.getHeight(),pixels,0,img.getWidth());
旧颜色=新颜色(旧颜色);
颜色newC=新颜色(newColor);
对于(int x=0;x
我让它工作了;这是工作方法:

    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;
}
private BufferedImage convertColor(BufferedImage img、int-oldColor、int-newColor)
{
int[]像素=新的int[img.getWidth()*img.getHeight()];
getRGB(0,0,img.getWidth(),img.getHeight(),pixels,0,img.getWidth());
旧颜色=新颜色(旧颜色);
颜色newC=新颜色(newColor);
对于(int x=0;x
使用getRGB获取颜色,并绘制小矩形-这将非常缓慢,尤其是在游戏中。最快的方法是直接操作BuffereImage后面的int数组,然后一次性绘制修改后的图像。@lbalazscs你说得对,我正在谷歌搜索如何实现和rgb过滤器,以满足他的需要。但是,既然他改变了图像的图形,那么仅仅调用这个函数一次,然后存储的图像就有了正确的颜色,这还不够吗@lbalazscs当我尝试这样做时:if(pixels[x+y*img.getWidth()]==oldColor){pixels[x+y*img.getWidth()]=newColor;}它进入if语句,但尽管我调用setRGB:newImg.setRGB(0,0,img.getWidth(),img.getHeight(),pixels,0,img.getWidth()),更改从未出现;顺便说一句,偏移量和扫描大小是多少?@lbalazscs Nvm,我解决了它:3I实际上建议使用
image.setRGB(int x,int y,int color)
。使用getRGB获取颜色,并绘制小矩形-这会很慢,尤其是对于游戏而言。最快的方法是直接操作BuffereImage后面的int数组,然后一次性绘制修改后的图像。@lbalazscs你说得对,我正在谷歌搜索如何实现和rgb过滤器,以满足他的需要。但是,既然他改变了图像的图形,那么仅仅调用这个函数一次,然后存储的图像就有了正确的颜色,这还不够吗@lbalazscs当我尝试这样做时:if(pixels[x+y*img.getWidth()]==oldColor){pixels[x+y*img.getWidth()]=newColor;}它进入if语句,但尽管我调用setRGB:newImg.setRGB(0,0,img.getWidth(),img.getHeight(),pixels,0,img.getWidth()),更改从未出现;顺便说一句,偏移量和扫描大小是多少?@lbalazscs Nvm,我解决了它:3I实际上建议使用
image.setRGB(int x,int y,int color)
。我犯了太多次这个错误,很惊讶我仍然犯了这个错误。。。由于某种原因,使用你的方法实际上不会将白色像素更改为红色,因此我不得不使用我的方法,但它也会在数字后面绘制部分红色图像。我要上厕所