acm.graphics
Class GObject

java.lang.Object
  extended by acm.graphics.GObject
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
GArc, GCompound, GImage, GLabel, GLine, GOval, GPen, GPolygon, GRect, GTurtle

public abstract class GObject
extends java.lang.Object
implements java.lang.Cloneable

This class is the common superclass of all graphical objects that can be displayed on a GCanvas. Because it is an abstract class, you are not allowed to construct an object whose class is GObject directly. What you do instead is construct one of the concrete subclasses like GRect or GLine. The purpose of this class definition is to define methods that apply to all graphical objects regardless of their specific class.


Field Summary
static java.awt.Color BLACK
          Constant representing the color black
static java.awt.Color BLUE
          Constant representing the color blue
static java.awt.Color CYAN
          Constant representing the color cyan
static java.awt.Color DARK_GRAY
          Constant representing the color dark gray
static java.awt.Color GRAY
          Constant representing the color gray
static java.awt.Color GREEN
          Constant representing the color green
static java.awt.Color LIGHT_GRAY
          Constant representing the color light gray
static java.awt.Color MAGENTA
          Constant representing the color magenta
static java.awt.Color ORANGE
          Constant representing the color orange
static java.awt.Color PINK
          Constant representing the color pink
static java.awt.Color RED
          Constant representing the color red
static java.awt.Color WHITE
          Constant representing the color white
static java.awt.Color YELLOW
          Constant representing the color yellow
 
Constructor Summary
protected GObject()
          Constructs a new GObject and initializes its state.
 
Method Summary
 void addActionListener(java.awt.event.ActionListener listener)
          Adds an action listener to this graphical object.
 void addMouseListener(java.awt.event.MouseListener listener)
          Adds a mouse listener to this graphical object.
 void addMouseMotionListener(java.awt.event.MouseMotionListener listener)
          Adds a mouse motion listener to this graphical object.
static double angle(double x, double y)
          Returns the angle in degrees from the origin to the point (xy).
protected static java.lang.String colorName(java.awt.Color color)
          Translates a color to a string representation.
 boolean contains(double x, double y)
          Checks to see whether a point is inside the object.
 boolean contains(GPoint pt)
          Checks to see whether a point is inside the object.
static double cosD(double angle)
          Returns the trigonometric sine of its argument where angle is expressed in degrees.
static double distance(double x, double y)
          Computes the distance between the origin and the point (xy).
static double distance(double x0, double y0, double x1, double y1)
          Computes the distance between the points (x0y0) and (x1y1).
 void fireActionEvent(java.awt.event.ActionEvent e)
          Triggers an action event for this graphical object.
 void fireActionEvent(java.lang.String actionCommand)
          Triggers an action event for this graphical object with the specified action command.
protected  void fireMouseListeners(java.awt.event.MouseEvent e)
          Sends the event to the appropriate listener.
abstract  GRectangle getBounds()
          Returns the bounding box of this object, which is defined to be the smallest rectangle that covers everything drawn by the figure.
 java.awt.Color getColor()
          Returns the color used to display this object.
protected  java.awt.Component getComponent()
          Returns the component in which this object is installed, or null if none exists.
 double getHeight()
          Returns the height of this object, which is defined to be the height of the bounding box.
 GPoint getLocation()
          Returns the location of this object as a GPoint.
protected  java.awt.Color getObjectColor()
          This method returns the color set for this specific object, which may be null.
 GContainer getParent()
          Returns the parent of this object, which is the canvas or compound object in which it is enclosed.
 GDimension getSize()
          Returns the size of the bounding box for this object.
 double getWidth()
          Returns the width of this object, which is defined to be the width of the bounding box.
 double getX()
          Returns the x-coordinate of the object.
 double getY()
          Returns the y-coordinate of the object.
 boolean isVisible()
          Checks to see whether this object is visible.
 void move(double dx, double dy)
          Moves the object on the screen using the displacements dx and dy.
 void movePolar(double r, double theta)
          Moves the object using displacements given in polar coordinates.
abstract  void paint(java.awt.Graphics g)
          All subclasses of GObject must define a paint method which allows the object to draw itself on the Graphics context passed in as the parameter g.
