Class StdDraw

Object
  extended by StdDraw
All Implemented Interfaces:
ActionListener, KeyListener, MouseListener, MouseMotionListener, EventListener

public final class StdDraw
extends Object
implements ActionListener, MouseListener, MouseMotionListener, KeyListener

Standard draw. This class provides a basic capability for creating drawings with your programs. It uses a simple graphics model that allows you to create drawings consisting of points, lines, and curves in a window on your computer and to save the drawings to a file.

For additional documentation, see Section 1.5 of Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.

Author:
Robert Sedgewick, Kevin Wayne

Field Summary
static Color BLACK
          The color black.
static Color BLUE
          The color blue.
static Color BOOK_BLUE
          Shade of blue used in Introduction to Programming in Java.
static Color BOOK_LIGHT_BLUE
          Shade of light blue used in Introduction to Programming in Java.
static Color BOOK_RED
          Shade of red used in Algorithms 4th edition.
static Color CYAN
          The color cyan.
static Color DARK_GRAY
          The color dark gray.
static Color GRAY
          The color gray.
static Color GREEN
          The color green.
static Color LIGHT_GRAY
          The color light gray.
static Color MAGENTA
          The color magenta.
static Color ORANGE
          The color orange.
static Color PINK
          The color pink.
static Color RED
          The color red.
static Color WHITE
          The color white.
static Color YELLOW
          The color yellow.
 
Method Summary
 void actionPerformed(ActionEvent e)
          This method cannot be called directly.
static 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).
static void circle(double x, double y, double r)
          Draw a circle of radius r, centered on (x, y).
static void clear()
          Clears the screen to the default color (white).
static void clear(Color color)
          Clears the screen to the given color.
static void ellipse(double x, double y, double semiMajorAxis, double semiMinorAxis)
          Draw an ellipse with given semimajor and semiminor axes, centered on (x, y).
static void filledCircle(double x, double y, double r)
          Draw filled circle of radius r, centered on (x, y).
static void filledEllipse(double x, double y, double semiMajorAxis, double semiMinorAxis)
          Draw an ellipse with given semimajor and semiminor axes, centered on (x, y).
static void filledPolygon(double[] x, double[] y)
          Draw a filled polygon with the given (x[i], y[i]) coordinates.
static 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).
static void filledSquare(double x, double y, double r)
          Draw a filled square of side length 2r, centered on (x, y).
static Font getFont()
          Gets the current font.
static Color getPenColor()
          Gets the current pen color.
static double getPenRadius()
          Gets the current pen radius.
static boolean hasNextKeyTyped()
          Has the user typed a key?
static boolean isKeyPressed(int keycode)
          Is the given key currently being pressed?
 void keyPressed(KeyEvent e)
          This method cannot be called directly.
 void keyReleased(KeyEvent e)
          This method cannot be called directly.
 void keyTyped(KeyEvent e)
          This method cannot be called directly.
static 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(MouseEvent e)
          This method cannot be called directly.
 void mouseDragged(MouseEvent e)
          This method cannot be called directly.
 void mouseEntered(MouseEvent e)
          This method cannot be called directly.
 void mouseExited(MouseEvent e)
          This method cannot be called directly.
 void mouseMoved(MouseEvent e)
          This method cannot be called directly.
static boolean mousePressed()
          Is the mouse being pressed?
 void mousePressed(MouseEvent e)
          This method cannot be called directly.
 void mouseReleased(MouseEvent e)
          This method cannot be called directly.
static double mouseX()
          Returns the x-coordinate of the mouse.
static double mouseY()
          Returns the y-coordinate of the mouse.
static char nextKeyTyped()
          The next key that was typed by the user.
static void picture(double x, double y, String s)
          Draw picture (gif, jpg, or png) centered on (x, y).
static 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.
static 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.
static 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.
static void point(double x, double y)
          Draw a point at (x, y).
static void polygon(double[] x, double[] y)
          Draw a polygon with the given (x[i], y[i]) coordinates.
static void rectangle(double x, double y, double halfWidth, double halfHeight)
          Draw a rectangle of given half width and half height, centered on (x, y).
static void save(String filename)
          Save onscreen image to file - suffix must be png, jpg, or gif.
