Class Draw

Object
  extended by Draw
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.util.EventListener

public final class Draw
extends Object
implements java.awt.event.ActionListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.event.KeyListener


Field Summary
static java.awt.Color BLACK
           
static java.awt.Color BLUE
           
static java.awt.Color BOOK_BLUE
          Shade of blue used in Introduction to Programming in Java.
static java.awt.Color BOOK_RED
          Shade of red used in Algorithms 4th edition.
static java.awt.Color CYAN
           
static java.awt.Color DARK_GRAY
           
static java.awt.Color GRAY
           
static java.awt.Color GREEN
           
static java.awt.Color LIGHT_GRAY
           
static java.awt.Color MAGENTA
           
static java.awt.Color ORANGE
           
static java.awt.Color PINK
           
static java.awt.Color RED
           
static java.awt.Color WHITE
           
static java.awt.Color YELLOW
           
 
Constructor Summary
Draw()
          Create an empty drawing object.
Draw(String name)
          Create an empty drawing object with the given name.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          This method cannot be called directly.
 void addListener(DrawListener listener)
          Event-based interactions.
 void arc(double x, double y, double r, double angle1, double angle2)
          Draw an arc of radius r, centered on (x, y), from angle1 to angle2 (in degrees).
 void circle(double x, double y, double r)
          Draw a circle of radius r, centered on (x, y).
 void clear()
          Clear the screen to the default color (white).
 void clear(java.awt.Color color)
          Clear the screen to the given color.
 void ellipse(double x, double y, double semiMajorAxis, double semiMinorAxis)
          Draw an ellipse with given semimajor and semiminor axes, centered on (x, y).
 void filledCircle(double x, double y, double r)
          Draw filled circle of radius r, centered on (x, y).
 void filledEllipse(double x, double y, double semiMajorAxis, double semiMinorAxis)
          Draw an ellipse with given semimajor and semiminor axes, centered on (x, y).
 void filledPolygon(double[] x, double[] y)
          Draw a filled polygon with the given (x[i], y[i]) coordinates.
 void filledRectangle(double x, double y, double halfWidth, double halfHeight)
          Draw a filled rectangle of given half width and half height, centered on (x, y).
 void filledSquare(double x, double y, double r)
          Draw a filled square of side length 2r, centered on (x, y).
 java.awt.Font getFont()
          Get the current font.
 java.awt.Color getPenColor()
          Get the current pen color.
 double getPenRadius()
          Get the current pen radius.
 boolean hasNextKeyTyped()
          Has the user typed a key?
 void keyPressed(java.awt.event.KeyEvent e)
          This method cannot be called directly.
 void keyReleased(java.awt.event.KeyEvent e)
          This method cannot be called directly.
 void keyTyped(java.awt.event.KeyEvent e)
          This method cannot be called directly.
 void line(double x0, double y0, double x1, double y1)
          Draw a line from (x0, y0) to (x1, y1).
