001    /* $Id: DoubleArrowIcon.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 two flat arrows 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 DoubleArrowIcon 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 = 15;
045        
046        private int size;
047        private int direction;
048        
049        
050        /**
051        * Constructs a new double arrow icon that has the default size and
052        * arrows that point in the specified direction.
053        *
054        * @param direction the direction of the arrows. One of DIRECTION_UP,
055        * DIRECTION_DOWN, DIRECTION_LEFT, or DIRECTION_RIGHTS.
056        */
057        public DoubleArrowIcon(int direction) {
058            init(DEFAULT_SIZE, direction);
059        }
060        
061        
062        /**
063        * Constructs a new double arrow icon with the specified size and
064        * arrows 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 arrows. One of DIRECTION_UP,
068        * DIRECTION_DOWN, DIRECTION_LEFT, or DIRECTION_RIGHTS
069        */
070        public DoubleArrowIcon(int size, int direction) {
071            init(size, direction);
072        }
073        
074        /** Alters the direction of the arrows.
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        /**
128        * Gets the height of this arrow icon.
129        *
130        * @return the height of this icon
131        */
132        public int getIconHeight() {
133            return size;
134        }
135        
136        
137        private void init(int size, int direction) {
138            this.size = size;
139            this.direction = direction;
140        }
141        
142        
143        private void drawArrow(Graphics g, int xo, int yo, int angle) {
144            Graphics2D g2 = (Graphics2D) g;
145            g2.setColor(Color.black);
146            
147            /*
148            *  draw the arrow
149            */
150            // draw GeneralPath (polygon)
151            int x1Points[] = {0, 0, 3, 6, 6, 5,  5,  1, 1, 0};
152            int y1Points[] = {5, 4, 1, 4, 5, 5, 13, 13, 5, 5};
153            GeneralPath polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
154            x1Points.length);
155            polygon.moveTo(x1Points[0], y1Points[0]);
156            for (int index = 1; index < x1Points.length; index++) {
157                polygon.lineTo(x1Points[index], y1Points[index]);
158            }
159            polygon.closePath();
160            
161            GeneralPath polygon2 = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
162            x1Points.length);
163            polygon2.moveTo(x1Points[0] + 8, y1Points[0]);
164            for (int index = 1; index < x1Points.length; index++) {
165                polygon2.lineTo(x1Points[index] + 8, y1Points[index]);
166            }
167            polygon2.closePath();
168            
169            /*
170            *  rotate, scale and translate
171            */
172            AffineTransform arrowt = new AffineTransform();
173            arrowt.translate(xo, yo);
174            arrowt.scale(size * 1./DEFAULT_SIZE, size * 1./DEFAULT_SIZE);
175            double center = DEFAULT_SIZE/2;
176            arrowt.rotate(angle * Math.PI/180, center, center);
177            
178            
179            /*
180            *  draw the arrow
181            */
182            AffineTransform savedt = g2.getTransform();
183            g2.transform(arrowt);
184            g2.draw(polygon);
185            g2.draw(polygon2);
186            g2.setTransform(savedt);
187        }
188        
189    }