acm.graphics
Class GPen

java.lang.Object
  extended by acm.graphics.GObject
      extended by acm.graphics.GPen
All Implemented Interfaces:
Serializable, Cloneable

public class GPen
extends GObject

The 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 and move and then draw lines using drawLine. The GPen object remembers the series of lines and can therefore repaint the screen image when necessary.

See Also:
Serialized Form

Constructor Summary
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).
 
Method Summary
 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(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.
 Color getFillColor()
          Returns the color used to display the filled region of this object.
protected  Rectangle getPenBounds()
          This method returns the bounds that the pen occupies.
 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(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(Color color)
          Sets the color of the pen.
 void setFillColor(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(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.
 
Methods inherited from class acm.graphics.GObject
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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GPen

public GPen()
Creates a new GPen object with an empty path.


GPen

public GPen(double x,
            double y)
Creates a new GPen object with an empty path, initially positioned at the point (x, y).

Parameters:
x - The initial x coordinate of the pen
y - The initial y coordinate of the pen
Method Detail

contains

public boolean contains(double x,
                        double y)
Contains is defined to be false for the GPen object to avoid having the trace intercept mouse clicks.

Overrides:
contains in class GObject
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

drawLine

public void drawLine(double dx,
                     double dy)
Draws a line segment with displacements 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.

Parameters:
dx - The extent of the line in the x direction
dy - The extent of the line in the y direction

drawPen

protected void drawPen(Graphics g)
This method draws a representation of the pen at the current location. Subclasses can override this method to draw fancier pens.

Parameters:
g - The graphics context in which to draw the pen

drawPolarLine

public final void drawPolarLine(double r,
                                double theta)
Draws a line segment using displacements given in polar coordinates. The parameter 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.

Parameters:
r - The length of the line segment
theta - The angle at which to draw the line, measured in degrees increasing counterclockwise from the +x axis

endFilledRegion

public void endFilledRegion()
Ends the definition of a region and fills it with the current color.


erasePath

public void erasePath()
Erases the entire path drawn by the pen but does not change its position.


getBounds

public GRectangle getBounds()
Returns the bounding box for the entire figure traced by the pen.

Specified by:
getBounds in class GObject
Returns:
A GRectangle representing the bounding box

getFillColor

public Color getFillColor()
Returns the color used to display the filled region of this object. If none has been set, getFillColor returns the color of the object.

Returns:
The color used to display the filled region of this object

getPenBounds

protected Rectangle getPenBounds()
This method returns the bounds that the pen occupies.


getPenImage

public Image getPenImage()
Returns the image used to draw the pen when setPenVisible has been called.


getSpeed

public double getSpeed()
Returns the current speed of the pen.

Returns:
The current speed of the pen (0 is slowest, 1 is fastest)

hidePen

public void hidePen()
Makes the pen itself invisible.


isPenVisible

public boolean isPenVisible()
Returns whether the pen is visible.

Returns:
true if the pen is visible, otherwise false

paint

public void paint(Graphics g)
Implements the paint operation for this graphical object. This method is not called directly by clients.

Specified by:
paint in class GObject
Parameters:
g - The graphics context into which the painting is done

scale

public void scale(double sx,
                  double sy)
Sets the scale factors 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.

Parameters:
sx - The factor used to scale all coordinates in the x direction
sy - The factor used to scale all coordinates in the y direction

setColor

public void setColor(Color color)
Sets the color of the pen.

Overrides:
setColor in class GObject
Parameters:
color - The new color for the pen

setFillColor

public void setFillColor(Color color)
Sets the color used to fill a region.

Parameters:
color - The new color used to fill a region

setLocation

public void setLocation(double x,
                        double y)
Moves the pen to the point (x, y) without drawing a line.

Overrides:
setLocation in class GObject
Parameters:
x - The x-coordinate of the new position
y - The y-coordinate of the new position

setPenImage

public void setPenImage(Image image)
Sets the image of the pen to be the specified image. This image is drawn with its center at the pen position.

Parameters:
image - The new image to use for the pen

setSpeed

public void setSpeed(double speed)
Sets the speed of the pen, which must be a number between 0 (slowest) and 1 (fastest). Setting speed to a value less than one makes the pen move slowly, thereby making it easy to see exactly how a figure is being drawn.

Parameters:
speed - The speed of the pen (0 is slowest, 1 is fastest)

showPen

public void showPen()
Makes the pen itself visible. If the pen is visible, the drawPen method will be called at the end of painting the path to show the current location.


startFilledRegion

public void startFilledRegion()
Starts defining a region that will be filled with the current color. Any line segments drawn between now and the matching call to endFilledRegion will be used to create a polygon that is then filled.