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