public abstract class GObject extends Object implements Cloneable, Serializable, GScalable
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
.
Modifier | Constructor and Description |
---|---|
protected |
GObject()
Constructs a new
GObject and initializes its state. |
Modifier and Type | Method and Description |
---|---|
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.
|
protected boolean |
areMouseListenersEnabled()
Returns
true if mouse listeners have ever been assigned to
this object. |
Object |
clone()
Returns a deep copy of this object.
|
static String |
colorName(Color color)
Translates a color to a string representation.
|
static String |
colorName(int colorRGB)
Translates a color RGB int to a string representation such as "0xFF00FF" or "YELLOW".
|
static String |
colorNameFriendly(int colorRGB)
Translates a color RGB int to a string representation such as "#ff00ff" or "dark gray".
|
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.
|
protected Graphics2D |
createTransformedGraphics(Graphics g)
Creates a new Graphics2D that includes any transformations done to this object,
such as scaling and rotation.
|
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.
|
protected void |
fireMouseListeners(MouseEvent e)
Sends the event to the appropriate listener.
|
double |
getBottomY()
Returns the bottom y-coordinate of the object.
|
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.
|
GPoint |
getCenterLocation()
Returns the central x/y-coordinates of this object.
|
double |
getCenterX()
Returns the central x-coordinate of this object.
|
double |
getCenterY()
Returns the central y-coordinate of this object.
|
Color |
getColor()
Returns the color used to display this object.
|
protected 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.
|
double |
getLineWidth()
Returns the thickness of lines drawn on this shape.
|
GPoint |
getLocation()
Returns the location of this object as a
GPoint . |
protected AffineTransform |
getMatrix()
Returns the transformation matrix for this object, including any scaling and rotation done to it.
|
protected 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.
|
double |
getRightX()
Returns the rightmost x-coordinate of the object.
|
acm.graphics.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 leftmost x-coordinate of the object.
|
double |
getY()
Returns the topmost y-coordinate of the object.
|
boolean |
intersects(GObject obj)
Returns true if this object touches the given other object.
|
static boolean |
isAntiAliasing() |
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 . |
protected abstract void |
paint2d(Graphics2D g)
All subclasses of
GObject must define a paint2d
method which allows the object to draw itself on the Graphics
context passed in as the parameter g . |
protected 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(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.
|
protected void |
repaint()
Signals that the object needs to be repainted.
|
void |
rotate(double degrees)
Rotates this object by the given degrees counter-clockwise.
|
void |
scale(double sf)
Scales the size of this object by the given factor in the x and y dimensions.
|
void |
scale(double sx,
double sy)
Scales the size of this object by the given factors in the x and y dimensions.
|
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.
|
static void |
setAntiAliasing(boolean value) |
void |
setBottomY(double bottomY)
Sets the bottom y-coordinate of this object to the given y value.
|
void |
setCenterLocation(double centerX,
double centerY)
Sets the central x/y-coordinates of this object to the given x/y values.
|
void |
setCenterLocation(GPoint pt)
Sets the central x/y-coordinates of this object to the given x/y values.
|
void |
setCenterX(double centerX)
Sets the central x-coordinate of this object to the given x value.
|
void |
setCenterY(double centerY)
Sets the central y-coordinate of this object to the given y value.
|
void |
setColor(Color color)
Sets the color used to display this object.
|
void |
setLineWidth(double lineWidth)
Sets the thickness of lines drawn on this shape to the given number of pixels.
|
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 |
setRightX(double rightX)
Sets the rightmost x-coordinate of this object to the given x value.
|
void |
setVisible(boolean visible)
Sets whether this object is visible.
|
void |
setX(double x)
Sets the leftmost x-coordinate of this object to the given x value.
|
void |
setY(double y)
Sets the topmost y-coordinate of this object to the given y value.
|
protected void |
start()
Starts a
GraphicsProgram containing this object. |
protected void |
start(String[] args)
Starts a
GraphicsProgram containing this object, passing
it the specified arguments. |
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 static boolean isAntiAliasing()
public static void setAntiAliasing(boolean value)
public void addActionListener(ActionListener listener)
listener
- Any object that implements the ActionListener
interfacepublic void addMouseListener(MouseListener listener)
listener
- Any object that implements the MouseListener
interfacepublic void addMouseMotionListener(MouseMotionListener listener)
listener
- Any object that implements the MouseMotionListener
interfaceprotected boolean areMouseListenersEnabled()
true
if mouse listeners have ever been assigned to
this object.true
if mouse listeners have been enabled in this objectpublic Object clone()
public static String colorName(Color color)
public static String colorName(int colorRGB)
public static String colorNameFriendly(int colorRGB)
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
otherwiseprotected Graphics2D createTransformedGraphics(Graphics g)
g
- initial graphics contextpublic void fireActionEvent(ActionEvent e)
e
- The ActionEvent
to firepublic void fireActionEvent(String actionCommand)
actionCommand
- The action command to include in the eventprotected void fireMouseListeners(MouseEvent e)
e
- The MouseEvent
that triggered this responsepublic double getBottomY()
public 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 final GPoint getCenterLocation()
public double getCenterX()
public double getCenterY()
public Color getColor()
protected Component getComponent()
null
if none exists.null
if none existspublic double getHeight()
public double getLineWidth()
public GPoint getLocation()
GPoint
.GPoint
protected AffineTransform getMatrix()
protected Color getObjectColor()
null
. It differs from getColor
in that
it does not walk up the containment chain.public GContainer getParent()
public double getRightX()
public acm.graphics.GDimension getSize()
public double getWidth()
public double getX()
public double getY()
public boolean intersects(GObject obj)
obj
- the other object to compare againstpublic boolean isVisible()
true
if the object is visible, otherwise false
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 final void paint(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 doneprotected abstract void paint2d(Graphics2D g)
GObject
must define a paint2d
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 doneprotected String paramString()
public void pause(double milliseconds)
Thread.sleep
, this method never throws an
exception.milliseconds
- The sleep time in millisecondspublic void removeActionListener(ActionListener listener)
listener
- The listener object to removepublic void removeMouseListener(MouseListener listener)
listener
- The listener object to removepublic void removeMouseMotionListener(MouseMotionListener listener)
listener
- The listener object to removeprotected void repaint()
public void rotate(double degrees)
degrees
- number of degrees to rotatepublic void scale(double sx, double sy)
public final void scale(double sf)
public void sendBackward()
public void sendForward()
public void sendToBack()
public void sendToFront()
public void setBottomY(double bottomY)
bottomY
- The new bottom y-coordinate for the objectpublic void setCenterLocation(double centerX, double centerY)
centerX
- The new central x-coordinate for the objectcenterY
- The new central y-coordinate for the objectpublic final void setCenterLocation(GPoint pt)
pt
- The new central x/y-coordinates for the objectpublic void setCenterX(double centerX)
centerX
- The new central x-coordinate for the objectpublic void setCenterY(double centerY)
centerY
- The new central y-coordinate for the objectpublic void setColor(Color color)
color
- The color used to display this objectpublic void setLineWidth(double lineWidth)
lineWidth
- line width in pixelspublic 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 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.public final void setRightX(double rightX)
rightX
- The new rightmost x-coordinate for the objectpublic void setVisible(boolean visible)
visible
- true
to make the object visible, false
to hide itpublic final void setX(double x)
x
- The new x-coordinate for the objectpublic final void setY(double y)
y
- The new y-coordinate for the objectprotected void start()
GraphicsProgram
containing this object.protected void start(String[] args)
GraphicsProgram
containing this object, passing
it the specified arguments.args
- The array of argumentspublic String toString()
toString
method in Object
to produce
more readable output.protected void updateEnabledList()