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: * ArrowPanel.java
029: * ---------------
030: * (C) Copyright 2002-2004, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: ArrowPanel.java,v 1.5 2005/11/16 15:58:41 taqua Exp $
036: *
037: * Changes
038: * -------
039: * 25-Sep-2002 : Version 1 (DG);
040: * 13-Oct-2002 : Added Javadocs (DG);
041: *
042: */
043:
044: package org.jfree.ui;
045:
046: import java.awt.Dimension;
047: import java.awt.Graphics;
048: import java.awt.Graphics2D;
049: import java.awt.Insets;
050: import java.awt.Polygon;
051: import java.awt.Shape;
052: import java.awt.geom.Rectangle2D;
053:
054: import javax.swing.JPanel;
055:
056: /**
057: * A basic panel that displays a small up or down arrow.
058: *
059: * @author David Gilbert
060: */
061: public class ArrowPanel extends JPanel {
062:
063: /** A constant for the up arrow. */
064: public static final int UP = 0;
065:
066: /** A constant for the down arrow. */
067: public static final int DOWN = 1;
068:
069: /** The arrow type. */
070: private int type = UP;
071:
072: /** The available area. */
073: private Rectangle2D available = new Rectangle2D.Float();
074:
075: /**
076: * Creates a new arrow panel.
077: *
078: * @param type the arrow type.
079: */
080: public ArrowPanel(final int type) {
081: this .type = type;
082: setPreferredSize(new Dimension(14, 9));
083: }
084:
085: /**
086: * Paints the arrow panel.
087: *
088: * @param g the graphics device for drawing on.
089: */
090: public void paintComponent(final Graphics g) {
091:
092: super .paintComponent(g);
093: final Graphics2D g2 = (Graphics2D) g;
094:
095: // first determine the size of the drawing area...
096: final Dimension size = getSize();
097: final Insets insets = getInsets();
098: this .available.setRect(insets.left, insets.top, size.getWidth()
099: - insets.left - insets.right, size.getHeight()
100: - insets.top - insets.bottom);
101: g2.translate(insets.left, insets.top);
102: g2.fill(getArrow(this .type));
103:
104: }
105:
106: /**
107: * Returns a shape for the arrow.
108: *
109: * @param t the arrow type.
110: *
111: * @return the arrow shape.
112: */
113: private Shape getArrow(final int t) {
114: switch (t) {
115: case UP:
116: return getUpArrow();
117: case DOWN:
118: return getDownArrow();
119: default:
120: return getUpArrow();
121: }
122: }
123:
124: /**
125: * Returns an up arrow.
126: *
127: * @return an up arrow.
128: */
129: private Shape getUpArrow() {
130: final Polygon result = new Polygon();
131: result.addPoint(7, 2);
132: result.addPoint(2, 7);
133: result.addPoint(12, 7);
134: return result;
135: }
136:
137: /**
138: * Returns a down arrow.
139: *
140: * @return a down arrow.
141: */
142: private Shape getDownArrow() {
143: final Polygon result = new Polygon();
144: result.addPoint(7, 7);
145: result.addPoint(2, 2);
146: result.addPoint(12, 2);
147: return result;
148: }
149:
150: }
|