The Location class represents an (x, y) location on the screen. Nearly every drawable object uses at least one Location to remember where it is, so you can use Location to build objects. In the mouse event methods - onMouseMove() and the like - you are given a Location saying where the mouse is. So, it is also useful to be able to get the coordinates from a Location object, or to ask how close it is to some other Location.

Location is part of the objectdraw library; you are automatically able to use it once you import objectdraw.* at the top of your java file.

Table of Contents

1. Creating a Location

Your browser doesn't do Java.
You will usually construct Locations using this constructor:

public Location(double x, double y)

This creates a Location using two numbers that you give it to be its x and y coordinates. Creating a Location in this way would look like this:

new Location(10, 190);
new Location(180, 20);

The first Location there will be created near the bottom left of a 200 by 200 screen; the second, near the top right. Remember, the y direction on a computer screen goes opposite what you would expect from math: y = 0 is the top and y = canvas.getHeight() is the bottom.

Note that a Location is not actually a visible object: it marks a point on the screen, but it is invisible, although I can use it to make some visible object:

new FramedOval(new Location(10, 190), new Location(180, 20), canvas);

2. Retrieving the coordinates from a Location

In event methods like onMousePress(), you are given a Location representing whatever point the user clicked at; you may want to find out the actual numerical coordinates that it contains:

public int getX()
public int getY()

So, for example, if I were making a Pong-like game where I use the mouse to move a paddle back and forth to hit a ball, I could set the paddle's x location to match the x of the mouse:

public void onMouseMove(Location point) {
   paddle.moveTo(point.getX() - PADDLE_WIDTH / 2, paddle.getY());

3. Moving a Location

Your browser doesn't do Java.
Sometimes it isn't enough to be able to make a Location anywhere I want; instead, I want to have a Location that is at some particular offset from a Location I already have, or I want to move my Location across the screen in some kind of increment and place objects at each location.

It is possible, once a Location is created, to translate it, shifting it some amount over and some amount up:

public void translate(double dx, double dy)

This will add dx to the x coordinate of the Location, and add dy to the y coordinate. This is often used to make objects that are centered around a given point rather than having their top left corner there:

public void onMousePress(Location point) {
   point.translate(-10, -10);
   new FilledOval(point, 20, 20, canvas);

Another common application is to move a location every time I click, or move it as I go through a loop:

Location loc = new Location(0, 0);
for(int i = 0; i < 10; i++) {
   new FilledOval(loc, 10, 10);
   loc.translate(15, 20);

4. Finding the distance between locations

You will occasionally want to use this method to find the distance between two Location objects:

public double distanceTo(Location other)

It simply finds the distance using the Pythagorean Theorem. A common use of it is to get the length of a line by measuring the distance from its start point to its end point:

double length = line.getStart().distanceTo(line.getEnd());

5. Locations and Points

When you get to the point of writing GUI code - buttons and text fields and the like - you will be using the Java libraries' version of Location, which is called java.awt.Point. The Location class is designed to make it easy for you to convert between the two. There is a constructor to make a Location matching a Point, and a method of Location that will return a Point that matches it.

public Location(java.awt.Point point)

java.awt.Point toPoint()

You convert between the two like this:

import objectdraw.*;
import java.awt.*;


Location loc = new Location(40, 80);
Point p = loc.toPoint();
Location loc2 = new Location(p);

6. The full Location specification

Here is a concise listing of all the constructors and methods of Location you might use:

public Location(double x, double y)
public Location(Location clone)
public Location(java.awt.Point point)

public int getX()
public int getY()
public double getDoubleX()
public double getDoubleY()
public void translate(double dx, double dy)
public double distanceTo(Location other)
public java.awt.Point toPoint()