public abstract class GObject
extends java.lang.Object
implements java.lang.Cloneable
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.Modifier | Constructor and Description |
---|---|
protected |
GObject()
Constructs a new
GObject and initializes its state. |
Modifier and Type | Method and Description |
---|---|
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.
|
protected boolean |
areMouseListenersEnabled()
Returns
true if mouse listeners have ever been assigned to this
object. |
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.
|
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.
|
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.
|
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 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 |
setLocation(GPoint pt)
Sets the location of this object to the specified point.
|
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.
|
protected void |
start()
Starts a
GraphicsProgram containing this object. |
protected void |
start(java.lang.String[] args)
Starts a
GraphicsProgram containing this object, passing it the
specified arguments. |
java.lang.String |
toString()
Overrides the
toString method in Object to produce
more readable output. |
protected void |
updateEnabledList()
Tells the parent to update its list of enabled objects.
|
protected GObject()
GObject
and initializes its state. This
constructor is never called explicitly, but is instead invoked by the
constructors of its subclasses.public abstract void paint(java.awt.Graphics g)
GObject
must define a paint
method which allows the object to draw itself on the Graphics
context passed in as the parameter g
.g
- The graphics context into which the painting is donepublic abstract GRectangle getBounds()
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.public void setLocation(double x, double y)
x
,
y
).x
- The new x-coordinate for the objecty
- The new y-coordinate for the objectpublic final void setLocation(GPoint pt)
pt
- The new location for this objectpublic GPoint getLocation()
GPoint
.GPoint
public double getX()
public double getY()
public void move(double dx, double dy)
dx
and
dy
.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)
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.r
- The distance to movetheta
- The angle in which to move, measured in degrees increasing
counterclockwise from the +x axispublic GDimension getSize()
public double getWidth()
public double getHeight()
public boolean contains(double x, double y)
x
- The x-coordinate of the point being testedy
- The y-coordinate of the point being testedtrue
if the point (x
, y
)
is inside the object, and false
otherwisepublic final boolean contains(GPoint pt)
pt
- The point being testedtrue
if the point is inside the object, and
false
otherwisepublic void sendToFront()
public void sendToBack()
public void sendForward()
public void sendBackward()
public void setColor(java.awt.Color color)
color
- The color used to display this objectpublic java.awt.Color getColor()
public void setVisible(boolean visible)
visible
- true
to make the object visible,
false
to hide itpublic boolean isVisible()
true
if the object is visible, otherwise
false
public java.lang.String toString()
toString
method in Object
to produce
more readable output.toString
in class java.lang.Object
public GContainer getParent()
public void pause(double milliseconds)
Thread.sleep
, this method never throws an
exception.milliseconds
- The sleep time in millisecondspublic void addMouseListener(java.awt.event.MouseListener listener)
listener
- Any object that implements the MouseListener
interfacepublic void removeMouseListener(java.awt.event.MouseListener listener)
listener
- The listener object to removepublic void addMouseMotionListener(java.awt.event.MouseMotionListener listener)
listener
- Any object that implements the
MouseMotionListener
interfacepublic void removeMouseMotionListener(java.awt.event.MouseMotionListener listener)
listener
- The listener object to removepublic void addActionListener(java.awt.event.ActionListener listener)
listener
- Any object that implements the ActionListener
interfacepublic void removeActionListener(java.awt.event.ActionListener listener)
listener
- The listener object to removepublic void fireActionEvent(java.lang.String actionCommand)
actionCommand
- The action command to include in the eventpublic void fireActionEvent(java.awt.event.ActionEvent e)
e
- The ActionEvent
to firepublic void setParent(GContainer parent)
GContainer
in which this is installed. The serialization
behavior of the parent data depends on the parent type. Because a
GCompound
is serializable, it needs to be maintained in a
nontransient variable; other parent classes are transient, so that these
parents are not recorded in the serial form.protected void fireMouseListeners(java.awt.event.MouseEvent e)
e
- The MouseEvent
that triggered this responseprotected boolean areMouseListenersEnabled()
true
if mouse listeners have ever been assigned to this
object.true
if mouse listeners have been enabled in this objectprotected void start()
GraphicsProgram
containing this object.protected void start(java.lang.String[] args)
GraphicsProgram
containing this object, passing it the
specified arguments.args
- The array of argumentsprotected java.awt.Color getObjectColor()
null
. It differs from getColor
in that it does not
walk up the containment chain.protected java.lang.String paramString()
protected static java.lang.String colorName(java.awt.Color color)
protected void paintObject(java.awt.Graphics g)
paint
procedure for this object.protected java.awt.Component getComponent()
null
if none exists.null
if none existsprotected void updateEnabledList()
protected void repaint()