001/*
002 * $Id: GradientTrackRenderer.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.Graphics2D;
026import java.awt.LinearGradientPaint;
027import java.awt.MultipleGradientPaint;
028import java.awt.Paint;
029import java.awt.Rectangle;
030import java.awt.geom.Point2D;
031import java.awt.geom.Rectangle2D;
032import java.util.List;
033
034import javax.swing.JComponent;
035
036import org.jdesktop.swingx.JXMultiThumbSlider;
037import org.jdesktop.swingx.multislider.Thumb;
038import org.jdesktop.swingx.multislider.TrackRenderer;
039import org.jdesktop.swingx.util.PaintUtils;
040
041/**
042 * <p><b>Dependency</b>: Because this class relies on LinearGradientPaint and
043 * RadialGradientPaint, it requires the optional MultipleGradientPaint.jar</p>
044 */
045public class GradientTrackRenderer extends JComponent implements TrackRenderer {
046    private Paint checker_paint;
047
048    public GradientTrackRenderer() {
049        checker_paint = PaintUtils.getCheckerPaint();
050    }
051    
052    private JXMultiThumbSlider slider;
053    
054    @Override
055    public void paint(Graphics g) {
056        super.paint(g);
057        paintComponent(g);
058    }
059
060    @Override
061    protected void paintComponent(Graphics gfx) {
062        Graphics2D g = (Graphics2D)gfx;
063        
064        // get the list of colors
065        List<Thumb<Color>> stops = slider.getModel().getSortedThumbs();
066        int len = stops.size();
067
068        // set up the data for the gradient
069        float[] fractions = new float[len];
070        Color[] colors = new Color[len];
071        int i = 0;
072        for(Thumb<Color> thumb : stops) {
073            colors[i] = (Color)thumb.getObject();
074            fractions[i] = thumb.getPosition();
075            i++;
076        }
077
078        // calculate the track area
079        int thumb_width = 12;
080        int track_width = slider.getWidth() - thumb_width;
081        g.translate(thumb_width / 2, 12);
082        Rectangle2D rect = new Rectangle(0, 0, track_width, 20);
083
084        // fill in the checker
085        g.setPaint(checker_paint);
086        g.fill(rect);
087
088        // fill in the gradient
089        Point2D start = new Point2D.Float(0,0);
090        Point2D end = new Point2D.Float(track_width,0);
091        MultipleGradientPaint paint = new LinearGradientPaint(
092                (float)start.getX(),
093                (float)start.getY(),
094                (float)end.getX(),
095                (float)end.getY(),
096                fractions,colors);
097        g.setPaint(paint);
098        g.fill(rect);
099
100        // draw a border
101        g.setColor(Color.black);
102        g.draw(rect);
103        g.translate(-thumb_width / 2, -12);
104    }
105
106    public JComponent getRendererComponent(JXMultiThumbSlider slider) {
107        this.slider = slider;
108        return this;
109    }
110}