protected  void paintObject(java.awt.Graphics g)
          Paints the object by setting up the necessary parameters and then dispatching to the paint procedure for this object.
protected  java.lang.String paramString()
          Returns a string indicating the parameters of this object.
static void pause(double milliseconds)
          Delays the calling thread for the specified time, which is expressed in milliseconds.
 void removeActionListener(java.awt.event.ActionListener listener)
          Removes an action listener from this graphical object.
 void removeMouseListener(java.awt.event.MouseListener listener)
          Removes a mouse listener from this graphical object.
 void removeMouseMotionListener(java.awt.event.MouseMotionListener listener)
          Removes a mouse motion listener from this graphical object.
protected  void repaint()
          Signals that the object needs to be repainted.
static int round(double x)
          Rounds a double value to the nearest int.
 void sendBackward()
          Moves this object one step toward the back in the z dimension.
 void sendForward()
          Moves this object one step toward the front in the z dimension.
 void sendToBack()
          Moves this object to the back of the display in the z dimension.
 void sendToFront()
          Moves this object to the front of the display in the z dimension.
 void setColor(java.awt.Color c)
          Sets the color used to display this object.
 void setLocation(double x, double y)
          Sets the location of this object to the point (x, y).
 void setLocation(GPoint pt)
          Sets the location of this object to the specified point.
protected  void setParent(GContainer parent)
          Sets the parent of this object, which should be called only by the GContainer in which this is installed.
 void setVisible(boolean visible)
          Sets whether this object is visible.
static double sinD(double angle)
          Returns the trigonometric sine of its argument where angle is expressed in degrees.
static double tanD(double angle)
          Returns the trigonometric sine of its argument where angle is expressed in degrees.
static double toDegrees(double radians)
          Converts an angle from radians to degrees.
static double toRadians(double degrees)
          Converts an angle from degrees to radians.
 java.lang.String toString()
          Overrides the toString method in Object to produce more readable output.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

BLACK

public static final java.awt.Color BLACK
Constant representing the color black


BLUE

public static final java.awt.Color BLUE
Constant representing the color blue


CYAN

public static final java.awt.Color CYAN
Constant representing the color cyan


DARK_GRAY

public static final java.awt.Color DARK_GRAY
Constant representing the color dark gray


GRAY

public static final java.awt.Color GRAY
Constant representing the color gray


GREEN

public static final java.awt.Color GREEN
Constant representing the color green


LIGHT_GRAY

public static final java.awt.Color LIGHT_GRAY
Constant representing the color light gray


MAGENTA

public static final java.awt.Color MAGENTA
Constant representing the color magenta


ORANGE

public static final java.awt.Color ORANGE
Constant representing the color orange


PINK

public static final java.awt.Color PINK
Constant representing the color pink


RED

public static final java.awt.Color RED
Constant representing the color red


WHITE

public static final java.awt.Color WHITE
Constant representing the color white


YELLOW

public static final java.awt.Color YELLOW
Constant representing the color yellow

Constructor Detail

GObject

protected GObject()
Constructs a new GObject and initializes its state. This constructor is never called explicitly, but is instead invoked by the constructors of its subclasses.

Method Detail

paint

public abstract void paint(java.awt.Graphics g)
All subclasses of GObject must define a paint method which allows the object to draw itself on the Graphics context passed in as the parameter g.

Parameters:
g - The graphics context into which the painting is done

getBounds

public abstract GRectangle getBounds()
Returns the bounding box of this object, which is defined to be the smallest rectangle that covers everything drawn by the figure. The coordinates of this rectangle do not necessarily match the location returned by getLocation. Given a getLocation returns the coordinates of the point on the baseline at which the string begins; getBounds, by contrast, returns a rectangle that covers the entire window area occupied by the string.

Returns:
The bounding box for this object

setLocation

public void setLocation(double x,
                        double y)
Sets the location of this object to the point (x, y).

Parameters:
x - The new x-coordinate for the object
y - The new y-coordinate for the object

setLocation

public final void setLocation(GPoint pt)
Sets the location of this object to the specified point.

Parameters:
pt - The new location for this object

getLocation

