001    /*
002     * $Id: ShapeUtils.java 3100 2008-10-14 22:33:10Z rah003 $
003     *
004     * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
005     * Santa Clara, California 95054, U.S.A. All rights reserved.
006     *
007     * This library is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU Lesser General Public
009     * License as published by the Free Software Foundation; either
010     * version 2.1 of the License, or (at your option) any later version.
011     * 
012     * This library is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015     * Lesser General Public License for more details.
016     * 
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this library; if not, write to the Free Software
019     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020     */
021    package org.jdesktop.swingx.util;
022    
023    import java.awt.Font;
024    import java.awt.Graphics2D;
025    import java.awt.Polygon;
026    import java.awt.Shape;
027    import java.awt.font.GlyphVector;
028    import java.awt.geom.AffineTransform;
029    import java.awt.geom.Ellipse2D;
030    import java.awt.geom.GeneralPath;
031    import java.awt.geom.Point2D;
032    import java.awt.geom.Rectangle2D;
033    import java.awt.image.BufferedImage;
034    
035    /**
036     *
037     * @author joshy
038     */
039    public final class ShapeUtils {
040        
041        /** Creates a new instance of ShapeUtils */
042        private ShapeUtils() { }
043        
044        public static Shape generatePolygon(int sides, int outsideRadius, boolean normalize) {
045            return generatePolygon(sides, outsideRadius, 0, normalize);
046        }
047        
048        public static Shape generatePolygon(int sides, int outsideRadius, int insideRadius, boolean normalize) {
049            Shape shape = generatePolygon(sides,outsideRadius,insideRadius);
050            if(normalize) {
051                Rectangle2D bounds = shape.getBounds2D();
052                GeneralPath path = new GeneralPath(shape);
053                shape = path.createTransformedShape(
054                        AffineTransform.getTranslateInstance(-bounds.getX(), -bounds.getY()));
055            }
056            return shape;
057        }
058        
059        
060        public static Shape generatePolygon(int sides, int outsideRadius, int insideRadius) {
061            
062            if(sides < 3) {
063                return new Ellipse2D.Float(0,0,10,10);
064            }
065            
066            AffineTransform trans = new AffineTransform();
067            Polygon poly = new Polygon();
068            for(int i=0; i<sides; i++) {
069                trans.rotate(Math.PI*2/(float)sides/2);
070                Point2D out = trans.transform(new Point2D.Float(0,outsideRadius),null);
071                poly.addPoint((int)out.getX(), (int)out.getY());
072                trans.rotate(Math.PI*2/(float)sides/2);
073                if(insideRadius > 0) {
074                    Point2D in = trans.transform(new Point2D.Float(0,insideRadius),null);
075                    poly.addPoint((int)in.getX(), (int)in.getY());
076                }
077            }
078            
079            return poly;
080        }
081    
082        public static Shape generateShapeFromText(Font font, char ch) {
083            return generateShapeFromText(font, String.valueOf(ch));
084        }
085        
086        public static Shape generateShapeFromText(Font font, String string) {
087            BufferedImage img = new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB);
088            Graphics2D g2 = img.createGraphics();
089            
090            try {
091                GlyphVector vect = font.createGlyphVector(g2.getFontRenderContext(), string);
092                Shape shape = vect.getOutline(0f,(float)-vect.getVisualBounds().getY());
093                
094                return shape;
095            } finally {
096                g2.dispose();
097            }
098        }
099    }