Line | Hits | Source |
---|---|---|
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; | |
37 | 0 | protected int m_arrowLength = 4; |
38 | 0 | protected int m_arrowWidth = 10; |
39 | protected Stroke m_arrowStroke; | |
40 | ||
41 | 0 | public Arrow(String type, int length, int width){ |
42 | 0 | m_arrowType = type; |
43 | 0 | if(length>0){ |
44 | 0 | m_arrowLength = length; |
45 | } | |
46 | ||
47 | 0 | if(width>0){ |
48 | 0 | m_arrowWidth = width; |
49 | } | |
50 | ||
51 | 0 | m_arrowStroke = new BasicStroke(2); |
52 | ||
53 | 0 | if(this.m_arrowType == SLEEK){ |
54 | 0 | arrowhead = getSleekArrow(); |
55 | }else{ | |
56 | 0 | arrowhead = getClassicArrow(); |
57 | } | |
58 | ||
59 | 0 | } |
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){ | |
96 | 0 | Stroke oldStroke = g2d.getStroke(); |
97 | 0 | g2d.setStroke(this.m_arrowStroke); |
98 | 0 | Point point1 = new Point(sourceX, sourceY); |
99 | 0 | Point point2 = new Point(destX, destY); |
100 | ||
101 | // get angle of line from 0 - 360 | |
102 | 0 | 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; | |
106 | 0 | AffineTransform at = new AffineTransform(); |
107 | 0 | at.translate(point2.getX() , point2.getY() ); |
108 | 0 | at.rotate(thetaRadians); |
109 | 0 | at.translate( - vertexDiam / 2.0f, 0 ); |
110 | 0 | Shape arrow = at.createTransformedShape(arrowhead); |
111 | 0 | g2d.fill(arrow); |
112 | 0 | g2d.setStroke(oldStroke); |
113 | 0 | } |
114 | ||
115 | protected GeneralPath getSleekArrow(){ | |
116 | 0 | GeneralPath arrow = new GeneralPath(); |
117 | // float distance = 0; | |
118 | // (float) point1.distance(point2)-vertexDiam/2.0f; | |
119 | // create arrowed line general path | |
120 | 0 | int width = (int) (m_arrowWidth/2.0f); |
121 | 0 | arrow.moveTo( 0, 0); |
122 | 0 | arrow.lineTo( (- m_arrowLength), width); |
123 | 0 | arrow.lineTo( (- m_arrowLength) , -width); |
124 | 0 | arrow.lineTo( 0, 0 ); |
125 | 0 | return arrow; |
126 | } | |
127 | ||
128 | protected GeneralPath getClassicArrow(){ | |
129 | 0 | GeneralPath arrow = new GeneralPath(); |
130 | // float distance = (float) point1.distance(point2)-vertexDiam/2.0f; | |
131 | 0 | float distance = 0; |
132 | // create arrowed line general path | |
133 | 0 | int width = (int) (m_arrowWidth/2.0f); |
134 | 0 | arrow.moveTo( distance , 0); |
135 | 0 | arrow.lineTo( (distance - m_arrowLength), width); |
136 | 0 | arrow.lineTo( (distance - m_arrowLength) , -width); |
137 | 0 | arrow.lineTo( distance , 0 ); |
138 | 0 | return arrow; |
139 | } | |
140 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |