1
2
3
4
5
6
7
8
9 package edu.uci.ics.jung.visualization.transform;
10
11 import java.awt.Component;
12 import java.awt.geom.Point2D;
13
14 import edu.uci.ics.jung.algorithms.layout.PolarPoint;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class MagnifyTransformer extends LensTransformer implements MutableTransformer {
31
32
33
34
35
36
37
38 public MagnifyTransformer(Component component) {
39 this(component, new MutableAffineTransformer());
40 }
41
42
43
44
45
46 public MagnifyTransformer(Component component, MutableTransformer delegate) {
47 super(component, delegate);
48 this.magnification = 3.f;
49 }
50
51
52
53
54 public Point2D transform(Point2D graphPoint) {
55 if(graphPoint == null) return null;
56 Point2D viewCenter = getViewCenter();
57 double viewRadius = getViewRadius();
58 double ratio = getRatio();
59
60 Point2D viewPoint = delegate.transform(graphPoint);
61
62 double dx = viewPoint.getX() - viewCenter.getX();
63 double dy = viewPoint.getY() - viewCenter.getY();
64
65 dx *= ratio;
66 Point2D pointFromCenter = new Point2D.Double(dx, dy);
67
68 PolarPoint polar = PolarPoint.cartesianToPolar(pointFromCenter);
69 double theta = polar.getTheta();
70 double radius = polar.getRadius();
71 if(radius > viewRadius) return viewPoint;
72
73 double mag = magnification;
74 radius *= mag;
75
76 radius = Math.min(radius, viewRadius);
77 Point2D projectedPoint = PolarPoint.polarToCartesian(theta, radius);
78 projectedPoint.setLocation(projectedPoint.getX()/ratio, projectedPoint.getY());
79 Point2D translatedBack = new Point2D.Double(projectedPoint.getX()+viewCenter.getX(),
80 projectedPoint.getY()+viewCenter.getY());
81 return translatedBack;
82 }
83
84
85
86
87 public Point2D inverseTransform(Point2D viewPoint) {
88
89 Point2D viewCenter = getViewCenter();
90 double viewRadius = getViewRadius();
91 double ratio = getRatio();
92 double dx = viewPoint.getX() - viewCenter.getX();
93 double dy = viewPoint.getY() - viewCenter.getY();
94
95 dx *= ratio;
96
97 Point2D pointFromCenter = new Point2D.Double(dx, dy);
98
99 PolarPoint polar = PolarPoint.cartesianToPolar(pointFromCenter);
100
101 double radius = polar.getRadius();
102 if(radius > viewRadius) return delegate.inverseTransform(viewPoint);
103
104 double mag = magnification;
105 radius /= mag;
106 polar.setRadius(radius);
107 Point2D projectedPoint = PolarPoint.polarToCartesian(polar);
108 projectedPoint.setLocation(projectedPoint.getX()/ratio, projectedPoint.getY());
109 Point2D translatedBack = new Point2D.Double(projectedPoint.getX()+viewCenter.getX(),
110 projectedPoint.getY()+viewCenter.getY());
111 return delegate.inverseTransform(translatedBack);
112 }
113
114
115
116
117
118
119 public Point2D magnify(Point2D graphPoint) {
120 if(graphPoint == null) return null;
121 Point2D viewCenter = getViewCenter();
122 double ratio = getRatio();
123
124 Point2D viewPoint = graphPoint;
125
126 double dx = viewPoint.getX() - viewCenter.getX();
127 double dy = viewPoint.getY() - viewCenter.getY();
128
129 dx *= ratio;
130 Point2D pointFromCenter = new Point2D.Double(dx, dy);
131
132 PolarPoint polar = PolarPoint.cartesianToPolar(pointFromCenter);
133 double theta = polar.getTheta();
134 double radius = polar.getRadius();
135
136 double mag = magnification;
137 radius *= mag;
138
139
140 Point2D projectedPoint = PolarPoint.polarToCartesian(theta, radius);
141 projectedPoint.setLocation(projectedPoint.getX()/ratio, projectedPoint.getY());
142 Point2D translatedBack = new Point2D.Double(projectedPoint.getX()+viewCenter.getX(),
143 projectedPoint.getY()+viewCenter.getY());
144 return translatedBack;
145 }
146
147 }