static void setCanvasSize()
          Sets the window size to the default size 512-by-512 pixels.
static void setCanvasSize(int w, int h)
          Sets the window size to w-by-h pixels.
static void setFont()
          Sets the font to the default font (sans serif, 16 point).
static void setFont(Font f)
          Sets the font to the given value.
static void setPenColor()
          Set the pen color to the default color (black).
static void setPenColor(Color color)
          Sets the pen color to the given color.
static void setPenColor(int red, int green, int blue)
          Sets the pen color to the given RGB color.
static void setPenRadius()
          Sets the pen size to the default (.002).
static void setPenRadius(double r)
          Sets the radius of the pen to the given size.
static void setScale(double min, double max)
          Sets both the x-scale and y-scale.
static void setXscale()
          Sets the x-scale to be the default (between 0.0 and 1.0).
static void setXscale(double min, double max)
          Sets the x-scale.
static void setYscale()
          Sets the y-scale to be the default (between 0.0 and 1.0).
static void setYscale(double min, double max)
          Sets the y-scale.
static 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.
static 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().
static void square(double x, double y, double r)
          Draw a square of side length 2r, centered on (x, y).
static void text(double x, double y, String s)
          Write the given text string in the current font, centered on (x, y).
static 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.
static void textLeft(double x, double y, String s)
          Write the given text string in the current font, left-aligned at (x, y).
static void textRight(double x, double y, String s)
          Write the given text string in the current font, right-aligned at (x, y).
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BLACK

public static final Color BLACK
The color black.


BLUE

public static final Color BLUE
The color blue.


CYAN

public static final Color CYAN
The color cyan.


DARK_GRAY

public static final Color DARK_GRAY
The color dark gray.


GRAY

public static final Color GRAY
The color gray.


GREEN

public static final Color GREEN
The color green.


LIGHT_GRAY

public static final Color LIGHT_GRAY
The color light gray.


MAGENTA

public static final Color MAGENTA
The color magenta.


ORANGE

public static final Color ORANGE
The color orange.


PINK

public static final Color PINK
The color pink.


RED

public static final Color RED
The color red.


WHITE

public static final Color WHITE
The color white.


YELLOW

public static final Color YELLOW
The color yellow.


BOOK_BLUE

public static final Color BOOK_BLUE
Shade of blue used in Introduction to Programming in Java. It is Pantone 300U. The RGB values are approximately (9, 90, 166).


BOOK_LIGHT_BLUE

public static final Color BOOK_LIGHT_BLUE
Shade of light blue used in Introduction to Programming in Java. The RGB values are approximately (103, 198, 243).


BOOK_RED

public static final Color BOOK_RED
Shade of red used in Algorithms 4th edition. It is Pantone 1805U. The RGB values are approximately (150, 35, 31).

Method Detail

setCanvasSize

public static void setCanvasSize()
Sets the window size to the default size 512-by-512 pixels. This method must be called before any other commands.


setCanvasSize

public static void setCanvasSize(int w,
                                 int h)
Sets the window size to w-by-h pixels. This method must be called before any other commands.

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

setXscale

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


setYscale

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


setXscale

public static void setXscale(double min,
                             double max)
Sets the x-scale.

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

setYscale

public static void setYscale(double min,
                             double max)
Sets the y-scale.

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

setScale

public static void setScale(double min,
                            double max)
Sets both the x-scale and y-scale.

Parameters:
min - the minimum value of the x- and y-scales
max - the maximum value of the x- and y-scales

clear

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


clear

public static void clear(Color color)
Clears the screen to the given color.

Parameters:
color - the color to make the background

getPenRadius

public static double getPenRadius()
Gets the current pen radius.

Returns:
the current value of the pen radius

setPenRadius

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


setPenRadius

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

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

getPenColor

public static Color getPenColor()
Gets the current pen color.

Returns:
the current pen color

setPenColor

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


setPenColor

public static void setPenColor(Color color)
Sets the pen color to the given color.

