View Javadoc

1   // Copyright 2004-2005, FreeHEP.
2   package hep.wired.edit;
3   
4   import java.awt.*;
5   import java.awt.geom.*;
6   import java.util.logging.*;
7   import javax.swing.*;
8   import javax.swing.undo.*;
9   
10  import hep.wired.services.RecordPlot;
11  import hep.wired.services.GraphicsPanel;
12  import hep.wired.feature.Transformable2D;
13  
14  /***
15   * Transforms a plot in 2D.
16   *
17   * @author Mark Donszelmann
18   * @version $Id: Transform2D.java 652 2005-03-01 01:12:05Z duns $
19   */
20  public class Transform2D extends AnimatedWiredEdit implements GraphicsPanelEdit {
21  
22      private static Logger logger = Logger.getLogger(Transform2D.class.getPackage().getName());
23      private double sx, shy, shx, sy, tx, ty;
24  
25      /***    
26       * Creates a transform edit with no changes. 
27       */    
28      public Transform2D() {
29          this(1, 0, 0, 1, 0, 0);
30      }
31  
32      /***    
33       * Creates a transform edit with given scaling, shearing and translation. 
34       */    
35      public Transform2D(double sx, double shy,
36                         double shx, double sy,
37                         double tx, double ty) {
38          this(sx, shy, shx, sy, tx, ty, null, 0);
39      }
40  
41      /***    
42       * Creates a transform edit with given scaling, shearing, translation, animation shape 
43       * and number of frames. 
44       */    
45      public Transform2D(double sx, double shy,
46                         double shx, double sy,
47                         double tx, double ty,
48                         Shape shape, int frames) {
49          super(shape, frames);
50          this.sx = sx;
51          this.shy = shy;
52          this.shx = shx;
53          this.sy = sy;
54          this.tx = tx;
55          this.ty = ty;
56      }
57      
58      public WiredEdit copy(RecordPlot plot) {
59          WiredEdit copy = new Transform2D(sx, shy, shx, sy, tx, ty, getShape(), getFrames());
60          copy.setRecordPlot(plot);
61          return copy;
62      }
63          
64      protected Shape[] getShapes(Shape shape, int steps) {
65          if ((shape == null) || (steps <= 1)) return null;
66          
67          Shape[] shapes = new Shape[steps];
68  
69          // calculate scale increments
70          double dsx = (sx - 1.0)/(steps-1);
71          double dsy = (sy - 1.0)/(steps-1);
72  
73          // calculate shear increments
74          double dshx = shx/(steps-1);
75          double dshy = shy/(steps-1);
76  
77          // calculate translate increments
78          double dtx = tx/(steps-1);
79          double dty = ty/(steps-1);
80  
81          // calculate window offset
82          double w = getRecordPlot().getWidth()/2.0;
83          double h = getRecordPlot().getHeight()/2.0;
84  
85          // calculate steps
86          for (int i=0; i<steps; i++) {
87              AffineTransform transform = new AffineTransform();
88              // put in the center
89              transform.translate(w, h);
90              // add our scale increment
91              transform.scale(1 + (i*dsx), 1 + (i*dsy));
92              // add our shear increment
93              transform.shear(-i*dshx, -i*dshy);
94              // add our translate increment
95              transform.translate(-i*dtx, i*dty);
96              // put back
97              transform.translate(-w, -h);
98              // transform
99              shapes[i] = transform.createTransformedShape(shape);
100         }
101         return shapes;
102     }
103 
104     public Shape createTransformedShape(Component component, Shape shape) {
105         double w = component.getWidth()/2.0;
106         double h = component.getHeight()/2.0;
107 
108         AffineTransform transform = new AffineTransform();
109         transform.translate(w, h);
110         transform.scale(sx, sy);
111         transform.shear(-shx, -shy);
112         transform.translate(-tx, ty);
113         transform.translate(-w, -h);
114         return transform.createTransformedShape(shape);
115     }
116 
117     public String getPresentationName() {
118         return "Transform2D"; // ("+sx+", "+shy+", "+shx+", "+sy+", "+tx+", "+ty+")";
119     }
120 
121     public boolean addEdit(UndoableEdit edit) {
122         if (edit instanceof Transform2D) {
123             // FIXME WIRED-248 check this, one could multiply transforms
124             return false;
125         }
126         return false;
127     }
128 
129     public boolean isSupportedBy(GraphicsPanel p) {
130         return p.supports(Transformable2D.class);
131     }
132 
133     protected void redoEdit() {
134         Transformable2D transformable2D = (Transformable2D)getRecordPlot().getGraphicsPanel().getFeature(Transformable2D.class);
135         if (transformable2D == null) {
136             System.err.println("Could not redo: "+this);
137             return;
138         }
139 
140         transformable2D.scale(sx, sy);
141         transformable2D.shear(shx, shy);
142         transformable2D.translate(tx, ty);
143 
144         getRecordPlot().repaint();
145         logger.finer(toString());
146     }
147 
148     protected void undoEdit() {
149         Transformable2D transformable2D = (Transformable2D)getRecordPlot().getGraphicsPanel().getFeature(Transformable2D.class);
150         if (transformable2D == null) {
151             System.err.println("Could not undo: "+this);
152             return;
153         }
154 
155         transformable2D.translate(-tx, -ty);
156         transformable2D.shear(-shx, -shy);
157         transformable2D.scale(1/sx, 1/sy);
158 
159         getRecordPlot().repaint();
160         logger.finer(toString());
161     }
162 
163     public String toString() {
164         return super.toString()+"; ("+sx+", "+shy+", "+shx+", "+sy+", "+tx+", "+ty+")";
165     }
166 }