Graphic Object Position and Size


All graphic objects have a location and a size, where location is the upper left x-y coordinate of the object's bounding box and size is the width and height of the bounding box. A Shape object's bounding box is the smallest box that surrounds the Shape. If a Shape is set to be selectable, the light gray selection box drawn around the Shape while it is selected corresponds with the Shape's bounding box. All Shapes may be moved and sized by setting the location and size of its bounding box.

Position- and Size-related Methods

Shape implements a number of mutator and accessor methods that may be used to modify its location and size, as well as to interrogate its current location and size. In all cases the methods can be thought of a modifying the Shape's bounding box. The Shape itself is modified to fill the new bounding box. The manner in which the Shape's defining coordinates change depend upon the Shape.

One additional Shape method to note is move(double dx, double dy). This is a convenience method that moves the location of a Shape by dx in the x-direction and by dy in the y-direction. It is useful when you have the amount by which a Shape should move relative to its current location rather than the new absolute position of a Shape.

The following Shape methods permit a Shape's bounding box to be modified. These methods may be used on all Shape subclasses.

MethodDescription
public double getHeight() Return the Shape's height.
public void setHeight(double height) Set the height of the Shape.
public double getWidth() Return the Shape's width.
public void setWidth(double width) Set the width of the Shape.
public Dimension getSize() Return the Shape's height and width as a Dimension object.
public void setSize(double width, double height) Set both the width and height of the Shape.
public Point getLocation() Return the location of the upper left corner of the Shape's bounding box as a Point object.
public void setLocation(double x, double y) Set the coordinates of the upper left corner of teh Shape's bounding box.
public double getX() Return the x-location of the upper left corner of the Shape's bounding box.
public void setX(double x) Set the x-coordinate of the upper left corner of the Shape's bounding box.
public double getY() Return the y-coordinate of the upper left corner of the Shape's bounding box.
public void setY(double y) Set the y-coordinate of the upper left corner of the Shape's bounding box.
public Point getCenter() Return the coordinates of the Shape's bounding box center.
public void setCenter(double cx, double cy) Move the Shape so that its bounding box has specified center coordinates.
public void move(double dx, double dy) Move a Shape by the distance (dx, dy).

Box Drive Example

In the following example a blue Rectangle is created and positioned on a Pad object. The default Pad object's keyPressed event is handled with a custom method that reacts to the arrow keys being pressed. Only arrow keys are tested; all other keys are ignore. When the down arrow is pressed, the box object's move(...) method is used to move the box 10 pixels down (by increasing its y-location). Likewise, the remaining three arrow keys move the box left, right or up, depending upon the key pressed. The result is a simple program that can be used to drive a box around the Pad using the arrow keys.

One item to note is the way the key String being pressed is tested. For all arrows keys the keyText String argument passed to the event handler method is compared to both a String describing the key (e.g. "Left") and a symbol (e.g. "←"). An arrow key is identified with a positive match to either the description or symbol String. The reason we test both is that the String passed to the event handler may be different depending upon your operating system. Specifically, Windows sets keyText to key description ("Left") while MacOS uses a symbol (e.g. "←"). Give it a try.

import doodlepad.*;

public class BoxDriver
{
    // The box to drive around the Pad
    private Rectangle box;

    public BoxDriver() {
        // Explicitly create a Pad and handle key-pressed event
        Pad p = new Pad(600, 600);
        p.setKeyPressedHandler( this::onKeyPressed );

        // Create ans style the box to drive
        box = new Rectangle(100, 100, 50, 50);
        box.setFillColor(0, 0, 255);
    }

    // Key pressed event handler moves box according key pressed
    public void onKeyPressed(Pad p, String keyText, String keyMods) {
        if (keyText.equals("Left") || keyText.equals("←")) {
            box.move(-10, 0);
        } else if (keyText.equals("Right") || keyText.equals("→")) {
            box.move(10, 0);
        } else if (keyText.equals("Up") || keyText.equals("↑")) {
            box.move(0, -10);
        } else if (keyText.equals("Down") || keyText.equals("↓")) {
            box.move(0, 10);
        }
    }

    public static void main(String[] args) {
        BoxDriver myBoxDriver = new BoxDriver();
    }
}