KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > editor > 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
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ------------------
28  * PaletteSample.java
29  * ------------------
30  * (C) Copyright 2002-2004, by David M. O'Donnell.
31  *
32  * Original Author: David M. O'Donnell;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: PaletteSample.java,v 1.1.2.1 2005/11/24 16:11:48 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 21-Jan-2003 : Added standard header (DG);
40  *
41  */

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

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

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

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

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

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

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

164     public void setPalette(ColorPalette palette) {
165         this.palette = palette;
166         this.repaint();
167     }
168
169 }
170
Popular Tags