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.4 2005/11/16 15:58:41 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;
053
054import javax.swing.JComponent;
055
056/**
057 * A panel that displays a paint sample.
058 *
059 * @author David Gilbert
060 */
061public class PaintSample extends JComponent {
062
063    /** The paint. */
064    private Paint paint;
065
066    /** The preferred size of the component. */
067    private Dimension preferredSize;
068
069    /**
070     * Standard constructor - builds a paint sample.
071     *
072     * @param paint   the paint to display.
073     */
074    public PaintSample(final Paint paint) {
075        this.paint = paint;
076        this.preferredSize = new Dimension(80, 12);
077    }
078
079    /**
080     * Returns the current Paint object being displayed in the panel.
081     *
082     * @return the paint.
083     */
084    public Paint getPaint() {
085        return this.paint;
086    }
087
088    /**
089     * Sets the Paint object being displayed in the panel.
090     *
091     * @param paint  the paint.
092     */
093    public void setPaint(final Paint paint) {
094        this.paint = paint;
095        repaint();
096    }
097
098    /**
099     * Returns the preferred size of the component.
100     *
101     * @return the preferred size.
102     */
103    public Dimension 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 g) {
113
114        final Graphics2D g2 = (Graphics2D) g;
115        final Dimension size = getSize();
116        final Insets 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 area = new Rectangle2D.Double(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}