001 /* $Id: ArrowIcon.java 12 2007-07-27 09:48:53Z ringler $
002 * This class is part of the de.mospace.swing library.
003 * Copyright (C) 2005-2006 Moritz Ringler
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019 package de.mospace.swing.icon;
020
021 import java.awt.Color;
022 import java.awt.Component;
023 import java.awt.Graphics;
024 import java.awt.Graphics2D;
025 import java.awt.geom.AffineTransform;
026 import java.awt.geom.GeneralPath;
027 import javax.swing.Icon;
028
029 /** A java icon with a single flat arrow pointing up, left, right, or down.
030 *
031 * @version $Revision: 12 $ ($Date: 2007-07-27 11:48:53 +0200 (Fr, 27 Jul 2007) $)
032 * @author Moritz Ringler
033 */
034 public class ArrowIcon implements Icon {
035 /** Constant for arrows pointing up. */
036 public final static int DIRECTION_UP = 1;
037 /** Constant for arrows pointing down. */
038 public final static int DIRECTION_DOWN = 3;
039 /** Constant for arrows pointing left. */
040 public final static int DIRECTION_LEFT = 2;
041 /** Constant for arrows pointing right. */
042 public final static int DIRECTION_RIGHT = 0;
043 /** The default size of this icon. */
044 private final static int DEFAULT_SIZE = 11;
045
046 private int size;
047 private int direction;
048
049
050 /**
051 * Constructs a new arrow icon that has the default size and an
052 * arrow that points in the specified direction.
053 *
054 * @param direction the direction of the arrow. One of DIRECTION_UP,
055 * DIRECTION_DOWN, DIRECTION_LEFT, or DIRECTION_RIGHTS.
056 */
057 public ArrowIcon(int direction){
058 init(DEFAULT_SIZE, direction);
059 }
060
061
062 /**
063 * Constructs a new arrow icon with the specified size and an
064 * arrow that points in the specified direction.
065 *
066 * @param size the size for the new icon in pixels
067 * @param direction the direction of the arrow. One of DIRECTION_UP,
068 * DIRECTION_DOWN, DIRECTION_LEFT, or DIRECTION_RIGHTS
069 */
070 public ArrowIcon(int size, int direction){
071 init(size, direction);
072 }
073
074 /** Alters the direction of the arrow.
075 * @param d the new direction. One of DIRECTION_UP,
076 * DIRECTION_DOWN, DIRECTION_LEFT, or DIRECTION_RIGHTS.
077 */
078 public void setDirection(int d){
079 direction = d % 4;
080 }
081
082 /** Returns the current direction of the arrow.
083 * @return the current direction of the arrow. One of DIRECTION_UP,
084 * DIRECTION_DOWN, DIRECTION_LEFT, or DIRECTION_RIGHTS.
085 */
086 public int getDirection(){
087 return direction;
088 }
089
090 /** Paints this icon on the specified graphics object at the
091 * specified location.
092 * @param c ignored, may be <code>null</code>
093 * @param g the graphics object to paint on
094 * @param x the x-offset for this icon
095 * @param y the y-offset for this icon
096 */
097 public void paintIcon(Component c, Graphics g, int x, int y){
098 switch (direction) {
099 case DIRECTION_DOWN:
100 drawArrow(g, x, y, 180);
101 break;
102 case DIRECTION_UP:
103 drawArrow(g, x, y, 0);
104 break;
105 case DIRECTION_RIGHT:
106 drawArrow(g, x, y, 90);
107 break;
108 case DIRECTION_LEFT:
109 drawArrow(g, x, y, 270);
110 break;
111 default:
112 drawArrow(g, x, y, 90);
113 break;
114 }
115 }
116
117 /**
118 * Gets the width of this arrow icon.
119 *
120 * @return the width of this icon
121 */
122 public int getIconWidth(){
123 return size;
124 }
125
126 /**
127 * Gets the height of this arrow icon.
128 *
129 * @return the height of this icon
130 */
131 public int getIconHeight(){
132 return size;
133 }
134
135
136 private void init(int size, int direction){
137 this.size = size;
138 this.direction = direction;
139 }
140
141
142 private void drawArrow(Graphics g, int xo, int yo, int angle){
143 Graphics2D g2 = (Graphics2D) g;
144 g2.setColor(Color.black);
145
146 /*
147 * draw the arrow
148 */
149 // draw GeneralPath (polygon)
150 int x1Points[] = {1, 5, 9, 7, 7, 3, 3, 1};
151 int y1Points[] = {4, 0, 4, 4, 10, 10, 4, 4};
152 GeneralPath polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
153 x1Points.length);
154 polygon.moveTo(x1Points[0], y1Points[0]);
155 for (int index = 1; index < x1Points.length; index++) {
156 polygon.lineTo(x1Points[index], y1Points[index]);
157 }
158 polygon.closePath();
159
160 /*
161 * rotate, scale and translate
162 */
163 AffineTransform arrowt = new AffineTransform();
164 arrowt.translate(xo, yo);
165 arrowt.scale(size * 1./DEFAULT_SIZE, size * 1./DEFAULT_SIZE);
166 double center = DEFAULT_SIZE/2;
167 arrowt.rotate(angle * Math.PI/180, center, center);
168
169
170 /*
171 * draw the arrow
172 */
173 AffineTransform savedt = g2.getTransform();
174 g2.transform(arrowt);
175 g2.draw(polygon);
176 g2.setTransform(savedt);
177 }
178
179 }