public GPoint getLocation()
Returns the location of this object as a GPoint.

Returns:
The location of this object as a GPoint

getX

public double getX()
Returns the x-coordinate of the object.

Returns:
The x-coordinate of the object

getY

public double getY()
Returns the y-coordinate of the object.

Returns:
The y-coordinate of the object

move

public void move(double dx,
                 double dy)
Moves the object on the screen using the displacements dx and dy.

Parameters:
dx - The distance to move the object in the x direction (positive is rightward)
dy - The distance to move the object in the y direction (positive is downward)

movePolar

public final void movePolar(double r,
                            double theta)
Moves the object using displacements given in polar coordinates. The parameter r specifies the distance to move and theta specifies the angle in which the motion occurs. The angle is measured in degrees increasing counterclockwise from the +x axis.

Parameters:
r - The distance to move
theta - The angle in which to move, measured in degrees increasing counterclockwise from the +x axis

getSize

public GDimension getSize()
Returns the size of the bounding box for this object.

Returns:
The size of this object

getWidth

public double getWidth()
Returns the width of this object, which is defined to be the width of the bounding box.

Returns:
The width of this object on the screen

getHeight

public double getHeight()
Returns the height of this object, which is defined to be the height of the bounding box.

Returns:
The height of this object on the screen

contains

public boolean contains(double x,
                        double y)
Checks to see whether a point is inside the object. By default, this method simply checks to see if the point is inside the bounding box. Many subclasses will need to override this to check whether the point is contained in the shape.

Parameters:
x - The x-coordinate of the point being tested
y - The y-coordinate of the point being tested
Returns:
true if the point (xy) is inside the object, and false otherwise

contains

public final boolean contains(GPoint pt)
Checks to see whether a point is inside the object.

Parameters:
pt - The point being tested
Returns:
true if the point is inside the object, and false otherwise

sendToFront

public void sendToFront()
Moves this object to the front of the display in the z dimension. By moving it to the front, this object will appear to be on top of the other graphical objects on the display and may hide any objects that are further back.


sendToBack

public void sendToBack()
Moves this object to the back of the display in the z dimension. By moving it to the back, this object will appear to be behind the other graphical objects on the display and may be obscured by other objects in front.


sendForward

public void sendForward()
Moves this object one step toward the front in the z dimension. If it was already at the front of the stack, nothing happens.


sendBackward

public void sendBackward()
Moves this object one step toward the back in the z dimension. If it was already at the back of the stack, nothing happens.


setColor

public void setColor(java.awt.Color c)
Sets the color used to display this object.

Parameters:
color - The color used to display this object

getColor

public java.awt.Color getColor()
Returns the color used to display this object.

Returns:
The color used to display this object

setVisible

public void setVisible(boolean visible)
Sets whether this object is visible.

Parameters:
visible - true to make the object visible, false to hide it

isVisible

public boolean isVisible()
Checks to see whether this object is visible.

Returns:
true if the object is visible, otherwise false

toString

public java.lang.String toString()
Overrides the toString method in Object to produce more readable output.

Overrides:
toString in class java.lang.Object

getParent

public GContainer getParent()
Returns the parent of this object, which is the canvas or compound object in which it is enclosed.

Returns:
The parent of this object

pause

public static void pause(double milliseconds)
Delays the calling thread for the specified time, which is expressed in milliseconds. Unlike Thread.sleep, this method never throws an exception.

Parameters:
milliseconds - The sleep time in milliseconds

round

public static int round(double x)
Rounds a double value to the nearest int.

Parameters:
x - A double value
Returns:
The nearest int value

sinD

public static double sinD(double angle)
Returns the trigonometric sine of its argument where angle is expressed in degrees.

Parameters:
angle - An angle measured in degrees
Returns:
The trigonometric sine of the angle

cosD

public static double cosD(double angle)
Returns the trigonometric sine of its argument where angle is expressed in degrees.

Parameters:
angle - An angle measured in degrees
Returns:
The trigonometric cosine of the angle

tanD

public static double tanD(double angle)
Returns the trigonometric sine of its argument where angle is expressed in degrees.

