Back to index

moin  1.9.0~rc2
TriangleRotationHandle.java
Go to the documentation of this file.
00001 /*
00002  * Sun Mar  2 19:15:28 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 TriangleRotationHandle extends AbstractHandle {
00020 
00021   private Point fOrigin = null;
00022   private Point fCenter = null;
00023 
00024   public TriangleRotationHandle(TriangleFigure owner) {
00025     super(owner);
00026  }
00027 
00028   public void invokeStart(int  x, int  y, Drawing drawing) {
00029     fCenter = owner().center();
00030     fOrigin = getOrigin();
00031   }
00032 
00033   public void invokeStep (int dx, int dy, Drawing drawing) {
00034     double angle = Math.atan2(fOrigin.y + dy - fCenter.y,
00035                               fOrigin.x + dx - fCenter.x);
00036     ((TriangleFigure)(owner())).rotate(angle);
00037   }
00038 
00039   public void invokeEnd  (int dx, int dy, Drawing drawing) {
00040     fOrigin = null;
00041     fCenter = null;
00042   }
00043 
00044   public Point locate() {
00045     return getOrigin();
00046   }
00047 
00048   Point getOrigin() { // find a nice place to put handle
00049     // almost same code as PolygonScaleHandle
00050     Polygon p = ((TriangleFigure)(owner())).polygon();
00051     Point first = new Point(p.xpoints[0], p.ypoints[0]);
00052     Point ctr = owner().center();
00053     double len = Geom.length(first.x, first.y, ctr.x, ctr.y);
00054     if (len == 0) // best we can do?
00055       return new Point(first.x - HANDLESIZE/2, first.y + HANDLESIZE/2);
00056 
00057     double u = HANDLESIZE / len;
00058     if (u > 1.0) // best we can do?
00059       return new Point((first.x * 3 + ctr.x)/4, (first.y * 3 + ctr.y)/4);
00060     else
00061       return new Point((int)(first.x * (1.0 - u) + ctr.x * u),
00062                        (int)(first.y * (1.0 - u) + ctr.y * u));
00063   }
00064 
00065   public void draw(Graphics g) {
00066     Rectangle r = displayBox();
00067 
00068     g.setColor(Color.yellow);
00069     g.fillOval(r.x, r.y, r.width, r.height);
00070 
00071     g.setColor(Color.black);
00072     g.drawOval(r.x, r.y, r.width, r.height);
00073   }
00074 }
00075