public final class Draw extends Object implements ActionListener, MouseListener, MouseMotionListener, KeyListener
For additional documentation, see Section 3.1 of Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.
Modifier and Type | Field and Description |
---|---|
static Color |
BLACK |
static Color |
BLUE |
static Color |
BOOK_BLUE
Shade of blue used in Introduction to Programming in Java.
|
static Color |
BOOK_RED
Shade of red used in Algorithms 4th edition.
|
static Color |
CYAN |
static Color |
DARK_GRAY |
static Color |
GRAY |
static Color |
GREEN |
static Color |
LIGHT_GRAY |
static Color |
MAGENTA |
static Color |
ORANGE |
static Color |
PINK |
static Color |
RED |
static Color |
WHITE |
static Color |
YELLOW |
Constructor and Description |
---|
Draw()
Create an empty drawing object.
|
Draw(String name)
Create an empty drawing object with the given name.
|
Modifier and Type | Method and Description |
---|---|
void |
actionPerformed(ActionEvent e)
This method cannot be called directly.
|
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).
|
void |
circle(double x,
double y,
double r)
Draw a circle of radius r, centered on (x, y).
|
void |
clear()
Clear the screen to the default color (white).
|
void |
clear(Color color)
Clear the screen to the given color.
|
void |
ellipse(double x,
double y,
double semiMajorAxis,
double semiMinorAxis)
Draw an ellipse with given semimajor and semiminor axes, centered on (x, y).
|
void |
filledCircle(double x,
double y,
double r)
Draw filled circle of radius r, centered on (x, y).
|
void |
filledEllipse(double x,
double y,
double semiMajorAxis,
double semiMinorAxis)
Draw an ellipse with given semimajor and semiminor axes, centered on (x, y).
|
void |
filledPolygon(double[] x,
double[] y)
Draw a filled polygon with the given (x[i], y[i]) coordinates.
|
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).
|
void |
filledSquare(double x,
double y,
double r)
Draw a filled square of side length 2r, centered on (x, y).
|
Font |
getFont()
Get the current font.
|
Color |
getPenColor()
Get the current pen color.
|
double |
getPenRadius()
Get the current pen radius.
|
boolean |
hasNextKeyTyped()
Has the user typed a key?
|
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)
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.
|
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.
|
double |
mouseX()
What is the x-coordinate of the mouse?
|
double |
mouseY()
What is the y-coordinate of the mouse?
|
char |
nextKeyTyped()
What is the next key that was typed by the user?
|
void |
picture(double x,
double y,
String s)
Draw picture (gif, jpg, or png) centered on (x, y).
|
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
|
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.
|
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.
|
void |
point(double x,
double y)
Draw a point at (x, y).
|
void |
polygon(double[] x,
double[] y)
Draw a polygon with the given (x[i], y[i]) coordinates.
|
void |
rectangle(double x,
double y,
double halfWidth,
double halfHeight)
Draw a rectangle of given half width and half height, centered on (x, y).
|
void |
save(String filename)
Save to file - suffix must be png, jpg, or gif.
|
void |
setCanvasSize(int w,
int h)
Set the window size to w-by-h pixels.
|
void |
setFont()
Set the font to the default font (sans serif, 16 point).
|
void |
setFont(Font f)
Set the font to the given value.
|
void |
setLocationOnScreen(int x,
int y)
Set the upper-left hand corner of the drawing window to be (x, y), where (0, 0) is upper left.
|
void |
setPenColor()
Set the pen color to the default color (black).
|
void |
setPenColor(Color color)
Set the pen color to the given color.
|
void |
setPenRadius()
Set the pen size to the default (.002).
|
void |
setPenRadius(double r)
Set the radius of the pen to the given size.
|
void |
setXscale()
Set the x-scale to be the default (between 0.0 and 1.0).
|
void |
setXscale(double min,
double max)
Set the x-scale (a 10% border is added to the values)
|
void |
setYscale()
Set the y-scale to be the default (between 0.0 and 1.0).
|
void |
setYscale(double min,
double max)
Set the y-scale (a 10% border is added to the values).
|
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.
|
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().
|
void |
square(double x,
double y,
double r)
Draw a square of side length 2r, centered on (x, y).
|
void |
text(double x,
double y,
String s)
Write the given text string in the current font, centered on (x, y).
|
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
|
void |
textLeft(double x,
double y,
String s)
Write the given text string in the current font, left-aligned at (x, y).
|
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_RED
public Draw(String name)
name
- the title of the drawing window.public Draw()
public void setLocationOnScreen(int x, int y)
x
- the number of pixels from the lefty
- the number of pixels from the topa
- RunTimeException if the width or height is 0 or negativepublic void setCanvasSize(int w, int h)
w
- the width as a number of pixelsh
- the height as a number of pixelsa
- RunTimeException if the width or height is 0 or negativepublic 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-scalepublic void setYscale(double min, double max)
min
- the minimum value of the y-scalemax
- the maximum value of the y-scalepublic void clear()
public void clear(Color color)
color
- the Color to make the backgroundpublic double getPenRadius()
public void setPenRadius()
public void setPenRadius(double r)
r
- the radius of the penRuntimeException
- if r is negativepublic Color getPenColor()
public void setPenColor()
public void setPenColor(Color color)
color
- the Color to make the penpublic Font getFont()
public void setFont()
public void setFont(Font f)
f
- the font to make textpublic 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 void point(double x, double y)
x
- the x-coordinate of the pointy
- the y-coordinate of the pointpublic 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 circleRuntimeException
- if the radius of the circle is negativepublic 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 circleRuntimeException
- if the radius of the circle is negativepublic 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 ellipseRuntimeException
- if either of the axes are negativepublic 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 ellipseRuntimeException
- if either of the axes are negativepublic 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.RuntimeException
- if the radius of the circle is negativepublic 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 squareRuntimeException
- if r is negativepublic 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 squareRuntimeException
- if r is negativepublic 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 rectangleRuntimeException
- if halfWidth or halfHeight is negativepublic 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 rectangleRuntimeException
- if halfWidth or halfHeight is negativepublic void polygon(double[] x, double[] y)
x
- an array of all the x-coordindates of the polygony
- an array of all the y-coordindates of the polygonpublic void filledPolygon(double[] x, double[] y)
x
- an array of all the x-coordindates of the polygony
- an array of all the y-coordindates of the polygonpublic void picture(double x, double y, 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"RuntimeException
- if the image is corruptpublic void picture(double x, double y, 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 counterclockwiseRuntimeException
- if the image is corruptpublic void picture(double x, double y, 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 imageRuntimeException
- if the image is corruptpublic void picture(double x, double y, 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 counterclockwiseRuntimeException
- if the image is corruptpublic void text(double x, double y, String s)
x
- the center x-coordinate of the texty
- the center y-coordinate of the texts
- the textpublic void text(double x, double y, 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 void textLeft(double x, double y, String s)
x
- the x-coordinate of the texty
- the y-coordinate of the texts
- the textpublic void show(int t)
t
- number of millisecondspublic void show()
public void save(String filename)
filename
- the name of the file with one of the required suffixespublic void actionPerformed(ActionEvent e)
actionPerformed
in interface ActionListener
public boolean mousePressed()
public double mouseX()
public double mouseY()
public void mouseClicked(MouseEvent e)
mouseClicked
in interface MouseListener
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 mouseDragged(MouseEvent e)
mouseDragged
in interface MouseMotionListener
public void mouseMoved(MouseEvent e)
mouseMoved
in interface MouseMotionListener
public boolean hasNextKeyTyped()
public char nextKeyTyped()
public 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 static void main(String[] args)