Coverage details for edu.uci.ics.jung.visualization.VertexShapeFactory

LineHitsSource
1 /*
2  * Copyright (c) 2003, the JUNG Project and the Regents of the University of
3  * California All rights reserved.
4  *
5  * This software is open-source under the BSD license; see either "license.txt"
6  * or http://jung.sourceforge.net/license.txt for a description.
7  *
8  * Created on Jul 20, 2004
9  */
10 package edu.uci.ics.jung.visualization;
11  
12 import java.awt.Shape;
13 import java.awt.geom.AffineTransform;
14 import java.awt.geom.Ellipse2D;
15 import java.awt.geom.GeneralPath;
16 import java.awt.geom.Point2D;
17 import java.awt.geom.Rectangle2D;
18 import java.awt.geom.RoundRectangle2D;
19  
20 import edu.uci.ics.jung.graph.Vertex;
21 import edu.uci.ics.jung.graph.decorators.ConstantVertexAspectRatioFunction;
22 import edu.uci.ics.jung.graph.decorators.ConstantVertexSizeFunction;
23 import edu.uci.ics.jung.graph.decorators.VertexAspectRatioFunction;
24 import edu.uci.ics.jung.graph.decorators.VertexSizeFunction;
25  
26 /**
27  * A utility class for generating <code>Shape</code>s for drawing vertices.
28  * The available shapes include rectangles, rounded rectangles, ellipses,
29  * regular polygons, and regular stars. The dimensions of the requested
30  * shapes are defined by the specified <code>VertexSizeFunction</code>
31  * and <code>VertexAspectRatioFunction</code> implementations: the width
32  * of the bounding box of the shape is given by the vertex size, and the
33  * height is given by the size multiplied by the vertex's aspect ratio.
34  *
35  * @author Joshua O'Madadhain
36  */
37 public class VertexShapeFactory
38 {
39     protected VertexSizeFunction vsf;
40     protected VertexAspectRatioFunction varf;
41     
42     /**
43      * Creates a <code>VertexShapeFactory</code> with the specified
44      * vertex size and aspect ratio functions.
45      */
46     public VertexShapeFactory(VertexSizeFunction vsf, VertexAspectRatioFunction varf)
470    {
480        this.vsf = vsf;
490        this.varf = varf;
500    }
51     
52     /**
53      * Creates a <code>VertexShapeFactory</code> with a constant size of
54      * 10 and a constant aspect ratio of 1.
55      */
56     public VertexShapeFactory()
57     {
580        this(new ConstantVertexSizeFunction(10),
59             new ConstantVertexAspectRatioFunction(1.0f));
600    }
61     
620    private static final Rectangle2D theRectangle = new Rectangle2D.Float();
63     /**
64      * Returns a <code>Rectangle2D</code> whose width and
65      * height are defined by this instance's size and
66      * aspect ratio functions for this vertex.
67      */
68     public Rectangle2D getRectangle(Vertex v)
69     {
700        float width = vsf.getSize(v);
710        float height = width * varf.getAspectRatio(v);
720        float h_offset = -(width / 2);
730        float v_offset = -(height / 2);
740        theRectangle.setFrame(h_offset, v_offset, width, height);
750        return theRectangle;
76     }
77  
780    private static final Ellipse2D theEllipse = new Ellipse2D.Float();
79     /**
80      * Returns a <code>Ellipse2D</code> whose width and
81      * height are defined by this instance's size and
82      * aspect ratio functions for this vertex.
83      */
84     public Ellipse2D getEllipse(Vertex v)
85     {
860        theEllipse.setFrame(getRectangle(v));
870        return theEllipse;
88     }
89     
900    private static final RoundRectangle2D theRoundRectangle =
91         new RoundRectangle2D.Float();
92     /**
93      * Returns a <code>RoundRectangle2D</code> whose width and
94      * height are defined by this instance's size and
95      * aspect ratio functions for this vertex. The arc size is
96      * set to be half the minimum of the height and width of the frame.
97      */
98     public RoundRectangle2D getRoundRectangle(Vertex v)
99     {
1000        Rectangle2D frame = getRectangle(v);
1010        float arc_size = (float)Math.min(frame.getHeight(), frame.getWidth()) / 2;
1020        theRoundRectangle.setRoundRect(frame.getX(), frame.getY(),
103                 frame.getWidth(), frame.getHeight(), arc_size, arc_size);
1040        return theRoundRectangle;
105     }
106     
1070    private static final GeneralPath thePolygon = new GeneralPath();
108     /**
109      * Returns a regular <code>num_sides</code>-sided
110      * <code>Polygon</code> whose bounding
111      * box's width and height are defined by this instance's size and
112      * aspect ratio functions for this vertex.
113      * @param num_sides the number of sides of the polygon; must be >= 3.
114      */
115     public Shape getRegularPolygon(Vertex v, int num_sides)
116     {
1170        if (num_sides < 3)
1180            throw new IllegalArgumentException("Number of sides must be >= 3");
1190        Rectangle2D frame = getRectangle(v);
1200        float width = (float)frame.getWidth();
1210        float height = (float)frame.getHeight();
122         
123         // generate coordinates
1240        double angle = 0;
1250        thePolygon.reset();
1260        thePolygon.moveTo(0,0);
1270        thePolygon.lineTo(width, 0);
1280        double theta = (2 * Math.PI) / num_sides;
1290        for (int i = 2; i < num_sides; i++)
130         {
1310            angle -= theta;
1320            float delta_x = (float) (width * Math.cos(angle));
1330            float delta_y = (float) (width * Math.sin(angle));
1340            Point2D prev = thePolygon.getCurrentPoint();
1350            thePolygon.lineTo((float)prev.getX() + delta_x, (float)prev.getY() + delta_y);
136         }
1370        thePolygon.closePath();
138         
139         // scale polygon to be right size, translate to center at (0,0)
1400        Rectangle2D r = thePolygon.getBounds2D();
1410        double scale_x = width / r.getWidth();
1420        double scale_y = height / r.getHeight();
1430        float translationX = (float) (r.getMinX() + r.getWidth()/2);
1440        float translationY = (float) (r.getMinY() + r.getHeight()/2);
145  
1460        AffineTransform at = AffineTransform.getScaleInstance(scale_x, scale_y);
1470        at.translate(-translationX, -translationY);
1480        Shape shape = at.createTransformedShape(thePolygon);
1490        return shape;
150     }
151     
152     /**
153      * Returns a regular <code>Polygon</code> of <code>num_points</code>
154      * points whose bounding
155      * box's width and height are defined by this instance's size and
156      * aspect ratio functions for this vertex.
157      * @param num_points the number of points of the polygon; must be >= 5.
158      */
159     public Shape getRegularStar(Vertex v, int num_points)
160     {
1610        if (num_points < 5)
1620            throw new IllegalArgumentException("Number of sides must be >= 5");
1630        Rectangle2D frame = getRectangle(v);
1640        float width = (float) frame.getWidth();
1650        float height = (float) frame.getHeight();
166         
167         // generate coordinates
1680        double theta = (2 * Math.PI) / num_points;
1690        double angle = -theta/2;
1700        thePolygon.reset();
1710        thePolygon.moveTo(0,0);
1720        float delta_x = width * (float)Math.cos(angle);
1730        float delta_y = width * (float)Math.sin(angle);
1740        Point2D prev = thePolygon.getCurrentPoint();
1750        thePolygon.lineTo((float)prev.getX() + delta_x, (float)prev.getY() + delta_y);
1760        for (int i = 1; i < num_points; i++)
177         {
1780            angle += theta;
1790            delta_x = width * (float)Math.cos(angle);
1800            delta_y = width * (float)Math.sin(angle);
1810            prev = thePolygon.getCurrentPoint();
1820            thePolygon.lineTo((float)prev.getX() + delta_x, (float)prev.getY() + delta_y);
1830            angle -= theta*2;
1840            delta_x = width * (float)Math.cos(angle);
1850            delta_y = width * (float)Math.sin(angle);
1860            prev = thePolygon.getCurrentPoint();
1870            thePolygon.lineTo((float)prev.getX() + delta_x, (float)prev.getY() + delta_y);
188         }
1890        thePolygon.closePath();
190         
191         // scale polygon to be right size, translate to center at (0,0)
1920        Rectangle2D r = thePolygon.getBounds2D();
1930        double scale_x = width / r.getWidth();
1940        double scale_y = height / r.getHeight();
195  
1960        float translationX = (float) (r.getMinX() + r.getWidth()/2);
1970        float translationY = (float) (r.getMinY() + r.getHeight()/2);
198         
1990        AffineTransform at = AffineTransform.getScaleInstance(scale_x, scale_y);
2000        at.translate(-translationX, -translationY);
201  
2020        Shape shape = at.createTransformedShape(thePolygon);
2030        return shape;
204     }
205 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.