Source for 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;
  47: import java.awt.Dimension;
  48: import java.awt.Graphics;
  49: import java.awt.Graphics2D;
  50: import java.awt.Insets;
  51: import java.awt.Paint;
  52: import java.awt.geom.Rectangle2D;
  53: 
  54: import javax.swing.JComponent;
  55: 
  56: /**
  57:  * A panel that displays a paint sample.
  58:  *
  59:  * @author David Gilbert
  60:  */
  61: public class PaintSample extends JComponent {
  62: 
  63:     /** The paint. */
  64:     private Paint paint;
  65: 
  66:     /** The preferred size of the component. */
  67:     private Dimension preferredSize;
  68: 
  69:     /**
  70:      * Standard constructor - builds a paint sample.
  71:      *
  72:      * @param paint   the paint to display.
  73:      */
  74:     public PaintSample(final Paint paint) {
  75:         this.paint = paint;
  76:         this.preferredSize = new Dimension(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 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 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 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: }