objectdraw
Class Location

java.lang.Object
  extended by objectdraw.ObjectDrawObject
      extended by objectdraw.Location
All Implemented Interfaces:
objectdraw.Dependent

public class Location
extends objectdraw.ObjectDrawObject

A Location object represents an (x, y) point on the screen. It is possible to interconvert between the Java Point object and a Location object using the methods and constructors in Location. You can retrieve coordinates from a Location, or translate it some distance across the screen. You can also use the distanceTo(Location other) method to find the distance between two Locations.

The translate() and offset() methods are a source of some confusion and need some more explanation. The offset() method leaves this Location unchanged, and returns a new Location some distance away. The translate() method, on the other hand, actually moves this Location to a new place. If this Location belongs to a particular object, this might result in that object moving.

Author:
Russell Zahniser (russell@zahniser.net)

Constructor Summary
Location(double x, double y)
          Construct a Location representing the point (x, y).
Location(Location point)
          Construct a Location that is a duplicate of the given point.
Location(Point point)
          Construct a Location representing the same coordinates as the given Java Point.
 
Method Summary
 double distanceTo(Location point)
          Find the distance from this point to another point.
 boolean equals(Object other)
          Return true if the other object given is a Location with the same coordinates as this one, false otherwise
 double getDoubleX()
          Get the x coordinate of this Location, in double precision.
 double getDoubleY()
          Get the y coordinate of this Location, in double precision.
 int getX()
          Get the x coordinate of this Location, in integer precision.
 int getY()
          Get the y coordinate of this Location, in integer precision.
 Location offset(double dx, double dy)
          Create a new Location that is offset from this one by the given amount.
 Point toPoint()
          Return a Java Point object representing the coordinates of this Location.
 String toString()
          Return a string describing how this Location might be constructed.
 void translate(double dx, double dy)
          Translate this Location by the given amount in the x and y.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Location

public Location(double x,
                double y)
Construct a Location representing the point (x, y).

Parameters:
x - x coordinate
y - y coordiante

Location

public Location(Location point)
Construct a Location that is a duplicate of the given point.

Parameters:
point - Location to duplicate

Location

public Location(Point point)
Construct a Location representing the same coordinates as the given Java Point.

Parameters:
point - Point to duplicate
Method Detail

distanceTo

public double distanceTo(Location point)
Find the distance from this point to another point. You could also think of this as the length of the line between the two points.

Parameters:
point - Location to find the distance to
Returns:
the distance between this and point

equals

public boolean equals(Object other)
Return true if the other object given is a Location with the same coordinates as this one, false otherwise

Overrides:
equals in class Object
Returns:
whether the other object given matches this Location

getDoubleX

public double getDoubleX()
Get the x coordinate of this Location, in double precision.

Returns:
the x coordinate of this Location, in double precision

getDoubleY

public double getDoubleY()
Get the y coordinate of this Location, in double precision.

Returns:
the y coordinate of this Location, in double precision

getX

public int getX()
Get the x coordinate of this Location, in integer precision.

Returns:
the x coordinate of this Location, in integer precision

getY

public int getY()
Get the y coordinate of this Location, in integer precision.

Returns:
the y coordinate of this Location, in integer precision

offset

public Location offset(double dx,
                       double dy)
Create a new Location that is offset from this one by the given amount. So, these two lines of code do the same thing:

Location loc1 = new Location(loc.getX() + 10, loc.getY() + 20);
Location loc1 = loc.offset(10, 20);


You can think of this as an alternative to translate() that creates a new Location, leaving the original unchanged.

Parameters:
dx - distance to move in the x direction. A positive dx places the new Location to the right of this one; a negative dx places it to the left.
dy - distance to move in the y direction. A positive dy places the new Location below this one; a negative dy places it above.
Returns:
A new Location offset from this one by the given amounts.

toPoint

public Point toPoint()
Return a Java Point object representing the coordinates of this Location.

Returns:
a Java Point object representing the coordinates of this Location

toString

public String toString()
Return a string describing how this Location might be constructed.

Overrides:
toString in class objectdraw.ObjectDrawObject
Returns:
a string describing how this Location might be constructed

translate

public void translate(double dx,
                      double dy)
Translate this Location by the given amount in the x and y. You can think of this as adding dx to its x and adding dy to its y. If this Location was retrieved by calling a shape's getLocation() method, translating the Location will cause the shape to move.

Parameters:
dx - distance to move in the x direction. A positive dx moves the point to the right; negative moves it to the left.
dy - distance to move in the y direction. A positive dy moves the point down; negative moves it up.