acm.graphics
Class GObject

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

public abstract class GObject
extends Object
implements Cloneable, Serializable

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.

The GObject class implements the Serializable interface by saving all of the internal state of the object. The parent, however, is saved only if the parent is a GCompound.


Method Summary
 void addActionListener(ActionListener listener)
Adds an action listener to this graphical object.
 void addMouseListener(MouseListener listener)
Adds a mouse listener to this graphical object.
 void addMouseMotionListener(MouseMotionListener listener)
Adds a mouse motion listener to this graphical object.
 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.
 void fireActionEvent(ActionEvent e)
Triggers an action event for this graphical object.
 void fireActionEvent(String actionCommand)
Triggers an action event for this graphical object with the specified action command.
GRectangle getBounds()
Returns the bounding box of this object, which is defined to be the smallest rectangle that covers everything drawn by the figure.
 Color getColor()
Returns the color used to display this object.
 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.
 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.
void paint(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.
 void pause(double milliseconds)
Delays the calling thread for the specified time, which is expressed in milliseconds.
 void removeActionListener(ActionListener listener)
Removes an action listener from this graphical object.
 void removeMouseListener(MouseListener listener)
Removes a mouse listener from this graphical object.
 void removeMouseMotionListener(MouseMotionListener listener)
Removes a mouse motion listener from this graphical object.
 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(Color color)
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 setVisible(boolean visible)
Sets whether this object is visible.
 

Method Detail

public void addActionListener(ActionListener listener)

Adds an action listener to this graphical object.

 
Usage: gobj.addActionListener(listener); 
Parameter: 
listener  Any object that implements the ActionListener interface
 


public void addMouseListener(MouseListener listener)

Adds a mouse listener to this graphical object.

 
Usage: gobj.addMouseListener(listener); 
Parameter: 
listener  Any object that implements the MouseListener interface
 


public void addMouseMotionListener(MouseMotionListener listener)

Adds a mouse motion listener to this graphical object.

 
Usage: gobj.addMouseMotionListener(listener); 
Parameter: 
listener  Any object that implements the MouseMotionListener interface
 


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.

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


public final boolean contains(GPoint pt)

Checks to see whether a point is inside the object.

 
Usage: if (gobj.contains(pt)) . . . 
Parameter: 
pt  The point being tested
Returns: true if the point is inside the object, and false otherwise
 


public void fireActionEvent(ActionEvent e)

Triggers an action event for this graphical object.

 
Usage: gobj.fireActionEvent(e); 
Parameter: 
 The ActionEvent to fire
 


public void fireActionEvent(String actionCommand)

Triggers an action event for this graphical object with the specified action command.

 
Usage: gobj.fireActionEvent(actionCommand); 
Parameter: 
actionCommand  The action command to include in the event
 


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 GLabel object, for example, 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.

 
Usage: GRectangle bounds = gobj.getBounds(); 
Returns: The bounding box for this object
 


public Color getColor()

Returns the color used to display this object.

 
Usage: Color color = gobj.getColor(); 
Returns: The color used to display this object
 


public double getHeight()

Returns the height of this object, which is defined to be the height of the bounding box.

 
Usage: double height = gobj.getHeight(); 
Returns: The height of this object on the screen
 


public GPoint getLocation()

Returns the location of this object as a GPoint.

 
Usage: GPoint pt = gobj.getLocation(); 
Returns: The location of this object as a GPoint
 


public GContainer getParent()

Returns the parent of this object, which is the canvas or compound object in which it is enclosed.

 
Usage: GContainer parent = gobj.getParent(); 
Returns: The parent of this object
 


public GDimension getSize()

Returns the size of the bounding box for this object.

 
Usage: GDimension size = gobj.getSize(); 
Returns: The size of this object
 


public double getWidth()

Returns the width of this object, which is defined to be the width of the bounding box.

 
Usage: double width = gobj.getWidth(); 
Returns: The width of this object on the screen
 


public double getX()

Returns the x-coordinate of the object.

 
Usage: double x = gobj.getX(); 
Returns: The x-coordinate of the object
 


public double getY()

Returns the y-coordinate of the object.

 
Usage: double y = gobj.getY(); 
Returns: The y-coordinate of the object
 


public boolean isVisible()

Checks to see whether this object is visible.

 
Usage: if (gobj.isVisible()) . . . 
Returns: true if the object is visible, otherwise false
 


public void move(double dx, double dy)

Moves the object on the screen using the displacements dx and dy.

 
Usage: gobj.move(dx, 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)
 


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.

 
Usage: gobj.movePolar(r, theta); 
Parameters: 
 The distance to move
theta  The angle in which to move, measured in degrees increasing counterclockwise from the +x axis
 


public abstract void paint(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.

 
Usage: gobj.paint(g); 
Parameter: 
 The graphics context into which the painting is done
 


public 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.

 
Usage: gobj.pause(milliseconds); 
Parameter: 
milliseconds  The sleep time in milliseconds
 


public void removeActionListener(ActionListener listener)

Removes an action listener from this graphical object.

 
Usage: gobj.removeActionListener(listener); 
Parameter: 
listener  The listener object to remove
 


public void removeMouseListener(MouseListener listener)

Removes a mouse listener from this graphical object.

 
Usage: gobj.removeMouseListener(listener); 
Parameter: 
listener  The listener object to remove
 


public void removeMouseMotionListener(MouseMotionListener listener)

Removes a mouse motion listener from this graphical object.

 
Usage: gobj.removeMouseMotionListener(listener); 
Parameter: 
listener  The listener object to remove
 


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.

 
Usage: gobj.sendBackward(); 
 


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.

 
Usage: gobj.sendForward(); 
 


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.

 
Usage: gobj.sendToBack(); 
 


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.

 
Usage: gobj.sendToFront(); 
 


public void setColor(Color color)

Sets the color used to display this object.

 
Usage: gobj.setColor(color); 
Parameter: 
color  The color used to display this object
 


public void setLocation(double x, double y)

Sets the location of this object to the point (x, y).

 
Usage: gobj.setLocation(x, y); 
Parameters: 
 The new x-coordinate for the object
 The new y-coordinate for the object
 


public void setVisible(boolean visible)

Sets whether this object is visible.

 
Usage: gobj.setVisible(visible); 
Parameter: 
visible  true to make the object visible, false to hide it