static void main(String[] args)
          Test client.
 void mouseClicked(java.awt.event.MouseEvent e)
          This method cannot be called directly.
 void mouseDragged(java.awt.event.MouseEvent e)
          This method cannot be called directly.
 void mouseEntered(java.awt.event.MouseEvent e)
          This method cannot be called directly.
 void mouseExited(java.awt.event.MouseEvent e)
          This method cannot be called directly.
 void mouseMoved(java.awt.event.MouseEvent e)
          This method cannot be called directly.
 boolean mousePressed()
          Is the mouse being pressed?
 void mousePressed(java.awt.event.MouseEvent e)
          This method cannot be called directly.
 void mouseReleased(java.awt.event.MouseEvent e)
          This method cannot be called directly.
 double mouseX()
          What is the x-coordinate of the mouse?
 double mouseY()
          What is the y-coordinate of the mouse?
 char nextKeyTyped()
          What is the next key that was typed by the user?
 void picture(double x, double y, String s)
          Draw picture (gif, jpg, or png) centered on (x, y).
 void picture(double x, double y, String s, double degrees)
          Draw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees
 void picture(double x, double y, String s, double w, double h)
          Draw picture (gif, jpg, or png) centered on (x, y), rescaled to w-by-h.
 void picture(double x, double y, String s, double w, double h, double degrees)
          Draw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees, rescaled to w-by-h.
 void point(double x, double y)
          Draw a point at (x, y).
 void polygon(double[] x, double[] y)
          Draw a polygon with the given (x[i], y[i]) coordinates.
 void rectangle(double x, double y, double halfWidth, double halfHeight)
          Draw a rectangle of given half width and half height, centered on (x, y).
 void save(String filename)
          Save to file - suffix must be png, jpg, or gif.
 void setCanvasSize(int w, int h)
          Set the window size to w-by-h pixels.
 void setFont()
          Set the font to the default font (sans serif, 16 point).
 void setFont(java.awt.Font f)
          Set the font to the given value.
 void setLocationOnScreen(int x, int y)
          Set the upper-left hand corner of the drawing window to be (x, y), where (0, 0) is upper left.
 void setPenColor()
          Set the pen color to the default color (black).
 void setPenColor(java.awt.Color color)
          Set the pen color to the given color.
 void setPenRadius()
          Set the pen size to the default (.002).
 void setPenRadius(double r)
          Set the radius of the pen to the given size.
 void setXscale()
          Set the x-scale to be the default (between 0.0 and 1.0).
 void setXscale(double min, double max)
          Set the x-scale (a 10% border is added to the values)
 void setYscale()
          Set the y-scale to be the default (between 0.0 and 1.0).
 void setYscale(double min, double max)
          Set the y-scale (a 10% border is added to the values).
 void show()
          Display on-screen and turn off animation mode: subsequent calls to drawing methods such as line(), circle(), and square() will be displayed on screen when called.
 void show(int t)
          Display on screen, pause for t milliseconds, and turn on animation mode: subsequent calls to drawing methods such as line(), circle(), and square() will not be displayed on screen until the next call to show().
 void square(double x, double y, double r)
          Draw a square of side length 2r, centered on (x, y).
 void text(double x, double y, String s)
          Write the given text string in the current font, centered on (x, y).
 void text(double x, double y, String s, double degrees)
          Write the given text string in the current font, centered on (x, y) and rotated by the specified number of degrees
 void textLeft(double x, double y, String s)
          Write the given text string in the current font, left-aligned at (x, y).
 void xorOff()
           
 void xorOn()
           
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BLACK

public static final java.awt.Color BLACK

BLUE

public static final java.awt.Color BLUE

CYAN

public static final java.awt.Color CYAN

DARK_GRAY

public static final java.awt.Color DARK_GRAY

GRAY

public static final java.awt.Color GRAY

GREEN

public static final java.awt.Color GREEN

LIGHT_GRAY

public static final java.awt.Color LIGHT_GRAY

MAGENTA

public static final java.awt.Color MAGENTA

ORANGE

public static final java.awt.Color ORANGE

PINK

public static final java.awt.Color PINK

RED

public static final java.awt.Color RED

WHITE

public static final java.awt.Color WHITE

YELLOW

public static final java.awt.Color YELLOW

BOOK_BLUE

public static final java.awt.Color BOOK_BLUE
Shade of blue used in Introduction to Programming in Java. The RGB values are (9, 90, 266).


BOOK_RED

public static final java.awt.Color BOOK_RED
Shade of red used in Algorithms 4th edition. The RGB values are (173, 32, 24).

Constructor Detail

Draw

public Draw(String name)
Create an empty drawing object with the given name.

Parameters:
name - the title of the drawing window.

Draw

public Draw()
Create an empty drawing object.

Method Detail

setLocationOnScreen

public void setLocationOnScreen(int x,
                                int y)
