Back to index

moin  1.9.0~rc2
RelativeLocator.java
Go to the documentation of this file.
00001 /*
00002  * @(#)RelativeLocator.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.standard;
00007 
00008 import java.awt.*;
00009 import java.io.IOException;
00010 import CH.ifa.draw.framework.*;
00011 import CH.ifa.draw.util.*;
00012 
00018 public class RelativeLocator extends AbstractLocator {
00019     /*
00020      * Serialization support.
00021      */
00022     private static final long serialVersionUID = 2619148876087898602L;
00023     private int relativeLocatorSerializedDataVersion = 1;
00024 
00025     double  fRelativeX;
00026     double  fRelativeY;
00027 
00028     public RelativeLocator() {
00029         fRelativeX = 0.0;
00030         fRelativeY = 0.0;
00031     }
00032 
00033     public RelativeLocator(double relativeX, double relativeY) {
00034         fRelativeX = relativeX;
00035         fRelativeY = relativeY;
00036     }
00037 
00038     public Point locate(Figure owner) {
00039         Rectangle r = owner.displayBox();
00040         return new Point(
00041             r.x + (int)(r.width*fRelativeX),
00042             r.y + (int)(r.height*fRelativeY)
00043         );
00044     }
00045 
00046     public void write(StorableOutput dw) {
00047         super.write(dw);
00048         dw.writeDouble(fRelativeX);
00049         dw.writeDouble(fRelativeY);
00050     }
00051 
00052     public void read(StorableInput dr) throws IOException {
00053         super.read(dr);
00054         fRelativeX = dr.readDouble();
00055         fRelativeY = dr.readDouble();
00056     }
00057 
00058     static public Locator east() {
00059         return new RelativeLocator(1.0, 0.5);
00060     }
00061 
00065     static public Locator north() {
00066         return new RelativeLocator(0.5, 0.0);
00067     }
00068 
00072     static public Locator west() {
00073         return new RelativeLocator(0.0, 0.5);
00074     }
00075 
00079     static public Locator northEast() {
00080         return new RelativeLocator(1.0, 0.0);
00081     }
00082 
00086     static public Locator northWest() {
00087         return new RelativeLocator(0.0, 0.0);
00088     }
00089 
00093     static public Locator south() {
00094         return new RelativeLocator(0.5, 1.0);
00095     }
00096 
00100     static public Locator southEast() {
00101         return new RelativeLocator(1.0, 1.0);
00102     }
00103 
00107     static public Locator southWest() {
00108         return new RelativeLocator(0.0, 1.0);
00109     }
00110 
00114     static public Locator center() {
00115         return new RelativeLocator(0.5, 0.5);
00116     }
00117 
00118 }