01: /*
02: * $Id: GradientThumbRenderer.java,v 1.5 2006/07/21 08:27:58 joshy Exp $
03: *
04: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
05: * Santa Clara, California 95054, U.S.A. All rights reserved.
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this library; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20: */
21: package org.jdesktop.swingx.color;
22:
23: import java.awt.Color;
24: import java.awt.Graphics;
25: import java.awt.Graphics2D;
26: import java.awt.Image;
27: import javax.imageio.ImageIO;
28: import javax.swing.JComponent;
29: import org.jdesktop.swingx.JXMultiThumbSlider;
30: import org.jdesktop.swingx.JXGradientChooser;
31: import org.jdesktop.swingx.multislider.ThumbRenderer;
32:
33: public class GradientThumbRenderer extends JComponent implements
34: ThumbRenderer {
35: private final JXGradientChooser gradientPicker;
36: private JXMultiThumbSlider slider;
37: private Image thumb_black;
38: private Image thumb_gray;
39:
40: public GradientThumbRenderer(JXGradientChooser gradientPicker,
41: JXMultiThumbSlider slider) {
42: super ();
43: this .gradientPicker = gradientPicker;
44: this .slider = slider;
45:
46: try {
47: thumb_black = ImageIO.read(this .getClass()
48: .getResourceAsStream("/icons/thumb_black.png"));
49: thumb_gray = ImageIO.read(this .getClass()
50: .getResourceAsStream("/icons/thumb_gray.png"));
51: } catch (Exception ex) {
52: ex.printStackTrace();
53: }
54: }
55:
56: private boolean selected;
57:
58: protected void paintComponent(Graphics g) {
59: JComponent thumb = this ;
60: int w = thumb.getWidth();
61: g.setColor(getForeground());
62: g.fillRect(0, 0, w - 1, w - 1);
63: if (selected) {
64: g.drawImage(thumb_black, 0, 0, null);
65: } else {
66: g.drawImage(thumb_gray, 0, 0, null);
67: }
68: }
69:
70: public JComponent getThumbRendererComponent(
71: JXMultiThumbSlider slider, int index, boolean selected) {
72: Color c = (Color) gradientPicker.getSlider().getModel()
73: .getThumbAt(index).getObject();
74: c = ColorUtil.removeAlpha(c);
75: this.setForeground(c);
76: this.selected = selected;
77: return this;
78: }
79: }
|