Back to index

moin  1.9.0~rc2
PolygonScaleHandle.java
Go to the documentation of this file.
00001 /*
00002  * Sat Mar  1 09:06:09 1997  Doug Lea  (dl at gee)
00003  * Based on RadiusHandle
00004  */
00005 
00006 package CH.ifa.draw.contrib;
00007 
00008 import java.awt.*;
00009 import java.util.*;
00010 import java.io.IOException;
00011 import CH.ifa.draw.framework.*;
00012 import CH.ifa.draw.util.*;
00013 import CH.ifa.draw.standard.*;
00014 
00015 
00019 class PolygonScaleHandle extends AbstractHandle {
00020 
00021   private Point fOrigin = null;
00022   private Point fCurrent = null;
00023   private Polygon fOrigPoly = null;
00024 
00025   public PolygonScaleHandle(PolygonFigure owner) {
00026     super(owner);
00027  }
00028 
00029   public void invokeStart(int  x, int  y, Drawing drawing) {
00030     fOrigPoly = ((PolygonFigure)(owner())).getPolygon();
00031     fOrigin = getOrigin();
00032     fCurrent = new Point(fOrigin.x, fOrigin.y);
00033   }
00034 
00035   public void invokeStep (int dx, int dy, Drawing drawing) {
00036     fCurrent = new Point(fOrigin.x + dx, fOrigin.y + dy);
00037     ((PolygonFigure)(owner())).scaleRotate(fOrigin, fOrigPoly, fCurrent);
00038   }
00039 
00040   public void invokeEnd  (int dx, int dy, Drawing drawing) {
00041     fOrigPoly = null;
00042     fOrigin = null;
00043     fCurrent = null;
00044   }
00045 
00046   public Point locate() {
00047     if (fCurrent != null)
00048       return fCurrent;
00049     else
00050       return getOrigin();
00051   }
00052 
00053   Point getOrigin() { // find a nice place to put handle
00054     // Need to pick a place that will not overlap with point handle
00055     // and is internal to polygon
00056 
00057     // Try for one HANDLESIZE step away from outermost toward center
00058 
00059     Point outer = ((PolygonFigure)(owner())).outermostPoint();
00060     Point ctr = ((PolygonFigure)(owner())).center();
00061     double len = Geom.length(outer.x, outer.y, ctr.x, ctr.y);
00062     if (len == 0) // best we can do?
00063       return new Point(outer.x - HANDLESIZE/2, outer.y + HANDLESIZE/2);
00064 
00065     double u = HANDLESIZE / len;
00066     if (u > 1.0) // best we can do?
00067       return new Point((outer.x * 3 + ctr.x)/4, (outer.y * 3 + ctr.y)/4);
00068     else
00069       return new Point((int)(outer.x * (1.0 - u) + ctr.x * u),
00070                        (int)(outer.y * (1.0 - u) + ctr.y * u));
00071   }
00072 
00073   public void draw(Graphics g) {
00074     Rectangle r = displayBox();
00075 
00076     g.setColor(Color.yellow);
00077     g.fillOval(r.x, r.y, r.width, r.height);
00078 
00079     g.setColor(Color.black);
00080     g.drawOval(r.x, r.y, r.width, r.height);
00081 
00082     /*
00083      * for debugging ...
00084     Point ctr = ((PolygonFigure)(owner())).center();
00085     g.setColor(Color.blue);
00086     g.fillOval(ctr.x, ctr.y, r.width, r.height);
00087 
00088     g.setColor(Color.black);
00089     g.drawOval(ctr.x, ctr.y, r.width, r.height);
00090 
00091     */
00092   }
00093 }
00094