KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > ui > PaintSample


1 /* ========================================================================
2  * JCommon : a free general purpose class 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/jcommon/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  * PaintSample.java
29  * ----------------
30  * (C) Copyright 2000-2004, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: PaintSample.java,v 1.4 2005/11/16 15:58:41 taqua Exp $
36  *
37  * Changes (from 26-Oct-2001)
38  * --------------------------
39  * 26-Oct-2001 : Changed package to com.jrefinery.ui.*;
40  * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
41  *
42  */

43
44 package org.jfree.ui;
45
46 import java.awt.Color 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.Paint JavaDoc;
52 import java.awt.geom.Rectangle2D JavaDoc;
53
54 import javax.swing.JComponent JavaDoc;
55
56 /**
57  * A panel that displays a paint sample.
58  *
59  * @author David Gilbert
60  */

61 public class PaintSample extends JComponent JavaDoc {
62
63     /** The paint. */
64     private Paint JavaDoc paint;
65
66     /** The preferred size of the component. */
67     private Dimension JavaDoc preferredSize;
68
69     /**
70      * Standard constructor - builds a paint sample.
71      *
72      * @param paint the paint to display.
73      */

74     public PaintSample(final Paint JavaDoc paint) {
75         this.paint = paint;
76         this.preferredSize = new Dimension JavaDoc(80, 12);
77     }
78
79     /**
80      * Returns the current Paint object being displayed in the panel.
81      *
82      * @return the paint.
83      */

84     public Paint JavaDoc getPaint() {
85         return this.paint;
86     }
87
88     /**
89      * Sets the Paint object being displayed in the panel.
90      *
91      * @param paint the paint.
92      */

93     public void setPaint(final Paint JavaDoc paint) {
94         this.paint = paint;
95         repaint();
96     }
97
98     /**
99      * Returns the preferred size of the component.
100      *
101      * @return the preferred size.
102      */

103     public Dimension JavaDoc getPreferredSize() {
104         return this.preferredSize;
105     }
106
107     /**
108      * Fills the component with the current Paint.
109      *
110      * @param g the graphics device.
111      */

112     public void paintComponent(final Graphics JavaDoc g) {
113
114         final Graphics2D JavaDoc g2 = (Graphics2D JavaDoc) g;
115         final Dimension JavaDoc size = getSize();
116         final Insets JavaDoc insets = getInsets();
117         final double xx = insets.left;
118         final double yy = insets.top;
119         final double ww = size.getWidth() - insets.left - insets.right - 1;
120         final double hh = size.getHeight() - insets.top - insets.bottom - 1;
121         final Rectangle2D JavaDoc area = new Rectangle2D.Double JavaDoc(xx, yy, ww, hh);
122         g2.setPaint(this.paint);
123         g2.fill(area);
124         g2.setPaint(Color.black);
125         g2.draw(area);
126
127     }
128
129 }
130
Popular Tags