001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 */
028
029package org.jfree.chart.ui;
030
031import java.awt.Color;
032import java.awt.Dimension;
033import java.awt.Graphics;
034import java.awt.Graphics2D;
035import java.awt.Insets;
036import java.awt.Paint;
037import java.awt.geom.Rectangle2D;
038import javax.swing.JComponent;
039
040/**
041 * A panel that displays a paint sample.
042 */
043public class PaintSample extends JComponent {
044
045    /** The paint. */
046    private Paint paint;
047
048    /** The preferred size of the component. */
049    private Dimension preferredSize;
050
051    /**
052     * Standard constructor - builds a paint sample.
053     *
054     * @param paint  the paint to display.
055     */
056    public PaintSample(Paint paint) {
057        this.paint = paint;
058        this.preferredSize = new Dimension(80, 12);
059    }
060
061    /**
062     * Returns the current Paint object being displayed in the panel.
063     *
064     * @return the paint.
065     */
066    public Paint getPaint() {
067        return this.paint;
068    }
069
070    /**
071     * Sets the Paint object being displayed in the panel.
072     *
073     * @param paint  the paint.
074     */
075    public void setPaint(Paint paint) {
076        this.paint = paint;
077        repaint();
078    }
079
080    /**
081     * Returns the preferred size of the component.
082     *
083     * @return the preferred size.
084     */
085    @Override
086    public Dimension getPreferredSize() {
087        return this.preferredSize;
088    }
089
090    /**
091     * Fills the component with the current Paint.
092     *
093     * @param g  the graphics device.
094     */
095    @Override
096    public void paintComponent(Graphics g) {
097
098        final Graphics2D g2 = (Graphics2D) g;
099        final Dimension size = getSize();
100        final Insets insets = getInsets();
101        final double xx = insets.left;
102        final double yy = insets.top;
103        final double ww = size.getWidth() - insets.left - insets.right - 1;
104        final double hh = size.getHeight() - insets.top - insets.bottom - 1;
105        final Rectangle2D area = new Rectangle2D.Double(xx, yy, ww, hh);
106        g2.setPaint(this.paint);
107        g2.fill(area);
108        g2.setPaint(Color.BLACK);
109        g2.draw(area);
110
111    }
112
113}
114