View Javadoc

1   /*
2    * Copyright (c) 2005, 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    * Created on Jul 11, 2005
9    */
10  
11  package edu.uci.ics.jung.visualization.transform.shape;
12  
13  import java.awt.Component;
14  import java.awt.Graphics2D;
15  import java.awt.Shape;
16  import java.awt.geom.AffineTransform;
17  import java.awt.geom.Area;
18  import java.awt.geom.Rectangle2D;
19  
20  import javax.swing.Icon;
21  
22  import edu.uci.ics.jung.visualization.transform.BidirectionalTransformer;
23  
24  
25  /**
26   * Subclassed to apply a magnification transform to an icon.
27   *  
28   * @author Tom Nelson 
29   *
30   *
31   */
32  public class MagnifyIconGraphics extends TransformingFlatnessGraphics {
33      
34      public MagnifyIconGraphics(BidirectionalTransformer transformer) {
35          this(transformer, null);
36      }
37      
38      public MagnifyIconGraphics(BidirectionalTransformer transformer, Graphics2D delegate) {
39          super(transformer, delegate);
40      }
41      
42      public void draw(Icon icon, Component c, Shape clip, int x, int y) {
43      	
44      	if(transformer instanceof MagnifyShapeTransformer) {
45      		MagnifyShapeTransformer mst = (MagnifyShapeTransformer)transformer;
46      		int w = icon.getIconWidth();
47      		int h = icon.getIconHeight();
48      		Rectangle2D r = new Rectangle2D.Double(x-w/2,y-h/2,w,h);
49      		Shape lens = mst.getLensShape();
50      		if(lens.intersects(r)) {
51      			// magnify the whole icon
52      			Rectangle2D s = mst.magnify(r).getBounds2D();
53      			if(lens.intersects(s)) {
54      				clip = mst.transform(clip);
55      				double sx = s.getWidth()/r.getWidth();
56      				double sy = s.getHeight()/r.getHeight();
57  
58      				AffineTransform old = delegate.getTransform();
59      				AffineTransform xform = new AffineTransform(old);
60      				xform.translate(s.getMinX(), s.getMinY());
61      				xform.scale(sx, sy);
62      				xform.translate(-s.getMinX(), -s.getMinY());
63      				Shape oldClip = delegate.getClip();
64      				delegate.clip(clip);
65      				delegate.setTransform(xform);
66      				icon.paintIcon(c, delegate, (int)s.getMinX(), (int)s.getMinY());
67      				delegate.setTransform(old);
68      				delegate.setClip(oldClip);
69      			} else {
70      				// clip out the lens so the small icon doesn't get drawn
71      				// inside of it
72      				Shape oldClip = delegate.getClip();
73      				Area viewBounds = new Area(oldClip);
74      				viewBounds.subtract(new Area(lens));
75      				delegate.setClip(viewBounds);
76      				icon.paintIcon(c, delegate, (int)r.getMinX(),(int)r.getMinY());
77      				delegate.setClip(oldClip);
78      			}
79  
80      		} else {
81      			icon.paintIcon(c, delegate, (int)r.getMinX(),(int)r.getMinY());
82      		}
83      	}
84      }
85  }