Package stdlib
Class StdDraw
java.lang.Object
stdlib.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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Colorstatic final Colorstatic final ColorShade of blue used in Introduction to Programming in Java.static final Colorstatic final ColorShade of red used in Algorithms 4th edition.private static final doublestatic final Colorstatic final Colorprivate static final Colorprivate static final Fontprivate static final Colorprivate static final doubleprivate static final intprivate static final doubleprivate static final doubleprivate static final doubleprivate static final doubleprivate static booleanprivate static Fontprivate static JFramestatic final Colorstatic final Colorprivate static intprivate static Objectprivate static LinkedList<Character> static final Colorstatic final Colorprivate static Objectprivate static booleanprivate static doubleprivate static doubleprivate static Graphics2Dprivate static BufferedImageprivate static Graphics2Dprivate static BufferedImagestatic final Colorprivate static Colorprivate static doublestatic final Colorstatic final Colorprivate static StdDrawstatic final Colorprivate static intprivate static doubleprivate static doublestatic final Colorprivate static doubleprivate static double -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidThis method cannot be called directly.static voidarc(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 voidcircle(double x, double y, double r) Draw a circle of radius r, centered on (x, y).static voidclear()Clear the screen to the default color (white).static voidClear the screen to the given color.private static JMenuBarprivate static voiddraw()static voidellipse(double x, double y, double semiMajorAxis, double semiMinorAxis) Draw an ellipse with given semimajor and semiminor axes, centered on (x, y).private static doublefactorX(double w) private static doublefactorY(double h) static voidfilledCircle(double x, double y, double r) Draw filled circle of radius r, centered on (x, y).static voidfilledEllipse(double x, double y, double semiMajorAxis, double semiMinorAxis) Draw an ellipse with given semimajor and semiminor axes, centered on (x, y).static voidfilledPolygon(double[] x, double[] y) Draw a filled polygon with the given (x[i], y[i]) coordinates.static voidfilledRectangle(double x, double y, double halfWidth, double halfHeight) Draw a filled rectangle of given half width and half height, centered on (x, y).static voidfilledSquare(double x, double y, double r) Draw a filled square of side length 2r, centered on (x, y).static FontgetFont()Get the current font.private static Imagestatic ColorGet the current pen color.static doubleGet the current pen radius.static booleanHas the user typed a key?private static voidinit()static booleanisKeyPressed(int keycode) Is the keycode currently being pressed? This method takes as an argument the keycode (corresponding to a physical key).voidThis method cannot be called directly.voidThis method cannot be called directly.voidThis method cannot be called directly.static voidline(double x0, double y0, double x1, double y1) Draw a line from (x0, y0) to (x1, y1).static voidTest client.voidThis method cannot be called directly.voidThis method cannot be called directly.voidThis method cannot be called directly.voidThis method cannot be called directly.voidThis method cannot be called directly.static booleanIs the mouse being pressed?voidThis method cannot be called directly.voidThis method cannot be called directly.static doublemouseX()What is the x-coordinate of the mouse?static doublemouseY()What is the y-coordinate of the mouse?static charWhat is 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').static voidDraw picture (gif, jpg, or png) centered on (x, y).static voidDraw picture (gif, jpg, or png) centered on (x, y), rotated given number of degreesstatic voidDraw picture (gif, jpg, or png) centered on (x, y), rescaled to w-by-h.static voidDraw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees, rescaled to w-by-h.private static voidpixel(double x, double y) Draw one pixel at (x, y).static voidpoint(double x, double y) Draw a point at (x, y).static voidpolygon(double[] x, double[] y) Draw a polygon with the given (x[i], y[i]) coordinates.static voidrectangle(double x, double y, double halfWidth, double halfHeight) Draw a rectangle of given half width and half height, centered on (x, y).static voidSave onscreen image to file - suffix must be png, jpg, or gif.private static doublescaleX(double x) private static doublescaleY(double y) static voidSet the window size to the default size 512-by-512 pixels.static voidsetCanvasSize(int w, int h) Set the window size to w-by-h pixels.static voidsetFont()Set the font to the default font (sans serif, 16 point).static voidSet the font to the given value.static voidSet the pen color to the default color (black).static voidsetPenColor(Color color) Set the pen color to the given color.static voidSet the pen size to the default (.002).static voidsetPenRadius(double r) Set the radius of the pen to the given size.static voidsetScale(double min, double max) Set the x-scale and y-scale (a 10% border is added to the values)static voidSet the x-scale to be the default (between 0.0 and 1.0).static voidsetXscale(double min, double max) Set the x-scale (a 10% border is added to the values)static voidSet the y-scale to be the default (between 0.0 and 1.0).static voidsetYscale(double min, double max) Set the y-scale (a 10% border is added to the values).static voidshow()Display on-screen and turn off animation mode: subsequent calls to drawing methods such asline(),circle(), andsquare()will be displayed on screen when called.static voidshow(int t) Display on screen, pause for t milliseconds, and turn on animation mode: subsequent calls to drawing methods such asline(),circle(), andsquare()will not be displayed on screen until the next call toshow().static voidsquare(double x, double y, double r) Draw a square of side length 2r, centered on (x, y).static voidWrite the given text string in the current font, centered on (x, y).static voidWrite the given text string in the current font, centered on (x, y) and rotated by the specified number of degreesstatic voidWrite the given text string in the current font, left-aligned at (x, y).static voidWrite the given text string in the current font, right-aligned at (x, y).private static doubleuserX(double x) private static doubleuserY(double y)
-
Field Details
-
BLACK
-
BLUE
-
CYAN
-
DARK_GRAY
-
GRAY
-
GREEN
-
LIGHT_GRAY
-
MAGENTA
-
ORANGE
-
PINK
-
RED
-
WHITE
-
YELLOW
-
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
-
BOOK_RED
Shade of red used in Algorithms 4th edition. It is Pantone 1805U. The RGB values are approximately (150, 35, 31). -
DEFAULT_PEN_COLOR
-
DEFAULT_CLEAR_COLOR
-
penColor
-
DEFAULT_SIZE
- See Also:
-
width
-
height
-
DEFAULT_PEN_RADIUS
- See Also:
-
penRadius
-
defer
-
BORDER
- See Also:
-
DEFAULT_XMIN
- See Also:
-
DEFAULT_XMAX
- See Also:
-
DEFAULT_YMIN
- See Also:
-
DEFAULT_YMAX
- See Also:
-
xmin
-
ymin
-
xmax
-
ymax
-
mouseLock
-
keyLock
-
DEFAULT_FONT
-
font
-
offscreenImage
-
onscreenImage
-
offscreen
-
onscreen
-
std
-
frame
-
mousePressed
-
mouseX
-
mouseY
-
keysTyped
-
keysDown
-
-
Constructor Details
-
StdDraw
private StdDraw()
-
-
Method Details
-
setCanvasSize
Set the window size to the default size 512-by-512 pixels. -
setCanvasSize
Set the window size to w-by-h pixels.- Parameters:
w- the width as a number of pixelsh- the height as a number of pixels- Throws:
RuntimeException- if the width or height is 0 or negative
-
init
-
createMenuBar
-
setXscale
Set the x-scale to be the default (between 0.0 and 1.0). -
setYscale
Set the y-scale to be the default (between 0.0 and 1.0). -
setXscale
Set the x-scale (a 10% border is added to the values)- Parameters:
min- the minimum value of the x-scalemax- the maximum value of the x-scale
-
setYscale
Set the y-scale (a 10% border is added to the values).- Parameters:
min- the minimum value of the y-scalemax- the maximum value of the y-scale
-
setScale
Set the x-scale and y-scale (a 10% border is added to the values)- Parameters:
min- the minimum value of the x- and y-scalesmax- the maximum value of the x- and y-scales
-
scaleX
-
scaleY
-
factorX
-
factorY
-
userX
-
userY
-
clear
Clear the screen to the default color (white). -
clear
Clear the screen to the given color.- Parameters:
color- the Color to make the background
-
getPenRadius
Get the current pen radius. -
setPenRadius
Set the pen size to the default (.002). -
setPenRadius
Set the radius of the pen to the given size.- Parameters:
r- the radius of the pen- Throws:
RuntimeException- if r is negative
-
getPenColor
Get the current pen color. -
setPenColor
Set the pen color to the default color (black). -
setPenColor
Set the pen color to the given color. The available pen colors are BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, and YELLOW.- Parameters:
color- the Color to make the pen
-
getFont
Get the current font. -
setFont
Set the font to the default font (sans serif, 16 point). -
setFont
Set the font to the given value.- Parameters:
f- the font to make text
-
line
Draw a line from (x0, y0) to (x1, y1).- Parameters:
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 point
-
pixel
Draw one pixel at (x, y).- Parameters:
x- the x-coordinate of the pixely- the y-coordinate of the pixel
-
point
Draw a point at (x, y).- Parameters:
x- the x-coordinate of the pointy- the y-coordinate of the point
-
circle
Draw a circle of radius r, centered on (x, y).- Parameters:
x- the x-coordinate of the center of the circley- the y-coordinate of the center of the circler- the radius of the circle- Throws:
RuntimeException- if the radius of the circle is negative
-
filledCircle
Draw filled circle of radius r, centered on (x, y).- Parameters:
x- the x-coordinate of the center of the circley- the y-coordinate of the center of the circler- the radius of the circle- Throws:
RuntimeException- if the radius of the circle is negative
-
ellipse
Draw an ellipse with given semimajor and semiminor axes, centered on (x, y).- Parameters:
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 ellipse- Throws:
RuntimeException- if either of the axes are negative
-
filledEllipse
Draw an ellipse with given semimajor and semiminor axes, centered on (x, y).- Parameters:
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 ellipse- Throws:
RuntimeException- if either of the axes are negative
-
arc
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 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.- Throws:
RuntimeException- if the radius of the circle is negative
-
square
Draw a square of side length 2r, centered on (x, y).- Parameters:
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 square- Throws:
RuntimeException- if r is negative
-
filledSquare
Draw a filled square of side length 2r, centered on (x, y).- Parameters:
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 square- Throws:
RuntimeException- if r is negative
-
rectangle
Draw a rectangle of given half width and half height, centered on (x, y).- Parameters:
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 rectangle- Throws:
RuntimeException- if halfWidth or halfHeight is negative
-
filledRectangle
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 rectangley- the y-coordinate of the center of the rectanglehalfWidth- is half the width of the rectanglehalfHeight- is half the height of the rectangle- Throws:
RuntimeException- if halfWidth or halfHeight is negative
-
polygon
Draw a polygon with the given (x[i], y[i]) coordinates.- Parameters:
x- an array of all the x-coordindates of the polygony- an array of all the y-coordindates of the polygon
-
filledPolygon
Draw a filled polygon with the given (x[i], y[i]) coordinates.- Parameters:
x- an array of all the x-coordindates of the polygony- an array of all the y-coordindates of the polygon
-
getImage
-
picture
Draw picture (gif, jpg, or png) centered on (x, y).- Parameters:
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"- Throws:
RuntimeException- if the image is corrupt
-
picture
Draw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees- Parameters:
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 counterclockwise- Throws:
RuntimeException- if the image is corrupt
-
picture
Draw picture (gif, jpg, or png) centered on (x, y), rescaled to w-by-h.- Parameters:
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 image- Throws:
RuntimeException- if the width height are negativeRuntimeException- if the image is corrupt
-
picture
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 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 counterclockwise- Throws:
RuntimeException- if the image is corrupt
-
text
Write the given text string in the current font, centered on (x, y).- Parameters:
x- the center x-coordinate of the texty- the center y-coordinate of the texts- the text
-
text
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 texty- the center y-coordinate of the texts- the textdegrees- is the number of degrees to rotate counterclockwise
-
textLeft
Write the given text string in the current font, left-aligned at (x, y).- Parameters:
x- the x-coordinate of the texty- the y-coordinate of the texts- the text
-
textRight
Write the given text string in the current font, right-aligned at (x, y).- Parameters:
x- the x-coordinate of the texty- the y-coordinate of the texts- the text
-
show
Display on screen, pause for t milliseconds, and turn on animation mode: subsequent calls to drawing methods such asline(),circle(), andsquare()will not be displayed on screen until the next call toshow(). 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 (callshow(0)to defer drawing on screen, draw the shapes, and callshow(0)to display them all on screen at once).- Parameters:
t- number of milliseconds
-
show
Display on-screen and turn off animation mode: subsequent calls to drawing methods such asline(),circle(), andsquare()will be displayed on screen when called. This is the default. -
draw
-
save
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
This method cannot be called directly.- Specified by:
actionPerformedin interfaceActionListener
-
mousePressed
Is the mouse being pressed?- Returns:
- true or false
-
mouseX
What is the x-coordinate of the mouse?- Returns:
- the value of the x-coordinate of the mouse
-
mouseY
What is the y-coordinate of the mouse?- Returns:
- the value of the y-coordinate of the mouse
-
mouseClicked
This method cannot be called directly.- Specified by:
mouseClickedin interfaceMouseListener
-
mouseEntered
This method cannot be called directly.- Specified by:
mouseEnteredin interfaceMouseListener
-
mouseExited
This method cannot be called directly.- Specified by:
mouseExitedin interfaceMouseListener
-
mousePressed
This method cannot be called directly.- Specified by:
mousePressedin interfaceMouseListener
-
mouseReleased
This method cannot be called directly.- Specified by:
mouseReleasedin interfaceMouseListener
-
mouseDragged
This method cannot be called directly.- Specified by:
mouseDraggedin interfaceMouseMotionListener
-
mouseMoved
This method cannot be called directly.- Specified by:
mouseMovedin interfaceMouseMotionListener
-
hasNextKeyTyped
Has the user typed a key?- Returns:
- true if the user has typed a key, false otherwise
-
nextKeyTyped
What is 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 Unicode key typed
-
isKeyPressed
Is the keycode currently being pressed? This method takes as an argument the keycode (corresponding to a physical key). It can handle action keys (such as F1 and arrow keys) and modifier keys (such as shift and control). See KeyEvent.java for a description of key codes.- Returns:
- true if keycode is currently being pressed, false otherwise
-
keyTyped
This method cannot be called directly.- Specified by:
keyTypedin interfaceKeyListener
-
keyPressed
This method cannot be called directly.- Specified by:
keyPressedin interfaceKeyListener
-
keyReleased
This method cannot be called directly.- Specified by:
keyReleasedin interfaceKeyListener
-
main
Test client.
-