Line | Hits | Source |
---|---|---|
1 | /* | |
2 | * Created on Apr 8, 2005 | |
3 | * | |
4 | * Copyright (c) 2004, the JUNG Project and the Regents of the University | |
5 | * of California | |
6 | * All rights reserved. | |
7 | * | |
8 | * This software is open-source under the BSD license; see either | |
9 | * "license.txt" or | |
10 | * http://jung.sourceforge.net/license.txt for a description. | |
11 | */ | |
12 | package edu.uci.ics.jung.graph.decorators; | |
13 | ||
14 | import java.awt.Color; | |
15 | import java.awt.GradientPaint; | |
16 | import java.awt.Paint; | |
17 | import java.awt.geom.Point2D; | |
18 | ||
19 | import edu.uci.ics.jung.graph.Edge; | |
20 | import edu.uci.ics.jung.graph.UndirectedEdge; | |
21 | import edu.uci.ics.jung.graph.Vertex; | |
22 | import edu.uci.ics.jung.utils.Pair; | |
23 | import edu.uci.ics.jung.visualization.HasGraphLayout; | |
24 | import edu.uci.ics.jung.visualization.Layout; | |
25 | import edu.uci.ics.jung.visualization.transform.LayoutTransformer; | |
26 | ||
27 | /** | |
28 | * Creates <code>GradientPaint</code> instances which can be used | |
29 | * to paint an <code>Edge</code>. For <code>DirectedEdge</code>s, | |
30 | * the color will blend from <code>c1</code> (source) to | |
31 | * <code>c2</code> (destination); for <code>UndirectedEdge</code>s, | |
32 | * the color will be <code>c1</code> at each end and <code>c2</code> | |
33 | * in the middle. | |
34 | * | |
35 | * @author Joshua O'Madadhain | |
36 | */ | |
37 | public class GradientEdgePaintFunction extends AbstractEdgePaintFunction | |
38 | { | |
39 | protected Color c1; | |
40 | protected Color c2; | |
41 | HasGraphLayout vv; | |
42 | LayoutTransformer transformer; | |
43 | ||
44 | public GradientEdgePaintFunction(Color c1, Color c2, | |
45 | HasGraphLayout vv, LayoutTransformer transformer) | |
46 | 0 | { |
47 | 0 | this.c1 = c1; |
48 | 0 | this.c2 = c2; |
49 | 0 | this.vv = vv; |
50 | 0 | this.transformer = transformer; |
51 | 0 | } |
52 | ||
53 | public Paint getDrawPaint(Edge e) | |
54 | { | |
55 | 0 | Layout layout = vv.getGraphLayout(); |
56 | 0 | Pair p = e.getEndpoints(); |
57 | 0 | Vertex b = (Vertex)p.getFirst(); |
58 | 0 | Vertex f = (Vertex)p.getSecond(); |
59 | 0 | Point2D pb = transformer.layoutTransform(layout.getLocation(b)); |
60 | 0 | Point2D pf = transformer.layoutTransform(layout.getLocation(f)); |
61 | 0 | float xB = (float) pb.getX(); |
62 | 0 | float yB = (float) pb.getY(); |
63 | 0 | float xF = (float) pf.getX(); |
64 | 0 | float yF = (float) pf.getY(); |
65 | 0 | if (e instanceof UndirectedEdge ) |
66 | { | |
67 | 0 | xF = (xF + xB) / 2; |
68 | 0 | yF = (yF + yB) / 2; |
69 | } | |
70 | 0 | if(xB == xF && yB == yF) |
71 | { | |
72 | // hack so loop edges don't 'vanish' | |
73 | 0 | xB -= 10; |
74 | 0 | yB -= 10; |
75 | 0 | xF += 10; |
76 | 0 | yF += 10; |
77 | } | |
78 | ||
79 | 0 | return new GradientPaint(xB, yB, getColor1(e), xF, yF, getColor2(e), true); |
80 | } | |
81 | ||
82 | /** | |
83 | * Returns <code>c1</code>. Subclasses may override | |
84 | * this method to enable more complex behavior (e.g., for | |
85 | * picked edges). | |
86 | */ | |
87 | protected Color getColor1(Edge e) | |
88 | { | |
89 | 0 | return c1; |
90 | } | |
91 | ||
92 | /** | |
93 | * Returns <code>c2</code>. Subclasses may override | |
94 | * this method to enable more complex behavior (e.g., for | |
95 | * picked edges). | |
96 | */ | |
97 | protected Color getColor2(Edge e) | |
98 | { | |
99 | 0 | return c2; |
100 | } | |
101 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |