public class GPen extends GObject
GPen
class simulates a pen drawing on a canvas. As with the
other graphical objects in the acm.graphics
package, you use this
class by constructing a new GPen
object and adding that object
to a GCanvas
. You can move the pen on the canvas by using the methods
setLocation(double, double)
, GObject.setLocation(GPoint)
, and
GObject.move(double, double)
and then draw lines using
drawLine(double, double)
. The GPen
object remembers the
series of lines and can therefore repaint the screen image when necessary.Constructor and Description |
---|
GPen()
Creates a new
GPen object with an empty path. |
GPen(double x,
double y)
Creates a new
GPen object with an empty path, initially
positioned at the point (x , y ). |
Modifier and Type | Method and Description |
---|---|
boolean |
contains(double x,
double y)
Contains is defined to be false for the
GPen object to avoid
having the trace intercept mouse clicks. |
void |
drawLine(double dx,
double dy)
Draws a line segment with displacements
dx and dy . |
protected void |
drawPen(java.awt.Graphics g)
This method draws a representation of the pen at the current location.
|
void |
drawPolarLine(double r,
double theta)
Draws a line segment using displacements given in polar coordinates.
|
void |
endFilledRegion()
Ends the definition of a region and fills it with the current color.
|
void |
erasePath()
Erases the entire path drawn by the pen but does not change its position.
|
GRectangle |
getBounds()
Returns the bounding box for the entire figure traced by the pen.
|
java.awt.Color |
getFillColor()
Returns the color used to display the filled region of this object.
|
protected java.awt.Rectangle |
getPenBounds()
This method returns the bounds that the pen occupies.
|
java.awt.Image |
getPenImage()
Returns the image used to draw the pen when
setPenVisible has
been called. |
double |
getSpeed()
Returns the current speed of the pen.
|
void |
hidePen()
Makes the pen itself invisible.
|
boolean |
isPenVisible()
Returns whether the pen is visible.
|
void |
paint(java.awt.Graphics g)
Implements the
paint operation for this graphical object. |
void |
scale(double sx,
double sy)
Sets the scale factors
sx and sy . |
void |
setColor(java.awt.Color color)
Sets the color of the pen.
|
void |
setFillColor(java.awt.Color color)
Sets the color used to fill a region.
|
void |
setLocation(double x,
double y)
Moves the pen to the point (
x , y ) without drawing a
line. |
void |
setPenImage(java.awt.Image image)
Sets the image of the pen to be the specified image.
|
void |
setSpeed(double speed)
Sets the speed of the pen, which must be a number between 0 (slowest) and 1
(fastest).
|
void |
showPen()
Makes the pen itself visible.
|
void |
startFilledRegion()
Starts defining a region that will be filled with the current color.
|
addActionListener, addMouseListener, addMouseMotionListener, areMouseListenersEnabled, colorName, contains, fireActionEvent, fireActionEvent, fireMouseListeners, getColor, getComponent, getHeight, getLocation, getObjectColor, getParent, getSize, getWidth, getX, getY, isVisible, move, movePolar, paintObject, paramString, pause, removeActionListener, removeMouseListener, removeMouseMotionListener, repaint, sendBackward, sendForward, sendToBack, sendToFront, setLocation, setParent, setVisible, start, start, toString, updateEnabledList
public GPen()
GPen
object with an empty path.public GPen(double x, double y)
GPen
object with an empty path, initially
positioned at the point (x
, y
).x
- The initial x coordinate of the peny
- The initial y coordinate of the penpublic void erasePath()
public void setLocation(double x, double y)
x
, y
) without drawing a
line.setLocation
in class GObject
x
- The x-coordinate of the new positiony
- The y-coordinate of the new positionpublic void drawLine(double dx, double dy)
dx
and dy
.
Drawing a line leaves the pen positioned at the end of the line segment, so
that the next call to drawLine
will continue from where this one
finished.dx
- The extent of the line in the x directiondy
- The extent of the line in the y directionpublic final void drawPolarLine(double r, double theta)
r
specifies the length of the line segment, and
theta
specifies the angle at which the line is drawn. The angle
is measured in degrees increasing counterclockwise from the +x axis. Thus,
the call
drawPolarLine(1.0, 0);
draws a line extending rightward one inch from the current point. Similarly
drawPolarLine(2.0, 135);
draws a two-inch line extending to the northwest from the current point.
r
- The length of the line segmenttheta
- The angle at which to draw the line, measured in degrees
increasing counterclockwise from the +x axispublic void setColor(java.awt.Color color)
public void setFillColor(java.awt.Color color)
color
- The new color used to fill a regionpublic java.awt.Color getFillColor()
getFillColor
returns the color of the object.public void startFilledRegion()
endFilledRegion
will be used to create a polygon that is then
filled.public void endFilledRegion()
public void showPen()
drawPen
method will be called at the end of painting the path to show the current
location.public void hidePen()
public boolean isPenVisible()
true
if the pen is visible, otherwise false
public void setSpeed(double speed)
speed
- The speed of the pen (0 is slowest, 1 is fastest)public double getSpeed()
public void scale(double sx, double sy)
sx
and sy
. Scaling a
GPen
object changes the location of points as well the size of
line segments. This behavior seems counterintuitive for a pen installed
directly in a GCanvas
, but is quite useful when a
GPen
is embedded in a compound object.sx
- The factor used to scale all coordinates in the x directionsy
- The factor used to scale all coordinates in the y directionpublic void paint(java.awt.Graphics g)
paint
operation for this graphical object. This
method is not called directly by clients.public GRectangle getBounds()
public boolean contains(double x, double y)
GPen
object to avoid
having the trace intercept mouse clicks.public void setPenImage(java.awt.Image image)
image
- The new image to use for the penpublic java.awt.Image getPenImage()
setPenVisible
has
been called.protected void drawPen(java.awt.Graphics g)
g
- The graphics context in which to draw the penprotected java.awt.Rectangle getPenBounds()