public final class Draw extends Object implements ActionListener, MouseListener, MouseMotionListener, KeyListener
For additional documentation, see Section 3.1 of Computer Science: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.
Modifier and Type | Field and Description |
---|---|
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 |
PRINCETON_ORANGE
Shade of orange used in Princeton's identity.
|
static Color |
RED
The color red.
|
static Color |
WHITE
The color white.
|
static Color |
YELLOW
The color yellow.
|
Constructor and Description |
---|
Draw()
Initializes an empty drawing object.
|
Modifier and Type | Method and Description |
---|---|
void |
actionPerformed(ActionEvent e)
This method cannot be called directly.
|
void |
addListener(DrawListener listener)
Adds a
DrawListener to listen to keyboard and mouse events. |
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).
|
void |
circle(double x,
double y,
double radius)
Draws a circle of the specified radius, centered at (x, y).
|
void |
clear()
Clears the screen to the default color (white).
|
void |
clear(Color color)
Clears the screen to the given color.
|
void |
disableDoubleBuffering()
Disable double buffering.
|
void |
disableTimer() |
void |
ellipse(double x,
double y,
double semiMajorAxis,
double semiMinorAxis)
Draws an ellipse with the specified semimajor and semiminor axes,
centered at (x, y).
|
void |
enableDoubleBuffering()
Enable double buffering.
|
void |
enableTimer(int callsPerSecond)
Sets a timer that calls update() method a specified number of times
per second.
|
void |
filledCircle(double x,
double y,
double radius)
Draws a filled circle of the specified radius, centered at (x, y).
|
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).
|
void |
filledPolygon(double[] x,
double[] y)
Draws a filled polygon with the vertices
(x0, y0),
(x1, y1), ...,
(xn–1, yn–1).
|
void |
filledRectangle(double x,
double y,
double halfWidth,
double halfHeight)
Draws a filled rectangle of the specified size, centered at (x, y).
|
void |
filledSquare(double x,
double y,
double halfLength)
Draws a square of the specified size, centered at (x, y).
|
Font |
getFont()
Gets the current font.
|
JLabel |
getJLabel()
Gets the current
JLabel for use in some other GUI. |
Color |
getPenColor()
Gets the current pen color.
|
double |
getPenRadius()
Gets the current pen radius.
|
boolean |
hasNextKeyTyped()
Returns true if the user has typed a key.
|
boolean |
isKeyPressed(int keycode)
Returns true if the keycode is being pressed.
|
boolean |
isMousePressed()
Returns true if the mouse is 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.
|
void |
line(double x0,
double y0,
double x1,
double y1)
Draws 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.
|
boolean |
mousePressed()
Deprecated.
replaced by
isMousePressed() |
void |
mousePressed(MouseEvent e)
This method cannot be called directly.
|
void |
mouseReleased(MouseEvent e)
This method cannot be called directly.
|
double |
mouseX()
Returns the x-coordinate of the mouse.
|
double |
mouseY()
Returns the y-coordinate of the mouse.
|
char |
nextKeyTyped()
The next key typed by the user.
|
void |
pause(int t)
Pause for t milliseconds.
|
void |
picture(double x,
double y,
String filename)
Draws the specified image centered at (x, y).
|
void |
picture(double x,
double y,
String filename,
double degrees)
Draws the specified image centered at (x, y),
rotated given number of degrees.
|
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.
|
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.
|
void |
point(double x,
double y)
Draws a point at (x, y).
|
void |
polygon(double[] x,
double[] y)
Draws a polygon with the vertices
(x0, y0),
(x1, y1), ...,
(xn–1, yn–1).
|
void |
rectangle(double x,
double y,
double halfWidth,
double halfHeight)
Draws a rectangle of the specified size, centered at (x, y).
|
void |
save(String filename)
Saves the drawing to using the specified filename.
|
void |
setCanvasSize(int canvasWidth,
int canvasHeight)
Sets the canvas (drawing area) to be width-by-height pixels.
|
void |
setDefaultCloseOperation(int value)
Sets the default close operation.
|
void |
setFont()
Sets the font to the default font (sans serif, 16 point).
|
void |
setFont(Font font)
Sets the font to the given value.
|
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.
|
void |
setPenColor()
Sets the pen color to the default color (black).
|
void |
setPenColor(Color color)
Sets the pen color to the given color.
|
void |
setPenColor(int red,
int green,
int blue)
Sets the pen color to the given RGB color.
|
void |
setPenRadius()
Sets the pen size to the default (.002).
|
void |
setPenRadius(double radius)
Sets the radius of the pen to the given size.
|
void |
setScale()
Sets both the x-scale and y-scale to the default range (between 0.0 and 1.0).
|
void |
setScale(double min,
double max)
Sets both the x-scale and y-scale to the (same) specified range.
|
void |
setTitle(String windowTitle)
Sets the title of the drawing window to the specified string.
|
void |
setVisible(boolean isVisible)
Makes the drawing window visible or invisible.
|
void |
setXscale()
Sets the x-scale to the default range (between 0.0 and 1.0).
|
void |
setXscale(double min,
double max)
Sets the x-scale to the specified range.
|
void |
setYscale()
Sets the y-scale to the default range (between 0.0 and 1.0).
|
void |
setYscale(double min,
double max)
Sets the y-scale to the specified range.
|
void |
show()
Copies offscreen buffer to onscreen buffer.
|
void |
show(int t)
Deprecated.
replaced by
enableDoubleBuffering() , show() , and pause(int t) |
void |
square(double x,
double y,
double halfLength)
Draws a square of the specified size, centered at (x, y).
|
void |
text(double x,
double y,
String text)
Writes the given text string in the current font, centered at (x, y).
|
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.
|
void |
textLeft(double x,
double y,
String text)
Writes the given text string in the current font, left-aligned at (x, y).
|
void |
textRight(double x,
double y,
String text)
Writes the given text string in the current font, right-aligned at (x, y).
|
void |
xorOff()
Turns off xor mode.
|
void |
xorOn()
Turns on xor mode.
|
public static final Color BLACK
public static final Color BLUE
public static final Color CYAN
public static final Color DARK_GRAY
public static final Color GRAY
public static final Color GREEN
public static final Color LIGHT_GRAY
public static final Color MAGENTA
public static final Color ORANGE
public static final Color PINK
public static final Color RED
public static final Color WHITE
public static final Color YELLOW
public static final Color BOOK_BLUE
public static final Color BOOK_LIGHT_BLUE
public static final Color BOOK_RED
public static final Color PRINCETON_ORANGE
public void setVisible(boolean isVisible)
isVisible
- if true
, makes the drawing winow visible,
otherwise hides the drawing window.public void setLocationOnScreen(int x, int y)
x
- the number of pixels from the lefty
- the number of pixels from the topIllegalArgumentException
- if the width or height is 0 or negativepublic void setDefaultCloseOperation(int value)
value
- the value, typically JFrame.EXIT_ON_CLOSE
(close all windows) or JFrame.DISPOSE_ON_CLOSE
(close current window)public void setCanvasSize(int canvasWidth, int canvasHeight)
canvasWidth
- the width as a number of pixelscanvasHeight
- the height as a number of pixelsIllegalArgumentException
- unless both canvasWidth
and canvasHeight
are positivepublic void setTitle(String windowTitle)
windowTitle
- the title of the windowIllegalArgumentException
- if title
is null
public void setXscale()
public void setYscale()
public void setXscale(double min, double max)
min
- the minimum value of the x-scalemax
- the maximum value of the x-scaleIllegalArgumentException
- if (max == min)
IllegalArgumentException
- if either min
or max
is either NaN or infinitepublic void setYscale(double min, double max)
min
- the minimum value of the y-scalemax
- the maximum value of the y-scaleIllegalArgumentException
- if (max == min)
IllegalArgumentException
- if either min
or max
is either NaN or infinitepublic void setScale()
public void setScale(double min, double max)
min
- the minimum value of the y-scalemax
- the maximum value of the y-scaleIllegalArgumentException
- if (max == min)
IllegalArgumentException
- if either min
or max
is either NaN or infinitepublic void clear()
public void clear(Color color)
color
- the color to make the backgroundIllegalArgumentException
- if color
is null
public double getPenRadius()
public void setPenRadius()
public void setPenRadius(double radius)
radius
- the radius of the penIllegalArgumentException
- if radius
is negative, NaN, or infinitepublic Color getPenColor()
public void setPenColor()
public void setPenColor(Color color)
color
- the color to make the penIllegalArgumentException
- if color
is null
public 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)IllegalArgumentException
- if red
, green
,
or blue
is outside its prescribed rangepublic void xorOn()
public void xorOff()
public JLabel getJLabel()
JLabel
for use in some other GUI.JLabel
public Font getFont()
public void setFont()
public void setFont(Font font)
font
- the fontIllegalArgumentException
- if font
is null
public 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 pointIllegalArgumentException
- if any coordinate is either NaN or infinitepublic void point(double x, double y)
x
- the x-coordinate of the pointy
- the y-coordinate of the pointIllegalArgumentException
- if either x
or y
is either NaN or infinitepublic void circle(double x, double y, double radius)
x
- the x-coordinate of the center of the circley
- the y-coordinate of the center of the circleradius
- the radius of the circleIllegalArgumentException
- if radius
is negativeIllegalArgumentException
- if any argument is either NaN or infinitepublic void filledCircle(double x, double y, double radius)
x
- the x-coordinate of the center of the circley
- the y-coordinate of the center of the circleradius
- the radius of the circleIllegalArgumentException
- if radius
is negativeIllegalArgumentException
- if any argument is either NaN or infinitepublic 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 ellipseIllegalArgumentException
- if either semiMajorAxis
or semiMinorAxis
is negativeIllegalArgumentException
- if any argument is either NaN or infinitepublic 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 ellipseIllegalArgumentException
- if either semiMajorAxis
or semiMinorAxis
is negativeIllegalArgumentException
- if any argument is either NaN or infinitepublic void arc(double x, double y, double radius, double angle1, double angle2)
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.IllegalArgumentException
- if radius
is negativeIllegalArgumentException
- if any argument is either NaN or infinitepublic void square(double x, double y, double halfLength)
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 squareIllegalArgumentException
- if halfLength
is negativeIllegalArgumentException
- if any argument is either NaN or infinitepublic void filledSquare(double x, double y, double halfLength)
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 squareIllegalArgumentException
- if halfLength
is negativeIllegalArgumentException
- if any argument is either NaN or infinitepublic 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
- one half the width of the rectanglehalfHeight
- one half the height of the rectangleIllegalArgumentException
- if either halfWidth
or halfHeight
is negativeIllegalArgumentException
- if any argument is either NaN or infinitepublic 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
- one half the width of the rectanglehalfHeight
- one half the height of the rectangleIllegalArgumentException
- if either halfWidth
or halfHeight
is negativeIllegalArgumentException
- if any argument is either NaN or infinitepublic 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 polygonIllegalArgumentException
- unless x[]
and y[]
are of the same lengthIllegalArgumentException
- if any coordinate is either NaN or infiniteIllegalArgumentException
- if either x[]
or y[]
is null
public 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 polygonIllegalArgumentException
- unless x[]
and y[]
are of the same lengthIllegalArgumentException
- if any coordinate is either NaN or infiniteIllegalArgumentException
- if either x[]
or y[]
is null
public void picture(double x, double y, String filename)
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"IllegalArgumentException
- if the image filename is invalidIllegalArgumentException
- if either x
or y
is either NaN or infinitepublic void picture(double x, double y, String filename, double degrees)
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 counterclockwiseIllegalArgumentException
- if the image filename is invalidIllegalArgumentException
- if x
, y
, degrees
is NaN or infiniteIllegalArgumentException
- if filename
is null
public void picture(double x, double y, String filename, double scaledWidth, double scaledHeight)
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)IllegalArgumentException
- if either scaledWidth
or scaledHeight
is negativeIllegalArgumentException
- if the image filename is invalidIllegalArgumentException
- if x
or y
is either NaN or infiniteIllegalArgumentException
- if filename
is null
public void picture(double x, double y, String filename, double scaledWidth, double scaledHeight, double degrees)
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 counterclockwiseIllegalArgumentException
- if either scaledWidth
or scaledHeight
is negativeIllegalArgumentException
- if the image filename is invalidpublic void text(double x, double y, String text)
x
- the center x-coordinate of the texty
- the center y-coordinate of the texttext
- the text to writeIllegalArgumentException
- if text
is null
IllegalArgumentException
- if x
or y
is either NaN or infinitepublic void text(double x, double y, String text, double degrees)
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 counterclockwiseIllegalArgumentException
- if text
is null
IllegalArgumentException
- if x
, y
, or degrees
is either NaN or infinitepublic void textLeft(double x, double y, String text)
x
- the x-coordinate of the texty
- the y-coordinate of the texttext
- the textIllegalArgumentException
- if text
is null
IllegalArgumentException
- if x
or y
is either NaN or infinitepublic void textRight(double x, double y, String text)
x
- the x-coordinate of the texty
- the y-coordinate of the texttext
- the text to writeIllegalArgumentException
- if text
is null
IllegalArgumentException
- if x
or y
is either NaN or infinite@Deprecated public void show(int t)
enableDoubleBuffering()
, show()
, and pause(int t)
t
- number of millisecondspublic void pause(int t)
t
- number of millisecondspublic void show()
public void enableDoubleBuffering()
line()
, circle()
,
and square()
will be deferred until the next call
to show(). Useful for animations.public void disableDoubleBuffering()
line()
, circle()
,
and square()
will be displayed on screen when called.
This is the default.public void save(String filename)
filename
- the name of the file with one of the required suffixesIllegalArgumentException
- if filename
is null
public void actionPerformed(ActionEvent e)
actionPerformed
in interface ActionListener
public void addListener(DrawListener listener)
DrawListener
to listen to keyboard and mouse events.listener
- the {\tt DrawListener} argumentpublic boolean isMousePressed()
true
if the mouse is being pressed;
false
otherwise@Deprecated public boolean mousePressed()
isMousePressed()
true
if the mouse is being pressed;
false
otherwisepublic double mouseX()
public double mouseY()
public void mouseEntered(MouseEvent e)
mouseEntered
in interface MouseListener
public void mouseExited(MouseEvent e)
mouseExited
in interface MouseListener
public void mousePressed(MouseEvent e)
mousePressed
in interface MouseListener
public void mouseReleased(MouseEvent e)
mouseReleased
in interface MouseListener
public void mouseClicked(MouseEvent e)
mouseClicked
in interface MouseListener
public void mouseDragged(MouseEvent e)
mouseDragged
in interface MouseMotionListener
public void mouseMoved(MouseEvent e)
mouseMoved
in interface MouseMotionListener
public boolean hasNextKeyTyped()
true
if the user has typed a key; false
otherwisepublic char nextKeyTyped()
public boolean isKeyPressed(int keycode)
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
for a description of key codes.
keycode
- the keycode to checktrue
if keycode
is currently being pressed;
false
otherwisepublic void keyTyped(KeyEvent e)
keyTyped
in interface KeyListener
public void keyPressed(KeyEvent e)
keyPressed
in interface KeyListener
public void keyReleased(KeyEvent e)
keyReleased
in interface KeyListener
public void enableTimer(int callsPerSecond)
callsPerSecond
- calls per secondpublic void disableTimer()
public static void main(String[] args)
args
- the command-line arguments