Standard draw. These methods provide basic capability for creating drawings and animations with your programs. It uses a simple graphics model that allows you to create drawings consisting of points, lines, shapes, and curves in a window on your computer and to save the drawings to a file.
This version is adapted for CS141 from Section 1.5 of Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.
Final Variable Summary | |
---|---|
Color |
BLACK
|
Color |
BLUE
|
Color |
BOOK_BLUE
Shade of blue used in Introduction to Programming in Java. |
Color |
BOOK_LIGHT_BLUE
|
Color |
BOOK_RED
Shade of red used in Algorithms 4th edition. |
Color |
CYAN
|
Color |
DARK_GRAY
|
Color |
GRAY
|
Color |
GREEN
|
Color |
LIGHT_GRAY
|
Color |
MAGENTA
|
Color |
ORANGE
|
Color |
BROWN
|
Color |
PINK
|
Color |
RED
|
Color |
WHITE
|
Color |
YELLOW
|
Method Summary | |
---|---|
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()
Clear the screen to the default color (white). |
static void |
clear(Color color)
Clear the screen to the given 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). |
Font |
getFont()
Get the current font. |
Color |
getPenColor()
Get the current pen color. |
static double |
getPenRadius()
Get the current pen radius. |
static boolean |
hasNextKeyTyped()
Has the user typed a key? |
static void |
line(double x0,
double y0,
double x1,
double y1)
Draw a line from (x0, y0) to (x1, y1). |
static boolean |
mousePressed()
Is the mouse being pressed? |
static double |
mouseX()
What is the x-coordinate of the mouse? |
static double |
mouseY()
What is the y-coordinate of the mouse? |
static char |
nextKeyTyped()
What is the next key that was typed by the user? |
static void |
picture(double x,
double y,
String s)
Draw picture (gif, jpg, or png) centered on (x, y). |
static 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 |
static 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. |
static 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. |
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(String filename)
Save to file - suffix must be png, jpg, or gif. |
static void |
setCanvasSize(int w,
int h)
Set the window size to w-by-h pixels. |
static void |
setFont()
Set the font to the default font (sans serif, 16 point). |
static void |
setFont(java.awt.Font f)
Set the font to the given value. |
static void |
setPenColor()
Set the pen color to the default color (black). |
static void |
setPenColor(Color color)
Set the pen color to the given color. |
static void |
setPenRadius()
Set the pen size to the default (.002). |
static void |
setPenRadius(double r)
Set the radius of the pen to the given size. |
static void |
setXscale()
Set the x-scale to be the default (between 0.0 and 1.0). |
static void |
setXscale(double min,
double max)
Set the x-scale (a 10% border is added to the values) |
static void |
setYscale()
Set the y-scale to be the default (between 0.0 and 1.0). |
static void |
setYscale(double min,
double max)
Set the y-scale (a 10% border is added to the values). |
double |
getXmin()
Get the minimum x value. |
double |
getXmax()
Get the maximum x value. |
double |
getYmin()
Get the minimum y value. |
double |
getYmax()
Get the maximum y value. |
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,
String s)
Write the given text string in the current font, centered on (x, y). |
static 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 |
static void |
textLeft(double x,
double y,
String s)
Write the given text string in the current font, left-aligned at (x, y). |
static void |
textRight(double x,
double y,
String s)
Write the given text string in the current font, right-aligned at (x, y). |
Methods inherited from class Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
final Color BLACK
final Color BLUE
final Color CYAN
final Color DARK_GRAY
final Color GRAY
final Color GREEN
final Color LIGHT_GRAY
final Color MAGENTA
final Color ORANGE
final Color BROWN
final Color PINK
final Color RED
final Color WHITE
final Color YELLOW
final Color BOOK_BLUE
final Color BOOK_LIGHT_BLUE
final Color BOOK_RED
Method Detail |
---|
void setCanvasSize(int w, int h)
w
- the width as a number of pixelsh
- the height as a number of pixels
a
- RunTimeException if the width or height is 0 or negativevoid setXscale()
void setYscale()
void setXscale(double min, double max)
min
- the minimum value of the x-scalemax
- the maximum value of the x-scalevoid setYscale(double min, double max)
min
- the minimum value of the y-scalemax
- the maximum value of the y-scaledouble getXmin()
double getXmax()
double getYmin()
double getYmax()
void clear()
void clear(Color color)
color
- the Color to make the backgrounddouble getPenRadius()
void setPenRadius()
void setPenRadius(double r)
r
- the radius of the pen
RuntimeException
- if r is negativeColor getPenColor()
void setPenColor()
void setPenColor(Color color)
color
- the Color to make the penjava.awt.Font getFont()
void setFont()
void setFont(java.awt.Font f)
f
- the font to make textvoid 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 pointvoid point(double x, double y)
x
- the x-coordinate of the pointy
- the y-coordinate of the pointvoid 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 circle
RuntimeException
- if the radius of the circle is negativevoid 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 circle
RuntimeException
- if the radius of the circle is negativevoid 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 ellipse
RuntimeException
- if either of the axes are negativevoid 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 ellipse
RuntimeException
- if either of the axes are negativevoid 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 negativevoid 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 square
RuntimeException
- if r is negativevoid 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 square
RuntimeException
- if r is negativevoid 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 rectangle
RuntimeException
- if halfWidth or halfHeight is negativevoid 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 rectangle
RuntimeException
- if halfWidth or halfHeight is negativevoid 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 polygonvoid 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 polygonvoid 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 corruptvoid 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 counterclockwise
RuntimeException
- if the image is corruptvoid 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 image
RuntimeException
- if the width height are negative
RuntimeException
- if the image is corruptvoid 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 counterclockwise
RuntimeException
- if the image is corruptvoid text(double x, double y, String s)
x
- the center x-coordinate of the texty
- the center y-coordinate of the texts
- the textvoid 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 counterclockwisevoid textLeft(double x, double y, String s)
x
- the x-coordinate of the texty
- the y-coordinate of the texts
- the textvoid textRight(double x, double y, String s)
x
- the x-coordinate of the texty
- the y-coordinate of the texts
- the textvoid show(int t)
t
- number of millisecondsvoid show()
void save(String filename)
filename
- the name of the file with one of the required suffixesboolean mousePressed()
double mouseX()
double mouseY()
boolean hasNextKeyTyped()
char nextKeyTyped()