Package edu.princeton.cs.algs4
Class Draw
- Object
-
- edu.princeton.cs.algs4.Draw
-
- All Implemented Interfaces:
ActionListener,KeyListener,MouseListener,MouseMotionListener,EventListener
public final class Draw extends Object implements ActionListener, MouseListener, MouseMotionListener, KeyListener
TheDrawdata type 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. This is the object-oriented version of standard draw; it supports multiple independent drawing windows.For additional documentation, see Section 3.1 of Computer Science: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Field Summary
Fields Modifier and Type Field Description static ColorAQUAThe color aqua (0, 255, 255).static ColorBLACKThe color black (0, 0, 0).static ColorBLUEThe color blue (0, 0, 255).static ColorBOOK_BLUEThe shade of blue used in Introduction to Programming in Java.static ColorBOOK_LIGHT_BLUEThe shade of light blue used in Introduction to Programming in Java.static ColorBOOK_REDThe shade of red used in Algorithms, 4th edition.static ColorCYANThe color cyan (0, 255, 255).static ColorDARK_GRAYThe color dark gray (64, 64, 64).static ColorFUSCIAThe color fuscia (255, 0, 255).static ColorGRAYThe color gray (128, 128, 128).static ColorGREENThe color green (0, 128, 0).static ColorLIGHT_GRAYThe color light gray (192, 192, 192).static ColorLIMEThe color lime (0, 255, 0).static ColorMAGENTAThe color magenta (255, 0, 255).static ColorMAROONThe color maroon (128, 0, 0).static ColorNAVYThe color navy (0, 0, 128).static ColorOLIVEThe color olive (128, 128, 0).static ColorORANGEThe color orange (255, 200, 0).static ColorPINKThe color pink (255, 175, 175).static ColorPRINCETON_ORANGEThe shade of orange used in Princeton University's identity.static ColorPURPLEThe color purple (128, 0, 128).static ColorREDThe color red (255, 0, 0).static ColorSILVERThe color silver (192, 192, 192).static ColorTEALThe color teal (0, 128, 128).static ColorTRANSPARENTA 100% transparent color, for a transparent background.static ColorWHITEThe color white (255, 255, 255).static ColorYELLOWThe color yellow (255, 255, 0).
-
Constructor Summary
Constructors Constructor Description Draw()Initializes an empty drawing object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidactionPerformed(ActionEvent event)This method cannot be called directly.voidaddListener(DrawListener listener)Adds aDrawListenerto listen to keyboard and mouse events.voidarc(double x, double y, double radius, double angle1, double angle2)Draws a circular arc of the specified radius, centered at (x, y), from angle1 to angle2 (in degrees).voidcircle(double x, double y, double radius)Draws a circle of the specified radius, centered at (x, y).voidclear()Clears the screen using the default background color (white).voidclear(Color color)Clears the screen using the specified background color.voidclose()Closes the drawing window.voiddisableDoubleBuffering()Disable double buffering.voiddisableTimer()voidellipse(double x, double y, double semiMajorAxis, double semiMinorAxis)Draws an ellipse with the specified semimajor and semiminor axes, centered at (x, y).voidenableDoubleBuffering()Enable double buffering.voidenableTimer(int callsPerSecond)Sets a timer that calls update() method a specified number of times per second.voidfilledCircle(double x, double y, double radius)Draws a filled circle of the specified radius, centered at (x, y).voidfilledEllipse(double x, double y, double semiMajorAxis, double semiMinorAxis)Draws a filled ellipse with the specified semimajor and semiminor axes, centered at (x, y).voidfilledPolygon(double[] x, double[] y)Draws a filled polygon with the vertices (x0, y0), (x1, y1), ..., (xn–1, yn–1).voidfilledRectangle(double x, double y, double halfWidth, double halfHeight)Draws a filled rectangle of the specified size, centered at (x, y).voidfilledSquare(double x, double y, double halfLength)Draws a square of the specified size, centered at (x, y).ColorgetBackgroundColor()Returns the current background color.FontgetFont()Returns the current font.JLabelgetJLabel()Returns the currentJLabelfor use in some other GUI.ColorgetPenColor()Returns the current pen color.doublegetPenRadius()Returns the current pen radius.booleanhasNextKeyTyped()Returns true if the user has typed a key.booleanisKeyPressed(int keycode)Returns true if the keycode is being pressed.booleanisMousePressed()Returns true if the mouse is being pressed.voidkeyPressed(KeyEvent event)This method cannot be called directly.voidkeyReleased(KeyEvent event)This method cannot be called directly.voidkeyTyped(KeyEvent event)This method cannot be called directly.voidline(double x0, double y0, double x1, double y1)Draws a line from (x0, y0) to (x1, y1).static voidmain(String[] args)Test client.voidmouseClicked(MouseEvent event)This method cannot be called directly.voidmouseDragged(MouseEvent event)This method cannot be called directly.voidmouseEntered(MouseEvent event)This method cannot be called directly.voidmouseExited(MouseEvent event)This method cannot be called directly.voidmouseMoved(MouseEvent event)This method cannot be called directly.voidmousePressed(MouseEvent event)This method cannot be called directly.voidmouseReleased(MouseEvent event)This method cannot be called directly.doublemouseX()Returns the x-coordinate of the mouse.doublemouseY()Returns the y-coordinate of the mouse.charnextKeyTyped()The next key typed by the user.voidpause(int t)Pause for t milliseconds.voidpicture(double x, double y, String filename)Draws the specified image centered at (x, y).voidpicture(double x, double y, String filename, double degrees)Draws the specified image centered at (x, y), rotated given number of degrees.voidpicture(double x, double y, String filename, double scaledWidth, double scaledHeight)Draws the specified image centered at (x, y), rescaled to the specified bounding box.voidpicture(double x, double y, String filename, double scaledWidth, double scaledHeight, double degrees)Draws the specified image centered at (x, y), rotated given number of degrees, and rescaled to the specified bounding box.voidpoint(double x, double y)Draws a point at (x, y).voidpolygon(double[] x, double[] y)Draws a polygon with the vertices (x0, y0), (x1, y1), ..., (xn–1, yn–1).voidrectangle(double x, double y, double halfWidth, double halfHeight)Draws a rectangle of the specified size, centered at (x, y).voidsave(String filename)Saves the drawing to a file in a supported file format (typically JPEG, PNG, GIF, TIFF, and BMP).voidsetCanvasSize()Sets the canvas (drawing area) to be 512-by-512 pixels.voidsetCanvasSize(int canvasWidth, int canvasHeight)Sets the canvas (drawing area) to be width-by-height pixels.voidsetDefaultCloseOperation(int value)Sets the default close operation.voidsetFont()Sets the font to the default font (sans serif, 16 point).voidsetFont(Font font)Sets the font to the given value.voidsetLocationOnScreen(int x, int y)Sets the upper-left hand corner of the drawing window to be (x, y), where (0, 0) is upper left.voidsetPenColor()Sets the pen color to the default color (black).voidsetPenColor(int red, int green, int blue)Sets the pen color to the given RGB color.voidsetPenColor(Color color)Sets the pen color to the given color.voidsetPenRadius()Sets the pen radius to the default (0.002).voidsetPenRadius(double radius)Sets the radius of the pen to the given size.voidsetScale()Sets both the x-scale and y-scale to the default range (between 0.0 and 1.0).voidsetScale(double min, double max)Sets both the x-scale and y-scale to the (same) specified range.voidsetTitle(String windowTitle)Sets the title of the drawing window to the specified string.voidsetVisible(boolean isVisible)Makes the drawing window visible or invisible.voidsetXscale()Sets the x-scale to the default range (between 0.0 and 1.0).voidsetXscale(double min, double max)Sets the x-scale to the specified range.voidsetYscale()Sets the y-scale to the default range (between 0.0 and 1.0).voidsetYscale(double min, double max)Sets the y-scale to the specified range.voidshow()Copies offscreen buffer to onscreen buffer.voidsquare(double x, double y, double halfLength)Draws a square of the specified size, centered at (x, y).voidtext(double x, double y, String text)Writes the given text string in the current font, centered at (x, y).voidtext(double x, double y, String text, double degrees)Writes the given text string in the current font, centered at (x, y) and rotated by the specified number of degrees.voidtextLeft(double x, double y, String text)Writes the given text string in the current font, left-aligned at (x, y).voidtextRight(double x, double y, String text)Writes the given text string in the current font, right-aligned at (x, y).voidxorOff()Turns off xor mode.voidxorOn()Turns on xor mode.
-
-
-
Field Detail
-
AQUA
public static final Color AQUA
The color aqua (0, 255, 255).
-
BLACK
public static final Color BLACK
The color black (0, 0, 0).
-
BLUE
public static final Color BLUE
The color blue (0, 0, 255).
-
CYAN
public static final Color CYAN
The color cyan (0, 255, 255).
-
FUSCIA
public static final Color FUSCIA
The color fuscia (255, 0, 255).
-
DARK_GRAY
public static final Color DARK_GRAY
The color dark gray (64, 64, 64).
-
GRAY
public static final Color GRAY
The color gray (128, 128, 128).
-
GREEN
public static final Color GREEN
The color green (0, 128, 0).
-
LIGHT_GRAY
public static final Color LIGHT_GRAY
The color light gray (192, 192, 192).
-
LIME
public static final Color LIME
The color lime (0, 255, 0).
-
MAGENTA
public static final Color MAGENTA
The color magenta (255, 0, 255).
-
MAROON
public static final Color MAROON
The color maroon (128, 0, 0).
-
NAVY
public static final Color NAVY
The color navy (0, 0, 128).
-
OLIVE
public static final Color OLIVE
The color olive (128, 128, 0).
-
ORANGE
public static final Color ORANGE
The color orange (255, 200, 0).
-
PINK
public static final Color PINK
The color pink (255, 175, 175).
-
PURPLE
public static final Color PURPLE
The color purple (128, 0, 128).
-
RED
public static final Color RED
The color red (255, 0, 0).
-
SILVER
public static final Color SILVER
The color silver (192, 192, 192).
-
TEAL
public static final Color TEAL
The color teal (0, 128, 128).
-
WHITE
public static final Color WHITE
The color white (255, 255, 255).
-
YELLOW
public static final Color YELLOW
The color yellow (255, 255, 0).
-
TRANSPARENT
public static final Color TRANSPARENT
A 100% transparent color, for a transparent background.
-
BOOK_BLUE
public static final Color BOOK_BLUE
The 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
The 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
The shade of red used in Algorithms, 4th edition. It is Pantone 1805U. The RGB values are approximately (150, 35, 31).
-
PRINCETON_ORANGE
public static final Color PRINCETON_ORANGE
The shade of orange used in Princeton University's identity. It is PMS 158. The RGB values are approximately (245, 128, 37).
-
-
Method Detail
-
setVisible
public void setVisible(boolean isVisible)
Makes the drawing window visible or invisible.- Parameters:
isVisible- iftrue, makes the drawing window visible, otherwise hides the drawing window.
-
setLocationOnScreen
public void setLocationOnScreen(int x, int y)Sets 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 lefty- the number of pixels from the top- Throws:
IllegalArgumentException- if the width or height is 0 or negative
-
setDefaultCloseOperation
public void setDefaultCloseOperation(int value)
Sets the default close operation.- Parameters:
value- the value, typicallyJFrame.EXIT_ON_CLOSE(close all windows) orJFrame.DISPOSE_ON_CLOSE(close current window)
-
setCanvasSize
public void setCanvasSize()
Sets the canvas (drawing area) to be 512-by-512 pixels. This also clears the current drawing using the default background color (white). Ordinarily, this method is called once, at the very beginning of a program.
-
setCanvasSize
public void setCanvasSize(int canvasWidth, int canvasHeight)Sets the canvas (drawing area) to be width-by-height pixels. This also clears the current drawing using the default background color (white). Ordinarily, this method is called once, at the very beginning of a program.- Parameters:
canvasWidth- the width as a number of pixelscanvasHeight- the height as a number of pixels- Throws:
IllegalArgumentException- unless bothcanvasWidthandcanvasHeightare positive
-
close
public void close()
Closes the drawing window. This allows the client program to terminate instead of requiring the user to close the drawing window manually. Drawing after calling this method will restore the previous window state.
-
setTitle
public void setTitle(String windowTitle)
Sets the title of the drawing window to the specified string.- Parameters:
windowTitle- the title of the window- Throws:
IllegalArgumentException- iftitleisnull
-
setXscale
public void setXscale()
Sets the x-scale to the default range (between 0.0 and 1.0).
-
setYscale
public void setYscale()
Sets the y-scale to the default range (between 0.0 and 1.0).
-
setXscale
public void setXscale(double min, double max)Sets the x-scale to the specified range.- Parameters:
min- the minimum value of the x-scalemax- the maximum value of the x-scale- Throws:
IllegalArgumentException- if(max == min)IllegalArgumentException- if eitherminormaxis either NaN or infinite
-
setYscale
public void setYscale(double min, double max)Sets the y-scale to the specified range.- Parameters:
min- the minimum value of the y-scalemax- the maximum value of the y-scale- Throws:
IllegalArgumentException- if(max == min)IllegalArgumentException- if eitherminormaxis either NaN or infinite
-
setScale
public void setScale()
Sets both the x-scale and y-scale to the default range (between 0.0 and 1.0).
-
setScale
public void setScale(double min, double max)Sets both the x-scale and y-scale to the (same) specified range.- Parameters:
min- the minimum value of the y-scalemax- the maximum value of the y-scale- Throws:
IllegalArgumentException- if(max == min)IllegalArgumentException- if eitherminormaxis either NaN or infinite
-
clear
public void clear()
Clears the screen using the default background color (white).
-
clear
public void clear(Color color)
Clears the screen using the specified background color. To make the background transparent, useDraw.TRANSPARENT.- Parameters:
color- the color to make the background- Throws:
IllegalArgumentException- ifcolorisnull
-
getPenRadius
public double getPenRadius()
Returns the current pen radius.- Returns:
- the current pen radius
-
setPenRadius
public void setPenRadius()
Sets the pen radius to the default (0.002).
-
setPenRadius
public void setPenRadius(double radius)
Sets the radius of the pen to the given size.- Parameters:
radius- the radius of the pen- Throws:
IllegalArgumentException- ifradiusis negative, NaN, or infinite
-
getPenColor
public Color getPenColor()
Returns the current pen color.- Returns:
- the current pen color
-
getBackgroundColor
public Color getBackgroundColor()
Returns the current background color.- Returns:
- the current background color
-
setPenColor
public void setPenColor()
Sets the pen color to the default color (black).
-
setPenColor
public void setPenColor(Color color)
Sets the pen color to the given color.- Parameters:
color- the color to make the pen- Throws:
IllegalArgumentException- ifcolorisnull
-
setPenColor
public 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- ifred,green, orblueis outside its prescribed range
-
xorOn
public void xorOn()
Turns on xor mode.
-
xorOff
public void xorOff()
Turns off xor mode.
-
getJLabel
public JLabel getJLabel()
Returns the currentJLabelfor use in some other GUI.- Returns:
- the current
JLabel
-
getFont
public Font getFont()
Returns the current font.- Returns:
- the current font
-
setFont
public void setFont()
Sets the font to the default font (sans serif, 16 point).
-
setFont
public void setFont(Font font)
Sets the font to the given value.- Parameters:
font- the font- Throws:
IllegalArgumentException- iffontisnull
-
line
public void line(double x0, double y0, double x1, double y1)Draws 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- Throws:
IllegalArgumentException- if any coordinate is either NaN or infinite
-
point
public void point(double x, double y)Draws a point at (x, y).- Parameters:
x- the x-coordinate of the pointy- the y-coordinate of the point- Throws:
IllegalArgumentException- if eitherxoryis either NaN or infinite
-
circle
public void circle(double x, double y, double radius)Draws a circle of the specified radius, centered at (x, y).- Parameters:
x- the x-coordinate of the center of the circley- the y-coordinate of the center of the circleradius- the radius of the circle- Throws:
IllegalArgumentException- ifradiusis negativeIllegalArgumentException- if any argument is either NaN or infinite
-
filledCircle
public void filledCircle(double x, double y, double radius)Draws a filled circle of the specified radius, centered at (x, y).- Parameters:
x- the x-coordinate of the center of the circley- the y-coordinate of the center of the circleradius- the radius of the circle- Throws:
IllegalArgumentException- ifradiusis negativeIllegalArgumentException- if any argument is either NaN or infinite
-
ellipse
public void ellipse(double x, double y, double semiMajorAxis, double semiMinorAxis)Draws an ellipse with the specified semimajor and semiminor axes, centered at (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:
IllegalArgumentException- if eithersemiMajorAxisorsemiMinorAxisis negativeIllegalArgumentException- if any argument is either NaN or infinite
-
filledEllipse
public void filledEllipse(double x, double y, double semiMajorAxis, double semiMinorAxis)Draws a filled ellipse with the specified semimajor and semiminor axes, centered at (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:
IllegalArgumentException- if eithersemiMajorAxisorsemiMinorAxisis negativeIllegalArgumentException- if any argument is either NaN or infinite
-
arc
public void arc(double x, double y, double radius, double angle1, double angle2)Draws a circular arc of the specified radius, centered at (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 circleradius- 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:
IllegalArgumentException- ifradiusis negativeIllegalArgumentException- if any argument is either NaN or infinite
-
square
public void square(double x, double y, double halfLength)Draws a square of the specified size, centered at (x, y).- Parameters:
x- the x-coordinate of the center of the squarey- the y-coordinate of the center of the squarehalfLength- one half the length of any side of the square- Throws:
IllegalArgumentException- ifhalfLengthis negativeIllegalArgumentException- if any argument is either NaN or infinite
-
filledSquare
public void filledSquare(double x, double y, double halfLength)Draws a square of the specified size, centered at (x, y).- Parameters:
x- the x-coordinate of the center of the squarey- the y-coordinate of the center of the squarehalfLength- one half the length of any side of the square- Throws:
IllegalArgumentException- ifhalfLengthis negativeIllegalArgumentException- if any argument is either NaN or infinite
-
rectangle
public void rectangle(double x, double y, double halfWidth, double halfHeight)Draws a rectangle of the specified size, centered at (x, y).- Parameters:
x- the x-coordinate of the center of the rectangley- the y-coordinate of the center of the rectanglehalfWidth- one half the width of the rectanglehalfHeight- one half the height of the rectangle- Throws:
IllegalArgumentException- if eitherhalfWidthorhalfHeightis negativeIllegalArgumentException- if any argument is either NaN or infinite
-
filledRectangle
public void filledRectangle(double x, double y, double halfWidth, double halfHeight)Draws a filled rectangle of the specified size, centered at (x, y).- Parameters:
x- the x-coordinate of the center of the rectangley- the y-coordinate of the center of the rectanglehalfWidth- one half the width of the rectanglehalfHeight- one half the height of the rectangle- Throws:
IllegalArgumentException- if eitherhalfWidthorhalfHeightis negativeIllegalArgumentException- if any argument is either NaN or infinite
-
polygon
public void polygon(double[] x, double[] y)Draws a polygon with the vertices (x0, y0), (x1, y1), ..., (xn–1, yn–1).- Parameters:
x- an array of all the x-coordinates of the polygony- an array of all the y-coordinates of the polygon- Throws:
IllegalArgumentException- unlessx[]andy[]are of the same lengthIllegalArgumentException- if any coordinate is either NaN or infiniteIllegalArgumentException- if eitherx[]ory[]isnull
-
filledPolygon
public void filledPolygon(double[] x, double[] y)Draws a filled polygon with the vertices (x0, y0), (x1, y1), ..., (xn–1, yn–1).- Parameters:
x- an array of all the x-coordinates of the polygony- an array of all the y-coordinates of the polygon- Throws:
IllegalArgumentException- unlessx[]andy[]are of the same lengthIllegalArgumentException- if any coordinate is either NaN or infiniteIllegalArgumentException- if eitherx[]ory[]isnull
-
picture
public void picture(double x, double y, String filename)Draws the specified image centered at (x, y). The supported image formats are typically JPEG, PNG, GIF, TIFF, and BMP. As an optimization, the picture is cached, so there is no performance penalty for redrawing the same image multiple times (e.g., in an animation). However, if you change the picture file after drawing it, subsequent calls will draw the original picture.- Parameters:
x- the center x-coordinate of the imagey- the center y-coordinate of the imagefilename- the name of the image/picture, e.g., "ball.gif"- Throws:
IllegalArgumentException- if the image filename is invalidIllegalArgumentException- if eitherxoryis either NaN or infinite
-
picture
public void picture(double x, double y, String filename, double degrees)Draws the specified image centered at (x, y), rotated given number of degrees. The supported image formats are typically JPEG, PNG, GIF, TIFF, and BMP.- Parameters:
x- the center x-coordinate of the imagey- the center y-coordinate of the imagefilename- the name of the image/picture, e.g., "ball.gif"degrees- is the number of degrees to rotate counterclockwise- Throws:
IllegalArgumentException- if the image filename is invalidIllegalArgumentException- ifx,y,degreesis NaN or infiniteIllegalArgumentException- iffilenameisnull
-
picture
public void picture(double x, double y, String filename, double scaledWidth, double scaledHeight)Draws the specified image centered at (x, y), rescaled to the specified bounding box. The supported image formats are typically JPEG, PNG, GIF, TIFF, and BMP.- Parameters:
x- the center x-coordinate of the imagey- the center y-coordinate of the imagefilename- the name of the image/picture, e.g., "ball.gif"scaledWidth- the width of the scaled image (in screen coordinates)scaledHeight- the height of the scaled image (in screen coordinates)- Throws:
IllegalArgumentException- if eitherscaledWidthorscaledHeightis negativeIllegalArgumentException- if the image filename is invalidIllegalArgumentException- ifxoryis either NaN or infiniteIllegalArgumentException- iffilenameisnull
-
picture
public void picture(double x, double y, String filename, double scaledWidth, double scaledHeight, double degrees)Draws the specified image centered at (x, y), rotated given number of degrees, and rescaled to the specified bounding box. The supported image formats are typically JPEG, PNG, GIF, TIFF, and BMP.- Parameters:
x- the center x-coordinate of the imagey- the center y-coordinate of the imagefilename- the name of the image/picture, e.g., "ball.gif"scaledWidth- the width of the scaled image (in screen coordinates)scaledHeight- the height of the scaled image (in screen coordinates)degrees- is the number of degrees to rotate counterclockwise- Throws:
IllegalArgumentException- if eitherscaledWidthorscaledHeightis negativeIllegalArgumentException- if the image filename is invalid
-
text
public void text(double x, double y, String text)Writes the given text string in the current font, centered at (x, y).- Parameters:
x- the center x-coordinate of the texty- the center y-coordinate of the texttext- the text to write- Throws:
IllegalArgumentException- iftextisnullIllegalArgumentException- ifxoryis either NaN or infinite
-
text
public void text(double x, double y, String text, double degrees)Writes the given text string in the current font, centered at (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 texttext- the text to writedegrees- is the number of degrees to rotate counterclockwise- Throws:
IllegalArgumentException- iftextisnullIllegalArgumentException- ifx,y, ordegreesis either NaN or infinite
-
textLeft
public void textLeft(double x, double y, String text)Writes 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 texttext- the text- Throws:
IllegalArgumentException- iftextisnullIllegalArgumentException- ifxoryis either NaN or infinite
-
textRight
public void textRight(double x, double y, String text)Writes 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 texttext- the text to write- Throws:
IllegalArgumentException- iftextisnullIllegalArgumentException- ifxoryis either NaN or infinite
-
pause
public void pause(int t)
Pause for t milliseconds. This method is intended to support computer animations.- Parameters:
t- number of milliseconds- Throws:
IllegalArgumentException- iftis negative
-
show
public void show()
Copies offscreen buffer to onscreen buffer. There is no reason to call this method unless double buffering is enabled.
-
enableDoubleBuffering
public void enableDoubleBuffering()
Enable double buffering. All subsequent calls to drawing methods such asline(),circle(), andsquare()will be deferred until the next call to show(). Useful for animations.
-
disableDoubleBuffering
public void disableDoubleBuffering()
Disable double buffering. All subsequent calls to drawing methods such asline(),circle(), andsquare()will be displayed on screen when called. This is the default.
-
save
public void save(String filename)
Saves the drawing to a file in a supported file format (typically JPEG, PNG, GIF, TIFF, and BMP). The filetype extension must be.jpg,.png,.gif,.bmp, or.tif.- Parameters:
filename- the name of the file- Throws:
IllegalArgumentException- iffilenameisnullIllegalArgumentException- iffilenameis the empty stringIllegalArgumentException- iffilenamehas invalid filetype extensionIllegalArgumentException- if cannot write the filefilename
-
actionPerformed
public void actionPerformed(ActionEvent event)
This method cannot be called directly.- Specified by:
actionPerformedin interfaceActionListener
-
addListener
public void addListener(DrawListener listener)
Adds aDrawListenerto listen to keyboard and mouse events.- Parameters:
listener- the {\tt DrawListener} argument
-
isMousePressed
public boolean isMousePressed()
Returns true if the mouse is being pressed.- Returns:
trueif the mouse is being pressed;falseotherwise
-
mouseX
public double mouseX()
Returns the x-coordinate of the mouse.- Returns:
- the x-coordinate of the mouse
-
mouseY
public double mouseY()
Returns the y-coordinate of the mouse.- Returns:
- the y-coordinate of the mouse
-
mouseEntered
public void mouseEntered(MouseEvent event)
This method cannot be called directly.- Specified by:
mouseEnteredin interfaceMouseListener
-
mouseExited
public void mouseExited(MouseEvent event)
This method cannot be called directly.- Specified by:
mouseExitedin interfaceMouseListener
-
mousePressed
public void mousePressed(MouseEvent event)
This method cannot be called directly.- Specified by:
mousePressedin interfaceMouseListener
-
mouseReleased
public void mouseReleased(MouseEvent event)
This method cannot be called directly.- Specified by:
mouseReleasedin interfaceMouseListener
-
mouseClicked
public void mouseClicked(MouseEvent event)
This method cannot be called directly.- Specified by:
mouseClickedin interfaceMouseListener
-
mouseDragged
public void mouseDragged(MouseEvent event)
This method cannot be called directly.- Specified by:
mouseDraggedin interfaceMouseMotionListener
-
mouseMoved
public void mouseMoved(MouseEvent event)
This method cannot be called directly.- Specified by:
mouseMovedin interfaceMouseMotionListener
-
hasNextKeyTyped
public boolean hasNextKeyTyped()
Returns true if the user has typed a key.- Returns:
trueif the user has typed a key;falseotherwise
-
nextKeyTyped
public char nextKeyTyped()
The next key typed by the user.- Returns:
- the next key typed by the user
-
isKeyPressed
public boolean isKeyPressed(int keycode)
Returns true if the keycode is 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
KeyEventfor a description of key codes.- Parameters:
keycode- the keycode to check- Returns:
trueifkeycodeis currently being pressed;falseotherwise
-
keyTyped
public void keyTyped(KeyEvent event)
This method cannot be called directly.- Specified by:
keyTypedin interfaceKeyListener
-
keyPressed
public void keyPressed(KeyEvent event)
This method cannot be called directly.- Specified by:
keyPressedin interfaceKeyListener
-
keyReleased
public void keyReleased(KeyEvent event)
This method cannot be called directly.- Specified by:
keyReleasedin interfaceKeyListener
-
enableTimer
public void enableTimer(int callsPerSecond)
Sets a timer that calls update() method a specified number of times per second.- Parameters:
callsPerSecond- calls per second
-
disableTimer
public void disableTimer()
-
main
public static void main(String[] args)
Test client.- Parameters:
args- the command-line arguments
-
-