java.lang.Objectobjectdraw.ObjectDrawObject
objectdraw.Location
public class Location
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
Location
s.
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.
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 |
---|
public Location(double x, double y)
Location
representing the point (x
, y
).
x
- x coordinatey
- y coordiantepublic Location(Location point)
Location
that is a duplicate of the given point
.
point
- Location
to duplicatepublic Location(Point point)
Location
representing the same coordinates as the given
Java Point
.
point
- Point
to duplicateMethod Detail |
---|
public double distanceTo(Location point)
point
- Location
to find the distance to
this
and point
public boolean equals(Object other)
true
if the other object given is a Location
with the
same coordinates as this one, false
otherwise
equals
in class Object
Location
public double getDoubleX()
x
coordinate of this Location
, in double
precision.
x
coordinate of this Location
, in double
precisionpublic double getDoubleY()
y
coordinate of this Location
, in double
precision.
y
coordinate of this Location
, in double
precisionpublic int getX()
x
coordinate of this Location
, in integer precision.
x
coordinate of this Location
, in integer precisionpublic int getY()
y
coordinate of this Location
, in integer precision.
y
coordinate of this Location
, in integer precisionpublic Location offset(double dx, double dy)
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);
translate()
that creates
a new Location
, leaving the original unchanged.
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.
Location
offset from this one by the given amounts.public Point toPoint()
Point
object representing the coordinates of this Location
.
Point
object representing the coordinates of this Location
public String toString()
Location
might be constructed.
toString
in class objectdraw.ObjectDrawObject
Location
might be constructedpublic void translate(double dx, double dy)
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.
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.