Set the upper-left hand corner of the drawing window to be (x, y), where (0, 0) is upper left.

Parameters:
x - the number of pixels from the left
y - the number of pixels from the top
Throws:
a - RunTimeException if the width or height is 0 or negative

setCanvasSize

public void setCanvasSize(int w,
                          int h)
Set the window size to w-by-h pixels.

Parameters:
w - the width as a number of pixels
h - the height as a number of pixels
Throws:
a - RunTimeException if the width or height is 0 or negative

setXscale

public void setXscale()
Set the x-scale to be the default (between 0.0 and 1.0).


setYscale

public void setYscale()
Set the y-scale to be the default (between 0.0 and 1.0).


setXscale

public void setXscale(double min,
                      double max)
Set the x-scale (a 10% border is added to the values)

Parameters:
min - the minimum value of the x-scale
max - the maximum value of the x-scale

setYscale

public void setYscale(double min,
                      double max)
Set the y-scale (a 10% border is added to the values).

Parameters:
min - the minimum value of the y-scale
max - the maximum value of the y-scale

clear

public void clear()
Clear the screen to the default color (white).


clear

public void clear(java.awt.Color color)
Clear the screen to the given color.

Parameters:
color - the Color to make the background

getPenRadius

public double getPenRadius()
Get the current pen radius.


setPenRadius

public void setPenRadius()
Set the pen size to the default (.002).


setPenRadius

public void setPenRadius(double r)
Set the radius of the pen to the given size.

Parameters:
r - the radius of the pen
Throws:
RuntimeException - if r is negative

getPenColor

public java.awt.Color getPenColor()
Get the current pen color.


setPenColor

public void setPenColor()
Set the pen color to the default color (black).


setPenColor

public void setPenColor(java.awt.Color color)
Set the pen color to the given color.

Parameters:
color - the Color to make the pen

xorOn

public void xorOn()

xorOff

public void xorOff()

getFont

public java.awt.Font getFont()
Get the current font.


setFont

public void setFont()
Set the font to the default font (sans serif, 16 point).


setFont

public void setFont(java.awt.Font f)
Set the font to the given value.

Parameters:
f - the font to make text

line

public void line(double x0,
                 double y0,
                 double x1,
                 double y1)
Draw a line from (x0, y0) to (x1, y1).

Parameters:
x0 - the x-coordinate of the starting point
y0 - the y-coordinate of the starting point
x1 - the x-coordinate of the destination point
y1 - the y-coordinate of the destination point

point

public void point(double x,
                  double y)
Draw a point at (x, y).

Parameters:
x - the x-coordinate of the point
y - the y-coordinate of the point

circle

public void circle(double x,
                   double y,
                   double r)
Draw a circle of radius r, centered on (x, y).

Parameters:
x - the x-coordinate of the center of the circle
y - the y-coordinate of the center of the circle
r - the radius of the circle
Throws:
RuntimeException - if the radius of the circle is negative

filledCircle

public void filledCircle(double x,
                         double y,
                         double r)
Draw filled circle of radius r, centered on (x, y).

Parameters:
x - the x-coordinate of the center of the circle
y - the y-coordinate of the center of the circle
r - the radius of the circle
Throws:
RuntimeException - if the radius of the circle is negative

ellipse

public void ellipse(double x,
                    double y,
                    double semiMajorAxis,
                    double semiMinorAxis)
Draw an ellipse with given semimajor and semiminor axes, centered on (x, y).

Parameters:
x - the x-coordinate of the center of the ellipse
y - the y-coordinate of the center of the ellipse
semiMajorAxis - is the semimajor axis of the ellipse
semiMinorAxis - is the semiminor axis of the ellipse
Throws:
RuntimeException - if either of the axes are negative

filledEllipse

public void filledEllipse(double x,
                          double y,
                          double semiMajorAxis,
                          double semiMinorAxis)
