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.Shearable2D;
13  
14  /***
15   * Shears a plot in 2D.
16   *
17   * @author Mark Donszelmann
18   * @version $Id: Shear.java 674 2005-03-11 04:53:14Z duns $
19   */
20  public class Shear extends AnimatedWiredEdit implements GraphicsPanelEdit {
21  
22      private static Logger logger = Logger.getLogger(Shear.class.getPackage().getName());
23      private double shx, shy;
24  
25      /***    
26       * Creates a shear edit with no shearing. 
27       */    
28      public Shear() {
29          this(0, 0);
30      }
31  
32      /***    
33       * Creates a shear edit with given shearing. 
34       */    
35      public Shear(double shx, double shy) {
36          this(shx, shy, null, 0);
37      }
38  
39      /***    
40       * Creates a shear edit with given shearing, animation shape and number of frames. 
41       */    
42      public Shear(double shx, double shy,
43                   Shape shape, int frames) {
44          super(shape, frames);
45          this.shx = shx;
46          this.shy = shy;
47      }
48      
49      public WiredEdit copy(RecordPlot plot) {
50          WiredEdit copy = new Shear(shx, shy, getShape(), getFrames());
51          copy.setRecordPlot(plot);
52          return copy;
53      }
54      
55      protected Shape[] getShapes(Shape shape, int steps) {
56          if ((shape == null) || (steps <= 1)) return null;
57          
58          Shape[] shapes = new Shape[steps];
59  
60          // calculate shear increments
61          double dshx = shx/(steps-1);
62          double dshy = shy/(steps-1);
63  
64          // calculate window offset
65          double w = getRecordPlot().getWidth()/2.0;
66          double h = getRecordPlot().getHeight()/2.0;
67  
68          // calculate steps
69          for (int i=0; i<steps; i++) {
70              AffineTransform transform = new AffineTransform();
71              // put in the center
72              transform.translate(w, h);
73              // add our shear increment
74              transform.shear(-i*dshx, -i*dshy);
75              // put back
76              transform.translate(-w, -h);
77              // transform
78              shapes[i] = transform.createTransformedShape(shape);
79          }
80          return shapes;
81      }
82  
83      public Shape createTransformedShape(Component component, Shape shape) {
84          double w = component.getWidth()/2.0;
85          double h = component.getHeight()/2.0;
86  
87          AffineTransform transform = new AffineTransform();
88          transform.translate(w, h);
89          transform.shear(-shx, -shy);
90          transform.translate(-w, -h);
91          return transform.createTransformedShape(shape);
92      }
93  
94      public String getPresentationName() {
95          return toString()+" ("+format.format(shx)+", "+format.format(shy)+")";
96      }
97  
98      public String toString() {
99          return "Shear";
100     }
101 
102     public boolean addEdit(UndoableEdit edit) {
103         if (edit instanceof Shear) {
104             Shear shear = (Shear)edit;
105             shx *= shear.shx;
106             shy *= shear.shy;
107             return true;
108         }
109         return false;
110     }
111 
112     public boolean isSupportedBy(GraphicsPanel p) {
113         return p.supports(Shearable2D.class);
114     }
115 
116     protected void redoEdit() {
117         Shearable2D shearable = (Shearable2D)getRecordPlot().getGraphicsPanel().getFeature(Shearable2D.class);
118         if (shearable == null) {
119             System.err.println("Could not redo: "+this);
120             return;
121         }
122         shearable.shear(shx, shy);
123         getRecordPlot().repaint();
124         logger.finer(toString());
125     }
126 
127     protected void undoEdit() {
128         Shearable2D shearable = (Shearable2D)getRecordPlot().getGraphicsPanel().getFeature(Shearable2D.class);
129         if (shearable == null) {
130             System.err.println("Could not undo: "+this);
131             return;
132         }
133         shearable.shear((shx == 0) ? 0 : 1/shx, (shy == 0) ? 0 : 1/shy);
134         getRecordPlot().repaint();
135         logger.finer(toString());
136     }
137 }