Source for org.jfree.ui.StrokeSample

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2009, 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:  * StrokeSample.java
  29:  * -----------------
  30:  * (C) Copyright 2000-2009, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: StrokeSample.java,v 1.5 2009/02/27 13:58:41 mungady 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:  * 21-Mar-2003 : Fixed null pointer exception, bug 705126 (DG);
  42:  *
  43:  */
  44: 
  45: package org.jfree.ui;
  46: 
  47: import java.awt.Component;
  48: import java.awt.Dimension;
  49: import java.awt.Graphics;
  50: import java.awt.Graphics2D;
  51: import java.awt.Insets;
  52: import java.awt.RenderingHints;
  53: import java.awt.Stroke;
  54: import java.awt.geom.Ellipse2D;
  55: import java.awt.geom.Line2D;
  56: import java.awt.geom.Point2D;
  57: import javax.swing.JComponent;
  58: import javax.swing.JList;
  59: import javax.swing.ListCellRenderer;
  60: 
  61: /**
  62:  * A panel that displays a stroke sample.
  63:  *
  64:  * @author David Gilbert
  65:  */
  66: public class StrokeSample extends JComponent implements ListCellRenderer {
  67: 
  68:     /** The stroke being displayed (may be null). */
  69:     private Stroke stroke;
  70: 
  71:     /** The preferred size of the component. */
  72:     private Dimension preferredSize;
  73: 
  74:     /**
  75:      * Creates a StrokeSample for the specified stroke.
  76:      *
  77:      * @param stroke  the sample stroke (<code>null</code> permitted).
  78:      */
  79:     public StrokeSample(final Stroke stroke) {
  80:         this.stroke = stroke;
  81:         this.preferredSize = new Dimension(80, 18);
  82:         setPreferredSize(this.preferredSize);
  83:     }
  84: 
  85:     /**
  86:      * Returns the current Stroke object being displayed.
  87:      *
  88:      * @return The stroke (possibly <code>null</code>).
  89:      */
  90:     public Stroke getStroke() {
  91:         return this.stroke;
  92:     }
  93: 
  94:     /**
  95:      * Sets the stroke object being displayed and repaints the component.
  96:      *
  97:      * @param stroke  the stroke (<code>null</code> permitted).
  98:      */
  99:     public void setStroke(final Stroke stroke) {
 100:         this.stroke = stroke;
 101:         repaint();
 102:     }
 103: 
 104:     /**
 105:      * Returns the preferred size of the component.
 106:      *
 107:      * @return the preferred size of the component.
 108:      */
 109:     public Dimension getPreferredSize() {
 110:         return this.preferredSize;
 111:     }
 112: 
 113:     /**
 114:      * Draws a line using the sample stroke.
 115:      *
 116:      * @param g  the graphics device.
 117:      */
 118:     public void paintComponent(final Graphics g) {
 119: 
 120:         final Graphics2D g2 = (Graphics2D) g;
 121:         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
 122:                 RenderingHints.VALUE_ANTIALIAS_ON);
 123:         final Dimension size = getSize();
 124:         final Insets insets = getInsets();
 125:         final double xx = insets.left;
 126:         final double yy = insets.top;
 127:         final double ww = size.getWidth() - insets.left - insets.right;
 128:         final double hh = size.getHeight() - insets.top - insets.bottom;
 129: 
 130:         // calculate point one
 131:         final Point2D one =  new Point2D.Double(xx + 6, yy + hh / 2);
 132:         // calculate point two
 133:         final Point2D two =  new Point2D.Double(xx + ww - 6, yy + hh / 2);
 134:         // draw a circle at point one
 135:         final Ellipse2D circle1 = new Ellipse2D.Double(one.getX() - 5,
 136:                 one.getY() - 5, 10, 10);
 137:         final Ellipse2D circle2 = new Ellipse2D.Double(two.getX() - 6,
 138:                 two.getY() - 5, 10, 10);
 139: 
 140:         // draw a circle at point two
 141:         g2.draw(circle1);
 142:         g2.fill(circle1);
 143:         g2.draw(circle2);
 144:         g2.fill(circle2);
 145: 
 146:         // draw a line connecting the points
 147:         final Line2D line = new Line2D.Double(one, two);
 148:         if (this.stroke != null) {
 149:             g2.setStroke(this.stroke);
 150:             g2.draw(line);
 151:         }
 152: 
 153:     }
 154: 
 155:     /**
 156:      * Returns a list cell renderer for the stroke, so the sample can be
 157:      * displayed in a list or combo.
 158:      *
 159:      * @param list  the list.
 160:      * @param value  the value.
 161:      * @param index  the index.
 162:      * @param isSelected  selected?
 163:      * @param cellHasFocus  focussed?
 164:      *
 165:      * @return the component for rendering.
 166:      */
 167:     public Component getListCellRendererComponent(JList list, Object value,
 168:             int index, boolean isSelected, boolean cellHasFocus) {
 169:         if (value instanceof Stroke) {
 170:             setStroke((Stroke) value);
 171:         }
 172:         else {
 173:             setStroke(null);
 174:         }
 175:         return this;
 176:     }
 177: 
 178: }