Draw an ellipse with given semimajor and semiminor axes, centered on (x, y).

Parameters:
x - the x-coordinate of the center of the ellipse
y - the y-coordinate of the center of the ellipse
semiMajorAxis - is the semimajor axis of the ellipse
semiMinorAxis - is the semiminor axis of the ellipse
Throws:
RuntimeException - if either of the axes are negative

arc

public void arc(double x,
                double y,
                double r,
                double angle1,
                double angle2)
Draw an arc of radius r, centered on (x, y), from angle1 to angle2 (in degrees).

Parameters:
x - the x-coordinate of the center of the circle
y - the y-coordinate of the center of the circle
r - the radius of the circle
angle1 - the starting angle. 0 would mean an arc beginning at 3 o'clock.
angle2 - the angle at the end of the arc. For example, if you want a 90 degree arc, then angle2 should be angle1 + 90.
Throws:
RuntimeException - if the radius of the circle is negative

square

public void square(double x,
                   double y,
                   double r)
Draw a square of side length 2r, centered on (x, y).

Parameters:
x - the x-coordinate of the center of the square
y - the y-coordinate of the center of the square
r - radius is half the length of any side of the square
Throws:
RuntimeException - if r is negative

filledSquare

public void filledSquare(double x,
                         double y,
                         double r)
Draw a filled square of side length 2r, centered on (x, y).

Parameters:
x - the x-coordinate of the center of the square
y - the y-coordinate of the center of the square
r - radius is half the length of any side of the square
Throws:
RuntimeException - if r is negative

rectangle

public void rectangle(double x,
                      double y,
                      double halfWidth,
                      double halfHeight)
Draw a rectangle of given half width and half height, centered on (x, y).

Parameters:
x - the x-coordinate of the center of the rectangle
y - the y-coordinate of the center of the rectangle
halfWidth - is half the width of the rectangle
halfHeight - is half the height of the rectangle
Throws:
RuntimeException - if halfWidth or halfHeight is negative

filledRectangle

public void filledRectangle(double x,
                            double y,
                            double halfWidth,
                            double halfHeight)
Draw a filled rectangle of given half width and half height, centered on (x, y).

Parameters:
x - the x-coordinate of the center of the rectangle
y - the y-coordinate of the center of the rectangle
halfWidth - is half the width of the rectangle
halfHeight - is half the height of the rectangle
Throws:
RuntimeException - if halfWidth or halfHeight is negative

polygon

public void polygon(double[] x,
                    double[] y)
Draw a polygon with the given (x[i], y[i]) coordinates.

Parameters:
x - an array of all the x-coordindates of the polygon
y - an array of all the y-coordindates of the polygon

filledPolygon

public void filledPolygon(double[] x,
                          double[] y)
Draw a filled polygon with the given (x[i], y[i]) coordinates.

Parameters:
x - an array of all the x-coordindates of the polygon
y - an array of all the y-coordindates of the polygon

picture

public void picture(double x,
                    double y,
                    String s)
Draw picture (gif, jpg, or png) centered on (x, y).

Parameters:
x - the center x-coordinate of the image
y - the center y-coordinate of the image
s - the name of the image/picture, e.g., "ball.gif"
Throws:
RuntimeException - if the image is corrupt

picture

public void picture(double x,
                    double y,
                    String s,
                    double degrees)
Draw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees

Parameters:
x - the center x-coordinate of the image
y - the center y-coordinate of the image
s - the name of the image/picture, e.g., "ball.gif"
degrees - is the number of degrees to rotate counterclockwise
Throws:
RuntimeException - if the image is corrupt

picture

public void picture(double x,
                    double y,
                    String s,
                    double w,
                    double h)
Draw picture (gif, jpg, or png) centered on (x, y), rescaled to w-by-h.

Parameters:
x - the center x coordinate of the image
y - the center y coordinate of the image
s - the name of the image/picture, e.g., "ball.gif"
w - the width of the image
h - the height of the image
Throws:
RuntimeException - if the image is corrupt