Parameters:
angle - An angle measured in degrees
Returns:
The trigonometric tangent of the angle

toDegrees

public static double toDegrees(double radians)
Converts an angle from radians to degrees. This method is defined in the Math class, but was added only in JDK1.2, which is not supported in all browsers.

Parameters:
radians - An angle measured in radians
Returns:
The equivalent angle in degrees

toRadians

public static double toRadians(double degrees)
Converts an angle from degrees to radians. This method is defined in the Math class, but was added only in JDK1.2, which is not supported in all browsers.

Parameters:
degrees - An angle measured in degrees
Returns:
The equivalent angle in radians

distance

public static double distance(double x,
                              double y)
Computes the distance between the origin and the point (xy).

Parameters:
x - The x-coordinate of the point
y - The y-coordinate of the point
Returns:
The distance from the origin to the point (xy)

distance

public static double distance(double x0,
                              double y0,
                              double x1,
                              double y1)
Computes the distance between the points (x0y0) and (x1y1).

Parameters:
x0 - The x-coordinate of one point
y0 - The y-coordinate of that point
x1 - The x-coordinate of the other point
y1 - The y-coordinate of that point
Returns:
The distance between the points (x0y0) and (x1y1)

angle

public static double angle(double x,
                           double y)
Returns the angle in degrees from the origin to the point (xy). This method is easier to use than atan2 because it specifies the displacements in the usual x/y order and because it takes care of the fact that the Java coordinate system is flipped. The point (0, 0) is arbitrarily defined to be at angle 0.

Parameters:
x - The x-coordinate of the point
y - The y-coordinate of the point
Returns:
The angle from the origin to the point (xy) measured in degrees counterclockwise from the +x axis

addMouseListener

public void addMouseListener(java.awt.event.MouseListener listener)
Adds a mouse listener to this graphical object.

Parameters:
listener - Any object that implements the MouseListener interface

removeMouseListener

public void removeMouseListener(java.awt.event.MouseListener listener)
Removes a mouse listener from this graphical object.

Parameters:
listener - The listener object to remove

addMouseMotionListener

public void addMouseMotionListener(java.awt.event.MouseMotionListener listener)
Adds a mouse motion listener to this graphical object.

Parameters:
listener - Any object that implements the MouseMotionListener interface

removeMouseMotionListener

public void removeMouseMotionListener(java.awt.event.MouseMotionListener listener)
Removes a mouse motion listener from this graphical object.

Parameters:
listener - The listener object to remove

addActionListener

public void addActionListener(java.awt.event.ActionListener listener)
Adds an action listener to this graphical object.

Parameters:
listener - Any object that implements the ActionListener interface

removeActionListener

public void removeActionListener(java.awt.event.ActionListener listener)
Removes an action listener from this graphical object.

Parameters:
listener - The listener object to remove

fireActionEvent

public void fireActionEvent(java.lang.String actionCommand)
Triggers an action event for this graphical object with the specified action command.

Parameters:
actionCommand - The action command to include in the event

fireActionEvent

public void fireActionEvent(java.awt.event.ActionEvent e)
Triggers an action event for this graphical object.

Parameters:
e - The ActionEvent to fire

fireMouseListeners

protected void fireMouseListeners(java.awt.event.MouseEvent e)
Sends the event to the appropriate listener.

Parameters:
e - The MouseEvent that triggered this response

getObjectColor

protected java.awt.Color getObjectColor()
This method returns the color set for this specific object, which may be null. It differs from getColor in that it does not walk up the containment chain.


paramString

protected java.lang.String paramString()
Returns a string indicating the parameters of this object.


colorName

protected static java.lang.String colorName(java.awt.Color color)
Translates a color to a string representation.


paintObject

protected void paintObject(java.awt.Graphics g)
Paints the object by setting up the necessary parameters and then dispatching to the paint procedure for this object.


setParent

protected void setParent(GContainer parent)
Sets the parent of this object, which should be called only by the GContainer in which this is installed.


getComponent

protected java.awt.Component getComponent()
Returns the component in which this object is installed, or null if none exists.

Returns:
The component in which this object is installed, or null if none exists

repaint

protected void repaint()
Signals that the object needs to be repainted.