Coverage details for edu.uci.ics.jung.visualization.transform.LensTransformer

LineHitsSource
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.Dimension;
13 import java.awt.event.ComponentAdapter;
14 import java.awt.event.ComponentEvent;
15 import java.awt.geom.Ellipse2D;
16 import java.awt.geom.Point2D;
17  
18 /**
19  * LensTransformer wraps a MutableAffineTransformer and modifies
20  * the transform and inverseTransform methods so that they create a
21  * projection of the graph points within an elliptical lens.
22  *
23  * LensTransformer uses an
24  * affine transform to cause translation, scaling, rotation, and shearing
25  * while applying a possibly non-affine filter in its transform and
26  * inverseTransform methods.
27  *
28  * @author Tom Nelson - RABA Technologies
29  *
30  *
31  */
320public abstract class LensTransformer extends MutableTransformerDecorator implements MutableTransformer {
33  
34     /**
35      * the area affected by the transform
36      */
370    protected Ellipse2D ellipse = new Ellipse2D.Float();
38     
390    protected float magnification = 0.7f;
40     
41     /**
42      * create an instance, setting values from the passed component
43      * and registering to listen for size changes on the component
44      * @param component
45      */
46     public LensTransformer(Component component) {
470        this(component, new MutableAffineTransformer());
480    }
49     /**
50      * create an instance with a possibly shared transform
51      * @param component
52      * @param delegate
53      */
54     public LensTransformer(Component component, MutableTransformer delegate) {
550            super(delegate);
560        setComponent(component);
570        component.addComponentListener(new ComponentListenerImpl());
580   }
59     
60     /**
61      * set values from the passed component.
62      * declared private so it can't be overridden
63      * @param component
64      */
65     private void setComponent(Component component) {
660        Dimension d = component.getSize();
670        if(d.width <= 0 || d.height <= 0) {
680            d = component.getPreferredSize();
69         }
700        float ewidth = d.width/1.5f;
710        float eheight = d.height/1.5f;
720        ellipse.setFrame(d.width/2-ewidth/2, d.height/2-eheight/2, ewidth, eheight);
730    }
74     
75     /**
76      * @return Returns the magnification.
77      */
78     public float getMagnification() {
790        return magnification;
80     }
81     /**
82      * @param magnification The magnification to set.
83      */
84     public void setMagnification(float magnification) {
850        this.magnification = magnification;
860    }
87     /**
88      * @return Returns the viewCenter.
89      */
90     public Point2D getViewCenter() {
910        return new Point2D.Double(ellipse.getCenterX(), ellipse.getCenterY());
92     }
93     /**
94      * @param viewCenter The viewCenter to set.
95      */
96     public void setViewCenter(Point2D viewCenter) {
970        double width = ellipse.getWidth();
980        double height = ellipse.getHeight();
990        ellipse.setFrame(viewCenter.getX()-width/2,
100                 viewCenter.getY()-height/2,
101                 width, height);
1020    }
103  
104     /**
105      * @return Returns the viewRadius.
106      */
107     public double getViewRadius() {
1080        return ellipse.getHeight()/2;
109     }
110     /**
111      * @param viewRadius The viewRadius to set.
112      */
113     public void setViewRadius(double viewRadius) {
1140        double x = ellipse.getCenterX();
1150        double y = ellipse.getCenterY();
1160        double viewRatio = getRatio();
1170        ellipse.setFrame(x-viewRadius/viewRatio,
118                 y-viewRadius,
119                 2*viewRadius/viewRatio,
120                 2*viewRadius);
1210    }
122     
123     /**
124      * @return Returns the ratio.
125      */
126     public double getRatio() {
1270        return ellipse.getHeight()/ellipse.getWidth();
128     }
129     
130     public void setEllipse(Ellipse2D ellipse) {
1310        this.ellipse = ellipse;
1320    }
133     public Ellipse2D getEllipse() {
1340        return ellipse;
135     }
136     public void setToIdentity() {
1370        this.delegate.setToIdentity();
1380    }
139  
140     /**
141      * react to size changes on a component
142      */
143     protected class ComponentListenerImpl extends ComponentAdapter {
144         public void componentResized(ComponentEvent e) {
145             setComponent(e.getComponent());
146          }
147     }
148     
149     /**
150      * a convenience class to represent a point in
151      * polar coordinates
152      */
153     protected static class PolarPoint extends Point2D.Double {
154         public PolarPoint(double theta, double radius) {
155             super(theta, radius);
156         }
157         public double getTheta() { return getX(); }
158         public double getRadius() { return getY(); }
159         public void setTheta(double theta) { setLocation(theta, getRadius()); }
160         public void setRadius(double radius) { setLocation(getTheta(), radius); }
161     }
162     
163     /**
164      * Returns the result of converting <code>polar</code> to Cartesian coordinates.
165      */
166     protected Point2D polarToCartesian(PolarPoint polar) {
1670        return polarToCartesian(polar.getTheta(), polar.getRadius());
168     }
169     
170     /**
171      * Returns the result of converting <code>(theta, radius)</code> to Cartesian coordinates.
172      */
173      protected Point2D polarToCartesian(double theta, double radius) {
1740        return new Point2D.Double(radius*Math.cos(theta), radius*Math.sin(theta));
175     }
176     
177     /**
178      * Returns the result of converting <code>point</code> to polar coordinates.
179      */
180     protected PolarPoint cartesianToPolar(Point2D point) {
1810        return cartesianToPolar(point.getX(), point.getY());
182     }
183     
184     /**
185      * Returns the result of converting <code>(x, y)</code> to polar coordinates.
186      */
187     protected PolarPoint cartesianToPolar(double x, double y) {
1880        double theta = Math.atan2(y,x);
1890        double radius = Math.sqrt(x*x+y*y);
1900        return new PolarPoint(theta, radius);
191     }
192     
193     /**
194      * override base class transform to project the fisheye effect
195      */
196     public abstract Point2D transform(Point2D graphPoint);
197     
198     /**
199      * override base class to un-project the fisheye effect
200      */
201     public abstract Point2D inverseTransform(Point2D viewPoint);
202     
203     public double getDistanceFromCenter(Point2D p) {
2040        double dx = ellipse.getCenterX()-p.getX();
2050        double dy = ellipse.getCenterY()-p.getY();
2060        dx *= getRatio();
2070        return Math.sqrt(dx*dx + dy*dy);
208     }
209 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.