picture

public void picture(double x,
                    double y,
                    String s,
                    double w,
                    double h,
                    double degrees)
Draw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees, rescaled to w-by-h.

Parameters:
x - the center x-coordinate of the image
y - the center y-coordinate of the image
s - the name of the image/picture, e.g., "ball.gif"
w - the width of the image
h - the height of the image
degrees - is the number of degrees to rotate counterclockwise
Throws:
RuntimeException - if the image is corrupt

text

public void text(double x,
                 double y,
                 String s)
Write the given text string in the current font, centered on (x, y).

Parameters:
x - the center x-coordinate of the text
y - the center y-coordinate of the text
s - the text

text

public void text(double x,
                 double y,
                 String s,
                 double degrees)
Write the given text string in the current font, centered on (x, y) and rotated by the specified number of degrees

Parameters:
x - the center x-coordinate of the text
y - the center y-coordinate of the text
s - the text
degrees - is the number of degrees to rotate counterclockwise

textLeft

public void textLeft(double x,
                     double y,
                     String s)
Write the given text string in the current font, left-aligned at (x, y).

Parameters:
x - the x-coordinate of the text
y - the y-coordinate of the text
s - the text

show

public void show(int t)
Display on screen, pause for t milliseconds, and turn on animation mode: subsequent calls to drawing methods such as line(), circle(), and square() will not be displayed on screen until the next call to show(). This is useful for producing animations (clear the screen, draw a bunch of shapes, display on screen for a fixed amount of time, and repeat). It also speeds up drawing a huge number of shapes (call show(0) to defer drawing on screen, draw the shapes, and call show(0) to display them all on screen at once).

Parameters:
t - number of milliseconds

show

public void show()
Display on-screen and turn off animation mode: subsequent calls to drawing methods such as line(), circle(), and square() will be displayed on screen when called. This is the default.


save

public void save(String filename)
Save to file - suffix must be png, jpg, or gif.

Parameters:
filename - the name of the file with one of the required suffixes

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
This method cannot be called directly.

Specified by:
actionPerformed in interface java.awt.event.ActionListener

addListener

public void addListener(DrawListener listener)
Event-based interactions.


mousePressed

public boolean mousePressed()
Is the mouse being pressed?

Returns:
true or false

mouseX

public double mouseX()
What is the x-coordinate of the mouse?

Returns:
the value of the x-coordinate of the mouse

mouseY

public double mouseY()
What is the y-coordinate of the mouse?

Returns:
the value of the y-coordinate of the mouse

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
This method cannot be called directly.

Specified by:
mouseClicked in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
This method cannot be called directly.

Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
This method cannot be called directly.

Specified by:
mouseExited in interface java.awt.event.MouseListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
This method cannot be called directly.

Specified by:
mousePressed in interface java.awt.event.MouseListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
This method cannot be called directly.

Specified by:
mouseReleased in interface java.awt.event.MouseListener

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
This method cannot be called directly.

Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
This method cannot be called directly.

Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener

hasNextKeyTyped

public boolean hasNextKeyTyped()
Has the user typed a key?

Returns:
true if the user has typed a key, false otherwise

nextKeyTyped

public char nextKeyTyped()
What is the next key that was typed by the user?

Returns:
the next key typed

keyTyped

public void keyTyped(java.awt.event.KeyEvent e)
This method cannot be called directly.

Specified by:
keyTyped in interface java.awt.event.KeyListener

keyPressed

public void keyPressed(java.awt.event.KeyEvent e)
This method cannot be called directly.

Specified by:
keyPressed in interface java.awt.event.KeyListener

keyReleased

public void keyReleased(java.awt.event.KeyEvent e)
This method cannot be called directly.

Specified by:
keyReleased in interface java.awt.event.KeyListener

main

public static void main(String[] args)
Test client.