KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > action > distortion > Distortion


1 package prefuse.action.distortion;
2
3 import java.awt.geom.Point2D JavaDoc;
4 import java.awt.geom.Rectangle2D JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 import prefuse.action.layout.Layout;
8 import prefuse.visual.VisualItem;
9
10
11 /**
12  * Abstract base class providing a structure for space-distortion techniques.
13  *
14  * @version 1.0
15  * @author <a HREF="http://jheer.org">jeffrey heer</a>
16  */

17 public abstract class Distortion extends Layout {
18
19     private Point2D JavaDoc m_tmp = new Point2D.Double JavaDoc();
20     protected boolean m_distortSize = true;
21     protected boolean m_distortX = true;
22     protected boolean m_distortY = true;
23     
24     // ------------------------------------------------------------------------
25

26     /**
27      * Create a new Distortion instance.
28      */

29     public Distortion() {
30         super();
31     }
32
33     /**
34      * Create a new Distortion instance that processes the given data group.
35      * @param group the data group processed by this Distortion instance
36      */

37     public Distortion(String JavaDoc group) {
38         super(group);
39     }
40     
41     // ------------------------------------------------------------------------
42

43     /**
44      * Controls whether item sizes are distorted along with the item locations.
45      * @param s true to distort size, false to distort positions only
46      */

47     public void setSizeDistorted(boolean s) {
48         m_distortSize = s;
49     }
50     
51     /**
52      * Indicates whether the item sizes are distorted along with the item
53      * locations.
54      * @return true if item sizes are distorted by this action, false otherwise
55      */

56     public boolean isSizeDistorted() {
57         return m_distortSize;
58     }
59     
60     // ------------------------------------------------------------------------
61

62     /**
63      * @see prefuse.action.Action#run(double)
64      */

65     public void run(double frac) {
66         Rectangle2D JavaDoc bounds = getLayoutBounds();
67         Point2D JavaDoc anchor = correct(m_anchor, bounds);
68         
69         final Iterator JavaDoc iter = getVisualization().visibleItems(m_group);
70         
71         while ( iter.hasNext() ) {
72             VisualItem item = (VisualItem)iter.next();
73             if ( item.isFixed() ) continue;
74             
75             // reset distorted values
76
// TODO - make this play nice with animation?
77
item.setX(item.getEndX());
78             item.setY(item.getEndY());
79             item.setSize(item.getEndSize());
80             
81             // compute distortion if we have a distortion focus
82
if ( anchor != null ) {
83                 Rectangle2D JavaDoc bbox = item.getBounds();
84                 double x = item.getX();
85                 double y = item.getY();
86                 
87                 // position distortion
88
if ( m_distortX )
89                     item.setX(x=distortX(x, anchor, bounds));
90                 if ( m_distortY )
91                     item.setY(y=distortY(y, anchor, bounds));
92                 
93                 // size distortion
94
if ( m_distortSize ) {
95                     double sz = distortSize(bbox, x, y, anchor, bounds);
96                     item.setSize(sz*item.getSize());
97                 }
98             }
99         }
100     }
101     
102     /**
103      * Corrects the anchor position, such that if the anchor is outside the
104      * layout bounds, the anchor is adjusted to be the nearest point on the
105      * edge of the bounds.
106      * @param anchor the un-corrected anchor point
107      * @param bounds the layout bounds
108      * @return the corrected anchor point
109      */

110     protected Point2D JavaDoc correct(Point2D JavaDoc anchor, Rectangle2D JavaDoc bounds) {
111         if ( anchor == null ) return anchor;
112         double x = anchor.getX(), y = anchor.getY();
113         double x1 = bounds.getMinX(), y1 = bounds.getMinY();
114         double x2 = bounds.getMaxX(), y2 = bounds.getMaxY();
115         x = (x < x1 ? x1 : (x > x2 ? x2 : x));
116         y = (y < y1 ? y1 : (y > y2 ? y2 : y));
117         
118         m_tmp.setLocation(x,y);
119         return m_tmp;
120     }
121     
122     /**
123      * Distorts an item's x-coordinate.
124      * @param x the undistorted x coordinate
125      * @param anchor the anchor or focus point of the display
126      * @param bounds the layout bounds
127      * @return the distorted x-coordinate
128      */

129     protected abstract double distortX(double x, Point2D JavaDoc anchor, Rectangle2D JavaDoc bounds);
130
131     /**
132      * Distorts an item's y-coordinate.
133      * @param y the undistorted y coordinate
134      * @param anchor the anchor or focus point of the display
135      * @param bounds the layout bounds
136      * @return the distorted y-coordinate
137      */

138     protected abstract double distortY(double y, Point2D JavaDoc anchor, Rectangle2D JavaDoc bounds);
139     
140     /**
141      * Returns the scaling factor by which to transform the size of an item.
142      * @param bbox the bounding box of the undistorted item
143      * @param x the x-coordinate of the distorted item
144      * @param y the y-coordinate of the distorted item
145      * @param anchor the anchor or focus point of the display
146      * @param bounds the layout bounds
147      * @return the scaling factor by which to change the size
148      */

149     protected abstract double distortSize(Rectangle2D JavaDoc bbox, double x, double y,
150             Point2D JavaDoc anchor, Rectangle2D JavaDoc bounds);
151
152 } // end of abstract class Distortion
153
Popular Tags