Coverage details for edu.uci.ics.jung.visualization.contrib.Arrow

LineHitsSource
1 /*
2  * Copyright (c) 2003, the JUNG Project and the Regents of the University
3  * of California
4  * All rights reserved.
5  *
6  * This software is open-source under the BSD license; see either
7  * "license.txt" or
8  * http://jung.sourceforge.net/license.txt for a description.
9  */
10  
11 package edu.uci.ics.jung.visualization.contrib;
12  
13 import java.awt.BasicStroke;
14 import java.awt.Graphics2D;
15 import java.awt.Point;
16 import java.awt.Shape;
17 import java.awt.Stroke;
18 import java.awt.geom.AffineTransform;
19 import java.awt.geom.GeneralPath;
20  
21 /**
22  * This pluggable utility paints either a "classic" or a "sleek" filled arrow
23  * on a given edge. To use, create an instance of the Arrow object
24  * with your preferred thickness, and then call
25  * arrow.drawArrow( graphics, source_x1, source_y1, dest_x, dest_y2 ) for the edge.
26  *
27  * Note that the arrow simply uses the color currently set in the graphics context.
28  *
29  * @author Jon Froehlich
30  */
31 public class Arrow {
32  
33     public static final String CLASSIC = "Arrow.CLASSIC";
34     public static final String SLEEK = "Arrow.SLEEK";
35     
36     protected String m_arrowType;
370    protected int m_arrowLength = 4;
380    protected int m_arrowWidth = 10;
39     protected Stroke m_arrowStroke;
40     
410    public Arrow(String type, int length, int width){
420        m_arrowType = type;
430        if(length>0){
440            m_arrowLength = length;
45         }
46         
470        if(width>0){
480            m_arrowWidth = width;
49         }
50         
510        m_arrowStroke = new BasicStroke(2);
52         
530        if(this.m_arrowType == SLEEK){
540            arrowhead = getSleekArrow();
55         }else{
560            arrowhead = getClassicArrow();
57         }
58  
590    }
60  
61     GeneralPath arrowhead;
62  
63     
64 // public void drawArrow(Graphics2D g2d, double x1, double y1,
65 // double x2, double y2, Shape vertex, boolean directed)
66 // {
67 // double theta = Math.atan2((y1 - y2), (x1 - x2)) + Math.PI;
68 //
69 // // calculate offset from center of vertex bounding box;
70 // // create coordinates for source and dest centered at dest
71 // // (since vertex shape will be centered at dest)
72 // Coordinates source = new Coordinates(x1-x2, y1-y2);
73 // Coordinates dest = new Coordinates(0,0);
74 // Coordinates c = CoordinateUtil.getClosestIntersection(source, dest, vertex.getBounds2D());
75 // if (c == null) // can happen if source and dest are the same
76 // return;
77 // double bounding_box_offset = CoordinateUtil.distance(c, dest);
78 //
79 // // transform arrowhead into dest coordinate space
80 // AffineTransform at = new AffineTransform();
81 // at.translate(x2, y2);
82 // if (directed)
83 // theta += Math.atan2(SettableRenderer.CONTROL_OFFSET,
84 // CoordinateUtil.distance(source,dest)/2);
85 // at.rotate(theta);
86 // at.translate(-bounding_box_offset, 0);
87 //
88 // // draw the arrowhead
89 // Stroke previous = g2d.getStroke();
90 // g2d.setStroke(this.m_arrowStroke);
91 // g2d.fill(at.createTransformedShape(arrowhead));
92 // g2d.setStroke(previous);
93 // }
94     
95     public void drawArrow(Graphics2D g2d, int sourceX, int sourceY, int destX, int destY, int vertexDiam){
960        Stroke oldStroke = g2d.getStroke();
970        g2d.setStroke(this.m_arrowStroke);
980        Point point1 = new Point(sourceX, sourceY);
990        Point point2 = new Point(destX, destY);
100         
101         // get angle of line from 0 - 360
1020        double thetaRadians = Math.atan2(( point1.getY() - point2.getY()),(point1.getX() -
103                 point2.getX()))+Math.PI;
104         
105 // float distance = (float) point1.distance(point2)-vertexDiam/2.0f;
1060        AffineTransform at = new AffineTransform();
1070        at.translate(point2.getX() , point2.getY() );
1080        at.rotate(thetaRadians);
1090        at.translate( - vertexDiam / 2.0f, 0 );
1100        Shape arrow = at.createTransformedShape(arrowhead);
1110        g2d.fill(arrow);
1120        g2d.setStroke(oldStroke);
1130    }
114     
115     protected GeneralPath getSleekArrow(){
1160        GeneralPath arrow = new GeneralPath();
117 // float distance = 0;
118 // (float) point1.distance(point2)-vertexDiam/2.0f;
119         // create arrowed line general path
1200        int width = (int) (m_arrowWidth/2.0f);
1210        arrow.moveTo( 0, 0);
1220        arrow.lineTo( (- m_arrowLength), width);
1230        arrow.lineTo( (- m_arrowLength) , -width);
1240        arrow.lineTo( 0, 0 );
1250        return arrow;
126     }
127     
128     protected GeneralPath getClassicArrow(){
1290        GeneralPath arrow = new GeneralPath();
130 // float distance = (float) point1.distance(point2)-vertexDiam/2.0f;
1310        float distance = 0;
132         // create arrowed line general path
1330        int width = (int) (m_arrowWidth/2.0f);
1340        arrow.moveTo( distance , 0);
1350        arrow.lineTo( (distance - m_arrowLength), width);
1360        arrow.lineTo( (distance - m_arrowLength) , -width);
1370        arrow.lineTo( distance , 0 );
1380        return arrow;
139     }
140 }

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.