001/*
002 * $Id: GradientThumbRenderer.java 4082 2011-11-15 18:39:43Z kschaefe $
003 *
004 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 * 
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 * 
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020 */
021package org.jdesktop.swingx.color;
022
023import java.awt.Color;
024import java.awt.Graphics;
025import java.awt.Image;
026
027import javax.imageio.ImageIO;
028import javax.swing.JComponent;
029
030import org.jdesktop.swingx.JXMultiThumbSlider;
031import org.jdesktop.swingx.multislider.ThumbRenderer;
032import org.jdesktop.swingx.util.PaintUtils;
033
034public class GradientThumbRenderer extends JComponent implements ThumbRenderer {
035    private Image thumb_black;
036    private Image thumb_gray;
037
038    public GradientThumbRenderer() {
039        super();
040    
041        try {
042            thumb_black = ImageIO.read(GradientThumbRenderer.class.getResourceAsStream("/icons/thumb_black.png"));
043            thumb_gray = ImageIO.read(GradientThumbRenderer.class.getResourceAsStream("/icons/thumb_gray.png"));
044        } catch (Exception ex)        {
045//            ex.printStackTrace();
046        }        
047    }
048    
049    private boolean selected;
050    @Override
051    protected void paintComponent(Graphics g) {
052        JComponent thumb = this;
053        int w = thumb.getWidth();
054        g.setColor(getForeground());
055        g.fillRect(0, 0, w - 1, w - 1);
056        if (selected) {
057            g.drawImage(thumb_black, 0, 0, null);
058        } else {
059            g.drawImage(thumb_gray, 0, 0, null);
060        }
061    }
062
063    public JComponent getThumbRendererComponent(JXMultiThumbSlider slider, int index, boolean selected) {
064        Color c = (Color)slider.getModel().getThumbAt(index).getObject();
065        c = PaintUtils.removeAlpha(c);
066        this.setForeground(c);
067        this.selected = selected;
068        return this;
069    }
070}