View Javadoc

1   /*
2    * Copyright (c) 2003, the JUNG Project and the Regents of the University of
3    * California All rights reserved.
4    * 
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or http://jung.sourceforge.net/license.txt for a description.
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   * MagnifyTransformer wraps a MutableAffineTransformer and modifies
18   * the transform and inverseTransform methods so that they create an
19   * enlarging projection of the graph points.
20   * 
21   * MagnifyTransformer uses an
22   * affine transform to cause translation, scaling, rotation, and shearing
23   * while applying a separate magnification filter in its transform and
24   * inverseTransform methods.
25   * 
26   * @author Tom Nelson 
27   *
28   *
29   */
30  public class MagnifyTransformer extends LensTransformer implements MutableTransformer {
31  
32      
33      /**
34       * create an instance, setting values from the passed component
35       * and registering to listen for size changes on the component
36       * @param component
37       */
38      public MagnifyTransformer(Component component) {
39          this(component, new MutableAffineTransformer());
40      }
41      /**
42       * create an instance with a possibly shared transform
43       * @param component
44       * @param delegate
45       */
46      public MagnifyTransformer(Component component, MutableTransformer delegate) {
47      		super(component, delegate);
48          this.magnification = 3.f;
49     }
50      
51      /**
52       * override base class transform to project the fisheye effect
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          // transform the point from the graph to the view
60          Point2D viewPoint = delegate.transform(graphPoint);
61          // calculate point from center
62          double dx = viewPoint.getX() - viewCenter.getX();
63          double dy = viewPoint.getY() - viewCenter.getY();
64          // factor out ellipse
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       * override base class to un-project the fisheye effect
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          // factor out ellipse
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      * magnifies the point, without considering the Lens
116      * @param graphPoint
117      * @return
118      */
119     public Point2D magnify(Point2D graphPoint) {
120         if(graphPoint == null) return null;
121         Point2D viewCenter = getViewCenter();
122         double ratio = getRatio();
123         // transform the point from the graph to the view
124         Point2D viewPoint = graphPoint;
125         // calculate point from center
126         double dx = viewPoint.getX() - viewCenter.getX();
127         double dy = viewPoint.getY() - viewCenter.getY();
128         // factor out ellipse
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 //        radius = Math.min(radius, viewRadius);
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 }