views:

46

answers:

2

Hi,

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() {
    @Override
    public void mousePressed(MouseEvent e) {
        //HERE I NEED THE VODOO :)
    }
});
+2  A: 

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

trashgod
I like both solutions, but I prefer the other one. Thanks for your answer; +1 Vote from me for you.
Erik
+2  A: 

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.

Jay Askren
Thanks - that is what I was thinking about.
Erik