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.5 2007/11/02 17:50:36 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: import javax.swing.JComponent;
  54: 
  55: /**
  56:  * A panel that displays a paint sample.
  57:  *
  58:  * @author David Gilbert
  59:  */
  60: public class PaintSample extends JComponent {
  61: 
  62:     /** The paint. */
  63:     private Paint paint;
  64: 
  65:     /** The preferred size of the component. */
  66:     private Dimension preferredSize;
  67: 
  68:     /**
  69:      * Standard constructor - builds a paint sample.
  70:      *
  71:      * @param paint   the paint to display.
  72:      */
  73:     public PaintSample(final Paint paint) {
  74:         this.paint = paint;
  75:         this.preferredSize = new Dimension(80, 12);
  76:     }
  77: 
  78:     /**
  79:      * Returns the current Paint object being displayed in the panel.
  80:      *
  81:      * @return the paint.
  82:      */
  83:     public Paint getPaint() {
  84:         return this.paint;
  85:     }
  86: 
  87:     /**
  88:      * Sets the Paint object being displayed in the panel.
  89:      *
  90:      * @param paint  the paint.
  91:      */
  92:     public void setPaint(final Paint paint) {
  93:         this.paint = paint;
  94:         repaint();
  95:     }
  96: 
  97:     /**
  98:      * Returns the preferred size of the component.
  99:      *
 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: }