KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > ui > PaletteSample


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ------------------
27  * PaletteSample.java
28  * ------------------
29  * (C) Copyright 2002-2004, by David M. O'Donnell.
30  *
31  * Original Author: David M. O'Donnell;
32  * Contributor(s): David Gilbert (for Object Refinery Limited);
33  *
34  * $Id: PaletteSample.java,v 1.2 2005/03/29 12:56:52 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 21-Jan-2003 : Added standard header (DG);
39  *
40  */

41
42 package org.jfree.chart.ui;
43
44 import java.awt.BasicStroke JavaDoc;
45 import java.awt.Component JavaDoc;
46 import java.awt.Dimension JavaDoc;
47 import java.awt.Graphics JavaDoc;
48 import java.awt.Graphics2D JavaDoc;
49 import java.awt.Insets JavaDoc;
50 import java.awt.RenderingHints JavaDoc;
51 import java.awt.geom.Line2D JavaDoc;
52
53 import javax.swing.JComponent JavaDoc;
54 import javax.swing.JList JavaDoc;
55 import javax.swing.ListCellRenderer JavaDoc;
56
57
58 /**
59  * A panel that displays a palette sample.
60  *
61  * @author David M. O'Donnell
62  */

63 public class PaletteSample extends JComponent JavaDoc implements ListCellRenderer JavaDoc {
64
65     /** The palette being displayed. */
66     private ColorPalette palette;
67
68     /** The preferred size of the component; */
69     private Dimension JavaDoc preferredSize;
70
71     /**
72      * Creates a new sample.
73      *
74      * @param palette the palette.
75      */

76     public PaletteSample(ColorPalette palette) {
77         this.palette = palette;
78         this.preferredSize = new Dimension JavaDoc(80, 18);
79     }
80
81     /**
82      * Returns a list cell renderer for the stroke, so the sample can be
83      * displayed in a list or combo.
84      *
85      * @param list the list component.
86      * @param value the value.
87      * @param index the index.
88      * @param isSelected a flag that indicates whether or not the item is
89      * selected.
90      * @param cellHasFocus a flag that indicates whether or not the cell has
91      * the focus.
92      *
93      * @return The renderer.
94      */

95     public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value,
96                                                   int index,
97                                                   boolean isSelected,
98                                                   boolean cellHasFocus) {
99         if (value instanceof PaletteSample) {
100             PaletteSample in = (PaletteSample) value;
101             setPalette(in.getPalette());
102         }
103         return this;
104     }
105
106     /**
107      * Returns the current palette object being displayed.
108      *
109      * @return The palette.
110      */

111     public ColorPalette getPalette() {
112         return this.palette;
113     }
114
115     /**
116      * Returns the preferred size of the component.
117      *
118      * @return The preferred size.
119      */

120     public Dimension JavaDoc getPreferredSize() {
121         return this.preferredSize;
122     }
123
124     /**
125      * Draws the sample.
126      *
127      * @param g the graphics device.
128      */

129     public void paintComponent(Graphics JavaDoc g) {
130
131         Graphics2D JavaDoc g2 = (Graphics2D JavaDoc) g;
132         g2.setRenderingHint(
133             RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF
134         );
135         Dimension JavaDoc size = getSize();
136         Insets JavaDoc insets = getInsets();
137         double ww = size.getWidth() - insets.left - insets.right;
138         double hh = size.getHeight() - insets.top - insets.bottom;
139
140         g2.setStroke(new BasicStroke JavaDoc(1.0f));
141
142         double y1 = insets.top;
143         double y2 = y1 + hh;
144         double xx = insets.left;
145         Line2D JavaDoc line = new Line2D.Double JavaDoc();
146         int count = 0;
147         while (xx <= insets.left + ww) {
148             count++;
149             line.setLine(xx, y1, xx, y2);
150             g2.setPaint(this.palette.getColor(count));
151             g2.draw(line);
152             xx += 1;
153         }
154     }
155
156     /**
157      * Sets the palette object being displayed.
158      *
159      * @param palette the palette.
160      */

161     public void setPalette(ColorPalette palette) {
162         this.palette = palette;
163         this.repaint();
164     }
165
166 }
167
Popular Tags