The predefined pen colors are StdDraw.BLACK, StdDraw.BLUE, StdDraw.CYAN, StdDraw.DARK_GRAY, StdDraw.GRAY, StdDraw.GREEN, StdDraw.LIGHT_GRAY, StdDraw.MAGENTA, StdDraw.ORANGE, StdDraw.PINK, StdDraw.RED, StdDraw.WHITE, and StdDraw.YELLOW.

Parameters:
color - the Color to make the pen

setPenColor

public static void setPenColor(int red,
                               int green,
                               int blue)
Sets the pen color to the given RGB color.

Parameters:
red - the amount of red (between 0 and 255)
green - the amount of green (between 0 and 255)
blue - the amount of blue (between 0 and 255)
Throws:
IllegalArgumentException - if the amount of red, green, or blue are outside prescribed range

getFont

public static Font getFont()
Gets the current font.

Returns:
the current font

setFont

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


setFont

public static void setFont(Font f)
Sets the font to the given value.

Parameters:
f - the font

line

public static 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 static 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 static 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:
IllegalArgumentException - if the radius of the circle is negative

filledCircle

public static 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:
IllegalArgumentException - if the radius of the circle is negative

ellipse

public static 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:
IllegalArgumentException - if either of the axes are negative

filledEllipse

public static 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:
IllegalArgumentException - if either of the axes are negative

arc

public static 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:
IllegalArgumentException - if the radius of the circle is negative

square

public static 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:
IllegalArgumentException - if r is negative

filledSquare

public static 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:
IllegalArgumentException - if r is negative

rectangle

public static 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:
IllegalArgumentException - if halfWidth or halfHeight is negative

filledRectangle

public static 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:
IllegalArgumentException - if halfWidth or halfHeight is negative

polygon

public static 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-coordinates of the polygon
y - an array of all the y-coordinates of the polygon

filledPolygon

public static 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-coordinates of the polygon
y - an array of all the y-coordinates of the polygon

picture

public static 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:
IllegalArgumentException - if the image is corrupt

picture

public static 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:
IllegalArgumentException - if the image is corrupt

picture

public static 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:
IllegalArgumentException - if the width height are negative
IllegalArgumentException - if the image is corrupt

picture

public static 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:
IllegalArgumentException - if the image is corrupt

text

public static 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 static 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 static 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

textRight

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

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

show

public static 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 static 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 static void save(String filename)
Save onscreen image 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(ActionEvent e)
This method cannot be called directly.

Specified by:
actionPerformed in interface ActionListener

mousePressed

public static boolean mousePressed()
Is the mouse being pressed?

Returns:
true if the mouse is being pressed; false otherwise

mouseX

public static double mouseX()
Returns the x-coordinate of the mouse.

Returns:
the x-coordinate of the mouse

mouseY

public static double mouseY()
Returns the y-coordinate of the mouse.

Returns:
y-coordinate of the mouse

mouseClicked

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

Specified by:
mouseClicked in interface MouseListener

mouseEntered

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

Specified by:
mouseEntered in interface MouseListener

mouseExited

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

Specified by:
mouseExited in interface MouseListener

mousePressed

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

Specified by:
mousePressed in interface MouseListener

mouseReleased

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

Specified by:
mouseReleased in interface MouseListener

mouseDragged

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

Specified by:
mouseDragged in interface MouseMotionListener

mouseMoved

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

Specified by:
mouseMoved in interface MouseMotionListener

hasNextKeyTyped

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

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

nextKeyTyped

public static char nextKeyTyped()
The next key that was typed by the user.

This method returns a Unicode character corresponding to the key typed (such as 'a' or 'A'). It cannot identify action keys (such as F1 and arrow keys) or modifier keys (such as control).

Returns:
the next key typed by the user

isKeyPressed

public static boolean isKeyPressed(int keycode)
Is the given key currently being pressed?

This method takes the keycode (corresponding to a physical key) as an argument. It can handle action keys (such as F1 and arrow keys) and modifier keys (such as shift and control). See KeyEvent for a description of key codes.

Parameters:
keycode - the key to check if it is being pressed
Returns:
true if keycode is currently being pressed; false otherwise

keyTyped

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

Specified by:
keyTyped in interface KeyListener

keyPressed

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

Specified by:
keyPressed in interface KeyListener

keyReleased

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

Specified by:
keyReleased in interface KeyListener

main

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