3.4. Rectangles

3.4.1. RectangularShape

As Figure 3.3 shows, java.awt.geom.RectangularShape is an important class. It's the abstract parent class of the rectangle, rounded rectangle, arc, and ellipse classes in java.awt.geom. It implements the methods of Shape and adds a few of its own.

First, a RectangularShape returns information about its location and size:

public abstract double getX()

This method returns the horizontal location of the left side of the rectangle.

public abstract double getY()

This method returns the vertical location of the top side of the rectangle.

public abstract double getWidth()

This method returns the width of the rectangle.

public abstract double getHeight()

This method returns the height of the rectangle.

public double getMinX()

This method returns the smallest x coordinate of the rectangle.

public double getMaxX()

This method returns the largest x coordinate of the rectangle.

public double getMinY()

This method returns the smallest y coordinate of the rectangle.

public double getMaxY()

This method returns the largest y coordinate of the rectangle.

Two methods are provided to examine the center point of the rectangle:

public double getCenterX()

public double getCenterY()

A rectangular shape is empty if its width and height are 0. The isEmpty() method returns true in this case:

public abstract boolean isEmpty()

Finally, RectangularShape includes three methods for setting the bounds of the shape:

public abstract void setFrame(double x, double ...

Get Java 2D Graphics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.