001    /*
002    *  $Id: PopupIcon.java 12 2007-07-27 09:48:53Z ringler $
003    *  This class is part of the de.mospace.swing library.
004    *  Copyright (C) 2005-2006 Moritz Ringler
005    *
006    *  This program is free software; you can redistribute it and/or
007    *  modify it under the terms of the GNU General Public License
008    *  as published by the Free Software Foundation; either version 2
009    *  of the License, or (at your option) any later version.
010    *
011    *  This program is distributed in the hope that it will be useful,
012    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014    *  GNU General Public License for more details.
015    *
016    *  You should have received a copy of the GNU General Public License
017    *  along with this program; if not, write to the Free Software
018    *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
019    */
020    package de.mospace.swing.icon;
021    
022    import java.awt.Color;
023    import java.awt.Component;
024    import java.awt.Graphics;
025    import java.awt.Graphics2D;
026    import java.awt.geom.AffineTransform;
027    import java.awt.geom.GeneralPath;
028    import javax.swing.Icon;
029    
030    /** A Java icon with a black triangle pointing downwards, similar to the
031    * combo box control for displaying the combo box selection.
032    * @author Moritz Ringler
033    * @version $Revision: 12 $ ($Date: 2007-07-27 11:48:53 +0200 (Fr, 27 Jul 2007) $)
034    */
035    public class PopupIcon implements Icon {
036        /** The default size of this icon. */
037        private final static int DEFAULT_SIZE = 10;
038    
039        private int size = DEFAULT_SIZE;
040    
041    
042        /**
043        * Constructs a new PopupIcon that has the default size.
044        *
045        */
046        public PopupIcon() {
047            //sole constructor
048        }
049    
050    
051        /**
052        * Constructs a new PopupIcon with the specified size.
053        *
054        * @param size the width and height of the new icon
055        */
056        public PopupIcon(int size) {
057            this.size = size;
058        }
059    
060    
061        /** Paints this icon on the specified graphics object at the
062        * specified location.
063        * @param c ignored, may be <code>null</code>
064        * @param g the graphics object to paint on
065        * @param xo the x-offset for this icon
066        * @param yo the y-offset for this icon
067        */
068        public void paintIcon(Component c, Graphics g, int xo, int yo) {
069            Graphics2D g2 = (Graphics2D) g;
070            g2.setColor(Color.black);
071    
072            /*
073            *  draw the arrow
074            */
075            // draw GeneralPath (polygon)
076            int x1Points[] = {0, 9, 5, 4, 0};
077            int y1Points[] = {0, 0, 9, 9, 0};
078            GeneralPath polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
079            x1Points.length);
080            polygon.moveTo(x1Points[0], y1Points[0]);
081            for (int index = 1; index < x1Points.length; index++) {
082                polygon.lineTo(x1Points[index], y1Points[index]);
083            }
084            polygon.closePath();
085    
086            /*
087            *  rotate, scale and translate
088            */
089            AffineTransform arrowt = new AffineTransform();
090            arrowt.translate(xo, yo);
091            arrowt.scale(size * 1. / DEFAULT_SIZE, size * 1. / DEFAULT_SIZE);
092            //double center = DEFAULT_SIZE / 2;
093    
094            /*
095            *  draw the arrow
096            */
097            AffineTransform savedt = g2.getTransform();
098            g2.transform(arrowt);
099    
100            g2.setPaint(Color.black);
101            g2.fill(polygon);
102            g2.draw(polygon);
103            g2.setTransform(savedt);
104    
105        }
106    
107        /**
108        * Gets the width of this icon.
109        *
110        * @return the width of this icon
111        */
112        public int getIconWidth() {
113            return size;
114        }
115    
116    
117       /**
118        * Gets the height of this icon.
119        *
120        * @return the height of this icon
121        */
122        public int getIconHeight() {
123            return size;
124        }
125    
126    }