1
2
3
4
5
6
7
8
9
10 package edu.uci.ics.jung.visualization.renderers;
11
12 import java.awt.Color;
13 import java.awt.Dimension;
14 import java.awt.GradientPaint;
15 import java.awt.Paint;
16 import java.awt.Rectangle;
17 import java.awt.Shape;
18 import java.awt.Stroke;
19 import java.awt.geom.AffineTransform;
20 import java.awt.geom.Point2D;
21
22 import javax.swing.JComponent;
23
24 import edu.uci.ics.jung.algorithms.layout.Layout;
25 import edu.uci.ics.jung.graph.Graph;
26 import edu.uci.ics.jung.graph.util.Context;
27 import edu.uci.ics.jung.visualization.Layer;
28 import edu.uci.ics.jung.visualization.RenderContext;
29 import edu.uci.ics.jung.visualization.picking.PickedState;
30 import edu.uci.ics.jung.visualization.transform.shape.GraphicsDecorator;
31
32
33
34
35
36
37
38
39 public class GradientVertexRenderer<V,E> implements Renderer.Vertex<V,E> {
40
41 Color colorOne;
42 Color colorTwo;
43 Color pickedColorOne;
44 Color pickedColorTwo;
45 PickedState<V> pickedState;
46 boolean cyclic;
47
48
49 public GradientVertexRenderer(Color colorOne, Color colorTwo, boolean cyclic) {
50 this.colorOne = colorOne;
51 this.colorTwo = colorTwo;
52 this.cyclic = cyclic;
53 }
54
55
56 public GradientVertexRenderer(Color colorOne, Color colorTwo, Color pickedColorOne, Color pickedColorTwo, PickedState<V> pickedState, boolean cyclic) {
57 this.colorOne = colorOne;
58 this.colorTwo = colorTwo;
59 this.pickedColorOne = pickedColorOne;
60 this.pickedColorTwo = pickedColorTwo;
61 this.pickedState = pickedState;
62 this.cyclic = cyclic;
63 }
64
65
66 public void paintVertex(RenderContext<V,E> rc, Layout<V,E> layout, V v) {
67 Graph<V,E> graph = layout.getGraph();
68 if (rc.getVertexIncludePredicate().apply(Context.<Graph<V,E>,V>getInstance(graph,v))) {
69 boolean vertexHit = true;
70
71 Shape shape = rc.getVertexShapeTransformer().apply(v);
72
73 Point2D p = layout.apply(v);
74 p = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, p);
75
76 float x = (float)p.getX();
77 float y = (float)p.getY();
78
79
80
81 AffineTransform xform = AffineTransform.getTranslateInstance(x,y);
82
83 shape = xform.createTransformedShape(shape);
84
85 vertexHit = vertexHit(rc, shape);
86
87
88 if (vertexHit) {
89 paintShapeForVertex(rc, v, shape);
90 }
91 }
92 }
93
94 protected boolean vertexHit(RenderContext<V,E> rc, Shape s) {
95 JComponent vv = rc.getScreenDevice();
96 Rectangle deviceRectangle = null;
97 if(vv != null) {
98 Dimension d = vv.getSize();
99 deviceRectangle = new Rectangle(
100 0,0,
101 d.width,d.height);
102 }
103 return rc.getMultiLayerTransformer().getTransformer(Layer.VIEW).transform(s).intersects(deviceRectangle);
104 }
105
106 protected void paintShapeForVertex(RenderContext<V,E> rc, V v, Shape shape) {
107 GraphicsDecorator g = rc.getGraphicsContext();
108 Paint oldPaint = g.getPaint();
109 Rectangle r = shape.getBounds();
110 float y2 = (float)r.getMaxY();
111 if(cyclic) {
112 y2 = (float)(r.getMinY()+r.getHeight()/2);
113 }
114
115 Paint fillPaint = null;
116 if(pickedState != null && pickedState.isPicked(v)) {
117 fillPaint = new GradientPaint((float)r.getMinX(), (float)r.getMinY(), pickedColorOne,
118 (float)r.getMinX(), y2, pickedColorTwo, cyclic);
119 } else {
120 fillPaint = new GradientPaint((float)r.getMinX(), (float)r.getMinY(), colorOne,
121 (float)r.getMinX(), y2, colorTwo, cyclic);
122 }
123 if(fillPaint != null) {
124 g.setPaint(fillPaint);
125 g.fill(shape);
126 g.setPaint(oldPaint);
127 }
128 Paint drawPaint = rc.getVertexDrawPaintTransformer().apply(v);
129 if(drawPaint != null) {
130 g.setPaint(drawPaint);
131 }
132 Stroke oldStroke = g.getStroke();
133 Stroke stroke = rc.getVertexStrokeTransformer().apply(v);
134 if(stroke != null) {
135 g.setStroke(stroke);
136 }
137 g.draw(shape);
138 g.setPaint(oldPaint);
139 g.setStroke(oldStroke);
140 }
141 }