如何给ImageIcon着色 [英] How to tint an ImageIcon
本文介绍了如何给ImageIcon着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何将通过此处的图标着色为其他颜色?假设我想拍摄一张白色的图像并使它更暗.我已经研究了BufferedImages之类的东西,但是似乎找不到适合我所使用的设置的任何东西.我还应该注意,如果有区别,我会将图像绘制到JLabel上.
How would I tint the icon that is passed through here to be a different color? Say I wanted to take a white image and make it a bit darker. I have looked into BufferedImages and such but I can't seem to find anything that will fit into the setup that I am using. I should also note that I am drawing the images onto a JLabel if that makes a difference.
这是我正在使用的来源,以便您可以了解我正在使用的工具.
Here is the source that I am using so that you can get an idea as to what I am working with.
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才能将其转换为:
you need to create a new BufferedImage to make the transform into:
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;
}
这篇关于如何给ImageIcon着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文