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.Graphics;
14  import java.awt.Graphics2D;
15  import java.awt.Image;
16  import java.awt.Rectangle;
17  import java.awt.Shape;
18  import java.awt.geom.AffineTransform;
19  import java.awt.geom.Rectangle2D;
20  import java.awt.image.ImageObserver;
21  
22  import edu.uci.ics.jung.visualization.transform.BidirectionalTransformer;
23  
24  
25  /**
26   * subclassed to pass certain operations thru the transformer
27   * before the base class method is applied
28   * This is useful when you want to apply non-affine transformations
29   * to the Graphics2D used to draw elements of the graph.
30   * 
31   * @author Tom Nelson 
32   *
33   *
34   */
35  public class TransformingGraphics extends GraphicsDecorator {
36      
37      /**
38       * the transformer to apply
39       */
40      protected BidirectionalTransformer transformer;
41      
42      public TransformingGraphics(BidirectionalTransformer transformer) {
43          this(transformer, null);
44      }
45      
46      public TransformingGraphics(BidirectionalTransformer transformer, Graphics2D delegate) {
47          super(delegate);
48          this.transformer = transformer;
49      }
50      
51      /**
52       * @return Returns the transformer.
53       */
54      public BidirectionalTransformer getTransformer() {
55          return transformer;
56      }
57      
58      /**
59       * @param transformer The transformer to set.
60       */
61      public void setTransformer(BidirectionalTransformer transformer) {
62          this.transformer = transformer;
63      }
64      
65      /**
66       * transform the shape before letting the delegate draw it
67       */
68      public void draw(Shape s) {
69          Shape shape = ((ShapeTransformer)transformer).transform(s);
70          delegate.draw(shape);
71      }
72      
73      public void draw(Shape s, float flatness) {
74          Shape shape = null;
75          if(transformer instanceof ShapeFlatnessTransformer) {
76              shape = ((ShapeFlatnessTransformer)transformer).transform(s, flatness);
77          } else {
78              shape = ((ShapeTransformer)transformer).transform(s);
79          }
80          delegate.draw(shape);
81          
82      }
83      
84      /**
85       * transform the shape before letting the delegate fill it
86       */
87      public void fill(Shape s) {
88          Shape shape = ((ShapeTransformer)transformer).transform(s);
89          delegate.fill(shape);
90      }
91      
92      public void fill(Shape s, float flatness) {
93          Shape shape = null;
94          if(transformer instanceof ShapeFlatnessTransformer) {
95              shape = ((ShapeFlatnessTransformer)transformer).transform(s, flatness);
96          } else {
97              shape = ((ShapeTransformer)transformer).transform(s);
98          }
99          delegate.fill(shape);
100     }
101     
102     public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
103     	Image image = null;
104         if(transformer instanceof ShapeFlatnessTransformer) {
105         	Rectangle2D r = new Rectangle2D.Double(x,y,img.getWidth(observer),img.getHeight(observer));
106         	Rectangle2D s = ((ShapeTransformer)transformer).transform(r).getBounds2D();
107         	image = img.getScaledInstance((int)s.getWidth(), (int)s.getHeight(), Image.SCALE_SMOOTH);
108         	x = (int) s.getMinX();
109         	y = (int) s.getMinY();
110         } else {
111             image = img;
112         }
113          return delegate.drawImage(image, x, y, observer);
114     }
115 
116     public boolean drawImage(Image img, AffineTransform at, ImageObserver observer) {
117     	Image image = null;
118     	int x = (int)at.getTranslateX();
119     	int y = (int)at.getTranslateY();
120         if(transformer instanceof ShapeFlatnessTransformer) {
121         	Rectangle2D r = new Rectangle2D.Double(x,y,img.getWidth(observer),img.getHeight(observer));
122         	Rectangle2D s = ((ShapeTransformer)transformer).transform(r).getBounds2D();
123         	image = img.getScaledInstance((int)s.getWidth(), (int)s.getHeight(), Image.SCALE_SMOOTH);
124         	x = (int) s.getMinX();
125         	y = (int) s.getMinY();
126         	at.setToTranslation(s.getMinX(), s.getMinY());
127         } else {
128             image = img;
129         }
130          return delegate.drawImage(image, at, observer);
131     }
132 
133     /**
134      * transform the shape before letting the delegate apply 'hit'
135      * with it
136      */
137     public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
138         Shape shape = ((ShapeTransformer)transformer).transform(s);
139         return delegate.hit(rect, shape, onStroke);
140     }
141     
142     public Graphics create() {
143         return delegate.create();
144     }
145     
146     public void dispose() {
147         delegate.dispose();
148     }
149     
150 }