【发布时间】:2015-05-25 18:37:23
【问题描述】:
如何将通过此处的图标着色为不同的颜色?假设我想拍一张白色的照片并让它变暗一点。我已经研究过 BufferedImages 等,但我似乎找不到任何适合我正在使用的设置的东西。我还应该注意,如果这会有所不同,我会将图像绘制到 JLabel 上。
这是我正在使用的来源,以便您了解我正在使用的内容。
public class Icon extends ImageIcon{
private int scale = 1;
private boolean mirror = false;
public Icon(URL url) throws IOException{
super(ImageIO.read(url));
}
public void setScale(int scale){
this.scale = scale;
}
@Override
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D)g.create();
int height = 0, width = this.getIconWidth(), x1 = 1;
if(mirror || scale != 1){
height = -this.getIconHeight();
}
if(mirror){
x1 = -1;
}else{
width = 0;
}
g2.translate(width * scale, height);
g2.scale(x1 * scale, 1 * scale);
super.paintIcon(c, g2, x, y);
}
public boolean isMirror() {
return mirror;
}
public void setMirror(boolean mirror) {
this.mirror = mirror;
}
}
【问题讨论】:
-
使用上面重复链接中的
ColorTintFilter,可以直接使用,也可以根据需要进行修改。它会很好地为您的图像着色。然而,如果你只是想让你的图像稍微暗一点,你可以用透明的黑色覆盖它(即new Color(0x20000000, true)或类似的)。
标签: java graphics bufferedimage imageicon tint