开发者

Fade/Redraw/Modify ImageIcon on click in SWING JLabel on the fly

I want a JLabel with an Icon to look "clicked", when mouse is clicked on the Label. The Label contains an ImageIcon. Instead of changing the icon to another one, I want to redraw the ImageIcon with another colorset (e.g.: setXORMode(new Color(255,0,0) ) "on the fly". Anyone has a hint how to manage that?

JLabel my_label = new JLabel("");
my_label.setIcon(new ImageIcon(MyClass.class.getResource("/path/to/resources/myicon.jpg")));
my_label.addMouseListener(new MouseAdapter开发者_JS百科() {
    @Override
    public void mousePressed(MouseEvent e) {
        //HERE I NEED THE VODOO :)
    }
});


Assuming you read the image into from disk you would do something like this.

URL url = getClass().getResource("images/BB.jpg");
BufferedImage picture = ImageIO.read(url);

Later when you need to change XOrMode you would do the following:

Graphics2D g = picture.createGraphics();
g.setXORMode(new Color(255,0,0) )
g.dispose();

If you want to fadein/fadeout, I'd recommend the timing framework. Also, if you want to repaint the part of the label itself in addition to the image in the label, you can override void paintComponent(Graphics g).

A very good resource for that you might consider looking into is the book Filthy Rich Clients. It's full of this kind of stuff. If you look the examples on the website from Chapter 4 there is some sample image code that might be useful. It would be a very, very good book for you to pick up too.


I'd use LookupOp to modify a copy of the icon when it's loaded. Then use setIcon() in the mouse handler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