001/* ========================================================================
002 * JCommon : a free general purpose class library for the Java(tm) platform
003 * ========================================================================
004 *
005 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006 * 
007 * Project Info:  http://www.jfree.org/jcommon/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it 
010 * under the terms of the GNU Lesser General Public License as published by 
011 * the Free Software Foundation; either version 2.1 of the License, or 
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but 
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022 * USA.  
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025 * in the United States and other countries.]
026 * 
027 * ----------------
028 * PaintSample.java
029 * ----------------
030 * (C) Copyright 2000-2004, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * $Id: PaintSample.java,v 1.5 2007/11/02 17:50:36 taqua Exp $
036 *
037 * Changes (from 26-Oct-2001)
038 * --------------------------
039 * 26-Oct-2001 : Changed package to com.jrefinery.ui.*;
040 * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
041 *
042 */
043
044package org.jfree.ui;
045
046import java.awt.Color;
047import java.awt.Dimension;
048import java.awt.Graphics;
049import java.awt.Graphics2D;
050import java.awt.Insets;
051import java.awt.Paint;
052import java.awt.geom.Rectangle2D;
053import javax.swing.JComponent;
054
055/**
056 * A panel that displays a paint sample.
057 *
058 * @author David Gilbert
059 */
060public class PaintSample extends JComponent {
061
062    /** The paint. */
063    private Paint paint;
064
065    /** The preferred size of the component. */
066    private Dimension preferredSize;
067
068    /**
069     * Standard constructor - builds a paint sample.
070     *
071     * @param paint   the paint to display.
072     */
073    public PaintSample(final Paint paint) {
074        this.paint = paint;
075        this.preferredSize = new Dimension(80, 12);
076    }
077
078    /**
079     * Returns the current Paint object being displayed in the panel.
080     *
081     * @return the paint.
082     */
083    public Paint getPaint() {
084        return this.paint;
085    }
086
087    /**
088     * Sets the Paint object being displayed in the panel.
089     *
090     * @param paint  the paint.
091     */
092    public void setPaint(final Paint paint) {
093        this.paint = paint;
094        repaint();
095    }
096
097    /**
098     * Returns the preferred size of the component.
099     *
100     * @return the preferred size.
101     */
102    public Dimension getPreferredSize() {
103        return this.preferredSize;
104    }
105
106    /**
107     * Fills the component with the current Paint.
108     *
109     * @param g  the graphics device.
110     */
111    public void paintComponent(final Graphics g) {
112
113        final Graphics2D g2 = (Graphics2D) g;
114        final Dimension size = getSize();
115        final Insets insets = getInsets();
116        final double xx = insets.left;
117        final double yy = insets.top;
118        final double ww = size.getWidth() - insets.left - insets.right - 1;
119        final double hh = size.getHeight() - insets.top - insets.bottom - 1;
120        final Rectangle2D area = new Rectangle2D.Double(xx, yy, ww, hh);
121        g2.setPaint(this.paint);
122        g2.fill(area);
123        g2.setPaint(Color.black);
124        g2.draw(area);
125
126    }
127
128}