2009-02-10 113 views

回答

3

我不知道这样做的API方法。并且默认情况下,Images不可写。但是,如果你有一个BufferedImage,你可以做这样的:

public void changeColor(BufferedImage img, Color old, Color new) { 
    final int oldRGB = old.getRGB(); 
    final int newRGB = new.getRGB(); 
    for (int x = 0; x < img.getWidth(); x++) { 
     for (int y = 0; y < img.getHeight(); y++) { 
      if (img.getRGB(x, y) == oldRGB) 
       img.setRGB(x, y, newRGB); 
     } 
    } 
} 

这不是最有效的方式做到这一点(这是可能的RGB数据读取到一个数组,而不是一个像素的同时) ,但对于24x24的图像,它不应该是一个问题。