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: * StrokeSample.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: StrokeSample.java,v 1.3 2005/10/18 13:18:34 mungady 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: * 21-Mar-2003 : Fixed null pointer exception, bug 705126 (DG);
042: *
043: */
044:
045: package org.jfree.ui;
046:
047: import java.awt.BasicStroke;
048: import java.awt.Component;
049: import java.awt.Dimension;
050: import java.awt.Graphics;
051: import java.awt.Graphics2D;
052: import java.awt.Insets;
053: import java.awt.RenderingHints;
054: import java.awt.Stroke;
055: import java.awt.geom.Ellipse2D;
056: import java.awt.geom.Line2D;
057: import java.awt.geom.Point2D;
058:
059: import javax.swing.JComponent;
060: import javax.swing.JList;
061: import javax.swing.ListCellRenderer;
062:
063: /**
064: * A panel that displays a stroke sample.
065: *
066: * @author David Gilbert
067: */
068: public class StrokeSample extends JComponent implements
069: ListCellRenderer {
070:
071: /** The stroke being displayed. */
072: private Stroke stroke;
073:
074: /** The preferred size of the component. */
075: private Dimension preferredSize;
076:
077: /**
078: * Creates a StrokeSample for the specified stroke.
079: *
080: * @param stroke the sample stroke.
081: */
082: public StrokeSample(final Stroke stroke) {
083: this .stroke = stroke;
084: this .preferredSize = new Dimension(80, 18);
085: }
086:
087: /**
088: * Returns the current Stroke object being displayed.
089: *
090: * @return the stroke.
091: */
092: public Stroke getStroke() {
093: return this .stroke;
094: }
095:
096: /**
097: * Sets the Stroke object being displayed.
098: *
099: * @param stroke the stroke.
100: */
101: public void setStroke(final Stroke stroke) {
102: this .stroke = stroke;
103: repaint();
104: }
105:
106: /**
107: * Returns the preferred size of the component.
108: *
109: * @return the preferred size of the component.
110: */
111: public Dimension getPreferredSize() {
112: return this .preferredSize;
113: }
114:
115: /**
116: * Draws a line using the sample stroke.
117: *
118: * @param g the graphics device.
119: */
120: public void paintComponent(final Graphics g) {
121:
122: final Graphics2D g2 = (Graphics2D) g;
123: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
124: RenderingHints.VALUE_ANTIALIAS_ON);
125: final Dimension size = getSize();
126: final Insets insets = getInsets();
127: final double xx = insets.left;
128: final double yy = insets.top;
129: final double ww = size.getWidth() - insets.left - insets.right;
130: final double hh = size.getHeight() - insets.top - insets.bottom;
131:
132: // calculate point one
133: final Point2D one = new Point2D.Double(xx + 6, yy + hh / 2);
134: // calculate point two
135: final Point2D two = new Point2D.Double(xx + ww - 6, yy + hh / 2);
136: // draw a circle at point one
137: final Ellipse2D circle1 = new Ellipse2D.Double(one.getX() - 5,
138: one.getY() - 5, 10, 10);
139: final Ellipse2D circle2 = new Ellipse2D.Double(two.getX() - 6,
140: two.getY() - 5, 10, 10);
141:
142: // draw a circle at point two
143: g2.draw(circle1);
144: g2.fill(circle1);
145: g2.draw(circle2);
146: g2.fill(circle2);
147:
148: // draw a line connecting the points
149: final Line2D line = new Line2D.Double(one, two);
150: if (this .stroke != null) {
151: g2.setStroke(this .stroke);
152: } else {
153: g2.setStroke(new BasicStroke(0.0f));
154: }
155: g2.draw(line);
156:
157: }
158:
159: /**
160: * Returns a list cell renderer for the stroke, so the sample can be displayed in a list or
161: * combo.
162: *
163: * @param list the list.
164: * @param value the value.
165: * @param index the index.
166: * @param isSelected selected?
167: * @param cellHasFocus focussed?
168: *
169: * @return the component for rendering.
170: */
171: public Component getListCellRendererComponent(final JList list,
172: final Object value, final int index,
173: final boolean isSelected, final boolean cellHasFocus) {
174: if (value instanceof StrokeSample) {
175: final StrokeSample in = (StrokeSample) value;
176: setStroke(in.getStroke());
177: }
178: return this;
179: }
180:
181: }
|