gpt4 book ai didi

java - 如何给 ImageIcon 着色

转载 作者:行者123 更新时间:2023-12-01 11:30:24 25 4
gpt4 key购买 nike

如何将通过此处的图标着色为不同的颜色?假设我想拍摄一张白色图像并将其调暗一点。我已经研究过 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;
}
}

最佳答案

您需要创建一个新的 BufferedImage 来进行转换:

public BufferedImage colorImage(BufferedImage loadImg, int red, int green, int blue) {
BufferedImage img = new BufferedImage(loadImg.getWidth(), loadImg.getHeight(),
BufferedImage.TRANSLUCENT);
Graphics2D graphics = img.createGraphics();
Color newColor = new Color(red, green, blue, 0 /* alpha needs to be zero */);
graphics.setXORMode(newColor);
graphics.drawImage(loadImg, null, 0, 0);
graphics.dispose();
return img;
}

关于java - 如何给 ImageIcon 着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30444232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com