public final class StdDraw
extends java.lang.Object
implements java.awt.event.ActionListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.event.KeyListener
For additional documentation, see Section 1.5 of Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.
| Modifier and Type | Field and Description |
|---|---|
static java.awt.Color |
BLACK
The color black.
|
static java.awt.Color |
BLUE
The color blue.
|
static java.awt.Color |
BOOK_BLUE
Shade of blue used in Introduction to Programming in Java.
|
static java.awt.Color |
BOOK_LIGHT_BLUE
Shade of light 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
The color cyan.
|
static java.awt.Color |
DARK_GRAY
The color dark gray.
|
static java.awt.Color |
GRAY
The color gray.
|
static java.awt.Color |
GREEN
The color green.
|
static java.awt.Color |
LIGHT_GRAY
The color light gray.
|
static java.awt.Color |
MAGENTA
The color magenta.
|
static java.awt.Color |
ORANGE
The color orange.
|
static java.awt.Color |
PINK
The color pink.
|
static java.awt.Color |
RED
The color red.
|
static java.awt.Color |
WHITE
The color white.
|
static java.awt.Color |
YELLOW
The color yellow.
|
| Modifier and Type | Method and Description |
|---|---|
void |
actionPerformed(java.awt.event.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(java.awt.Color color)
Clears the screen to the specified 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 java.awt.Font |
getFont()
Gets the current font.
|
static java.awt.Color |
getPenColor()
Gets the current pen color.
|
static double |
getPenRadius()
Gets the current pen radius.
|
static boolean |
hasNextKeyTyped()
Returns true if the user has typed a key.
|
static boolean |
isKeyPressed(int keycode)
Returns true if the given key is being pressed.
|
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.
|
static void |
line(double x0,
double y0,
double x1,
double y1)
Draw a line from (x0, y0) to (x1, y1).
|
static void |
main(java.lang.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.
|
static boolean |
mousePressed()
Returns true if the mouse is 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.
|
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,
java.lang.String s)
Draw picture (gif, jpg, or png) centered on (x, y).
|
static void |
picture(double x,
double y,
java.lang.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,
java.lang.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,
java.lang.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(java.lang.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(java.awt.Font f)
Sets the font to the specified value.
|
static void |
setPenColor()
Set the pen color to the default color (black).
|
static void |
setPenColor(java.awt.Color color)
Sets the pen color to the specified color.
|
static void |
setPenColor(int red,
int green,
int blue)
Sets the pen color to the specified RGB color.
|
static void |
setPenRadius()
Sets the pen size to the default size (.002).
|
static void |
setPenRadius(double r)
Sets the radius of the pen to the specified 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,
java.lang.String s)
Write the given text string in the current font, centered on (x, y).
|
static void |
text(double x,
double y,
java.lang.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,
java.lang.String s)
Write the given text string in the current font, left-aligned at (x, y).
|
static void |
textRight(double x,
double y,
java.lang.String s)
Write the given text string in the current font, right-aligned at (x, y).
|
public static final java.awt.Color BLACK
public static final java.awt.Color BLUE
public static final java.awt.Color CYAN
public static final java.awt.Color DARK_GRAY
public static final java.awt.Color GRAY
public static final java.awt.Color GREEN
public static final java.awt.Color LIGHT_GRAY
public static final java.awt.Color MAGENTA
public static final java.awt.Color ORANGE
public static final java.awt.Color PINK
public static final java.awt.Color RED
public static final java.awt.Color WHITE
public static final java.awt.Color YELLOW
public static final java.awt.Color BOOK_BLUE
public static final java.awt.Color BOOK_LIGHT_BLUE
public static final java.awt.Color BOOK_RED
public static void setCanvasSize()
public static void setCanvasSize(int w,
int h)
w - the width as a number of pixelsh - the height as a number of pixelsjava.lang.IllegalArgumentException - if the width or height is 0 or negativepublic static void setXscale()
public static void setYscale()
public static void setXscale(double min,
double max)
min - the minimum value of the x-scalemax - the maximum value of the x-scalepublic static void setYscale(double min,
double max)
min - the minimum value of the y-scalemax - the maximum value of the y-scalepublic static void setScale(double min,
double max)
min - the minimum value of the x- and y-scalesmax - the maximum value of the x- and y-scalespublic static void clear()
public static void clear(java.awt.Color color)
color - the color to make the backgroundpublic static double getPenRadius()
public static void setPenRadius()
public static void setPenRadius(double r)
r - the radius of the penjava.lang.IllegalArgumentException - if r is negativepublic static java.awt.Color getPenColor()
public static void setPenColor()
public static void setPenColor(java.awt.Color 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.
color - the Color to make the penpublic static void setPenColor(int red,
int green,
int blue)
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)java.lang.IllegalArgumentException - if the amount of red, green, or blue are outside prescribed rangepublic static java.awt.Font getFont()
public static void setFont()
public static void setFont(java.awt.Font f)
f - the fontpublic static void line(double x0,
double y0,
double x1,
double y1)
x0 - the x-coordinate of the starting pointy0 - the y-coordinate of the starting pointx1 - the x-coordinate of the destination pointy1 - the y-coordinate of the destination pointpublic static void point(double x,
double y)
x - the x-coordinate of the pointy - the y-coordinate of the pointpublic static void circle(double x,
double y,
double r)
x - the x-coordinate of the center of the circley - the y-coordinate of the center of the circler - the radius of the circlejava.lang.IllegalArgumentException - if the radius of the circle is negativepublic static void filledCircle(double x,
double y,
double r)
x - the x-coordinate of the center of the circley - the y-coordinate of the center of the circler - the radius of the circlejava.lang.IllegalArgumentException - if the radius of the circle is negativepublic static void ellipse(double x,
double y,
double semiMajorAxis,
double semiMinorAxis)
x - the x-coordinate of the center of the ellipsey - the y-coordinate of the center of the ellipsesemiMajorAxis - is the semimajor axis of the ellipsesemiMinorAxis - is the semiminor axis of the ellipsejava.lang.IllegalArgumentException - if either of the axes are negativepublic static void filledEllipse(double x,
double y,
double semiMajorAxis,
double semiMinorAxis)
x - the x-coordinate of the center of the ellipsey - the y-coordinate of the center of the ellipsesemiMajorAxis - is the semimajor axis of the ellipsesemiMinorAxis - is the semiminor axis of the ellipsejava.lang.IllegalArgumentException - if either of the axes are negativepublic static void arc(double x,
double y,
double r,
double angle1,
double angle2)
x - the x-coordinate of the center of the circley - the y-coordinate of the center of the circler - the radius of the circleangle1 - 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.java.lang.IllegalArgumentException - if the radius of the circle is negativepublic static void square(double x,
double y,
double r)
x - the x-coordinate of the center of the squarey - the y-coordinate of the center of the squarer - radius is half the length of any side of the squarejava.lang.IllegalArgumentException - if r is negativepublic static void filledSquare(double x,
double y,
double r)
x - the x-coordinate of the center of the squarey - the y-coordinate of the center of the squarer - radius is half the length of any side of the squarejava.lang.IllegalArgumentException - if r is negativepublic static void rectangle(double x,
double y,
double halfWidth,
double halfHeight)
x - the x-coordinate of the center of the rectangley - the y-coordinate of the center of the rectanglehalfWidth - is half the width of the rectanglehalfHeight - is half the height of the rectanglejava.lang.IllegalArgumentException - if halfWidth or halfHeight is negativepublic static void filledRectangle(double x,
double y,
double halfWidth,
double halfHeight)
x - the x-coordinate of the center of the rectangley - the y-coordinate of the center of the rectanglehalfWidth - is half the width of the rectanglehalfHeight - is half the height of the rectanglejava.lang.IllegalArgumentException - if halfWidth or halfHeight is negativepublic static void polygon(double[] x,
double[] y)
x - an array of all the x-coordinates of the polygony - an array of all the y-coordinates of the polygonpublic static void filledPolygon(double[] x,
double[] y)
x - an array of all the x-coordinates of the polygony - an array of all the y-coordinates of the polygonpublic static void picture(double x,
double y,
java.lang.String s)
x - the center x-coordinate of the imagey - the center y-coordinate of the images - the name of the image/picture, e.g., "ball.gif"java.lang.IllegalArgumentException - if the image is corruptpublic static void picture(double x,
double y,
java.lang.String s,
double degrees)
x - the center x-coordinate of the imagey - the center y-coordinate of the images - the name of the image/picture, e.g., "ball.gif"degrees - is the number of degrees to rotate counterclockwisejava.lang.IllegalArgumentException - if the image is corruptpublic static void picture(double x,
double y,
java.lang.String s,
double w,
double h)
x - the center x coordinate of the imagey - the center y coordinate of the images - the name of the image/picture, e.g., "ball.gif"w - the width of the imageh - the height of the imagejava.lang.IllegalArgumentException - if the width height are negativejava.lang.IllegalArgumentException - if the image is corruptpublic static void picture(double x,
double y,
java.lang.String s,
double w,
double h,
double degrees)
x - the center x-coordinate of the imagey - the center y-coordinate of the images - the name of the image/picture, e.g., "ball.gif"w - the width of the imageh - the height of the imagedegrees - is the number of degrees to rotate counterclockwisejava.lang.IllegalArgumentException - if the image is corruptpublic static void text(double x,
double y,
java.lang.String s)
x - the center x-coordinate of the texty - the center y-coordinate of the texts - the textpublic static void text(double x,
double y,
java.lang.String s,
double degrees)
x - the center x-coordinate of the texty - the center y-coordinate of the texts - the textdegrees - is the number of degrees to rotate counterclockwisepublic static void textLeft(double x,
double y,
java.lang.String s)
x - the x-coordinate of the texty - the y-coordinate of the texts - the textpublic static void textRight(double x,
double y,
java.lang.String s)
x - the x-coordinate of the texty - the y-coordinate of the texts - the textpublic static void show(int t)
t - number of millisecondspublic static void show()
public static void save(java.lang.String filename)
filename - the name of the file with one of the required suffixespublic void actionPerformed(java.awt.event.ActionEvent e)
actionPerformed in interface java.awt.event.ActionListenerpublic static boolean mousePressed()
public static double mouseX()
public static double mouseY()
public void mouseClicked(java.awt.event.MouseEvent e)
mouseClicked in interface java.awt.event.MouseListenerpublic void mouseEntered(java.awt.event.MouseEvent e)
mouseEntered in interface java.awt.event.MouseListenerpublic void mouseExited(java.awt.event.MouseEvent e)
mouseExited in interface java.awt.event.MouseListenerpublic void mousePressed(java.awt.event.MouseEvent e)
mousePressed in interface java.awt.event.MouseListenerpublic void mouseReleased(java.awt.event.MouseEvent e)
mouseReleased in interface java.awt.event.MouseListenerpublic void mouseDragged(java.awt.event.MouseEvent e)
mouseDragged in interface java.awt.event.MouseMotionListenerpublic void mouseMoved(java.awt.event.MouseEvent e)
mouseMoved in interface java.awt.event.MouseMotionListenerpublic static boolean hasNextKeyTyped()
public static char nextKeyTyped()
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).
public static boolean isKeyPressed(int keycode)
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.
keycode - the key to check if it is being pressedpublic void keyTyped(java.awt.event.KeyEvent e)
keyTyped in interface java.awt.event.KeyListenerpublic void keyPressed(java.awt.event.KeyEvent e)
keyPressed in interface java.awt.event.KeyListenerpublic void keyReleased(java.awt.event.KeyEvent e)
keyReleased in interface java.awt.event.KeyListenerpublic static void main(java.lang.String[] args)