public final class StdDraw extends Object implements ActionListener, MouseListener, MouseMotionListener, KeyListener
StdDraw 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, squares,
circles, and other geometric shapes in a window on your computer and
to save the drawings to a file. Standard drawing also includes
facilities for text, color, pictures, and animation, along with
user interaction via the keyboard and mouse.
Getting started.
To use this class, you must have StdDraw.class in your
Java classpath. If you used our autoinstaller, you should be all set.
Otherwise, either download
stdlib.jar
and add to your Java classpath or download
StdDraw.java
and put a copy in your working directory.
Now, type the following short program into your editor:
public class TestStdDraw {
public static void main(String[] args) {
StdDraw.setPenRadius(0.05);
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.point(0.5, 0.5);
StdDraw.setPenColor(StdDraw.MAGENTA);
StdDraw.line(0.2, 0.2, 0.8, 0.2);
}
}
If you compile and execute the program, you should see a window
appear with a thick magenta line and a blue point.
This program illustrates the two main types of methods in standard
drawing—methods that draw geometric shapes and methods that
control drawing parameters.
The methods StdDraw.line() and StdDraw.point()
draw lines and points; the methods StdDraw.setPenRadius()
and StdDraw.setPenColor() control the line thickness and color.
Points and lines. You can draw points and line segments with the following methods:
The x- and y-coordinates must be in the drawing area (between 0 and 1 and by default) or the points and lines will not be visible.
Squares, circles, rectangles, and ellipses. You can draw squares, circles, rectangles, and ellipses using the following methods:
circle(double x, double y, double radius)
ellipse(double x, double y, double semiMajorAxis, double semiMinorAxis)
square(double x, double y, double halfLength)
rectangle(double x, double y, double halfWidth, double halfHeight)
All of these methods take as arguments the location and size of the shape. The location is always specified by the x- and y-coordinates of its center. The size of a circle is specified by its radius and the size of an ellipse is specified by the lengths of its semi-major and semi-minor axes. The size of a square or rectangle is specified by its half-width or half-height. The convention for drawing squares and rectangles is parallel to those for drawing circles and ellipses, but may be unexpected to the uninitiated.
The methods above trace outlines of the given shapes. The following methods draw filled versions:
filledCircle(double x, double y, double radius)
filledEllipse(double x, double y, double semiMajorAxis, double semiMinorAxis)
filledSquare(double x, double y, double radius)
filledRectangle(double x, double y, double halfWidth, double halfHeight)
Circular arcs. You can draw circular arcs with the following method:
The arc is from the circle centered at (x, y) of the specified radius.
The arc extends from angle1 to angle2. By convention, the angles are
polar (counterclockwise angle from the x-axis)
and represented in degrees. For example, StdDraw.arc(0.0, 0.0, 1.0, 0, 90)
draws the arc of the unit circle from 3 o'clock (0 degrees) to 12 o'clock (90 degrees).
Polygons. You can draw polygons with the following methods:
The points in the polygon are (x[i], y[i]).
For example, the following code fragment draws a filled diamond
with vertices (0.1, 0.2), (0.2, 0.3), (0.3, 0.2), and (0.2, 0.1):
double[] x = { 0.1, 0.2, 0.3, 0.2 };
double[] y = { 0.2, 0.3, 0.2, 0.1 };
StdDraw.filledPolygon(x, y);
Pen size. The pen is circular, so that when you set the pen radius to r and draw a point, you get a circle of radius r. Also, lines are of thickness 2r and have rounded ends. The default pen radius is 0.005 and is not affected by coordinate scaling. This default pen radius is about 1/200 the width of the default canvas, so that if you draw 100 points equally spaced along a horizontal or vertical line, you will be able to see individual circles, but if you draw 200 such points, the result will look like a line.
For example, StdDraw.setPenRadius(0.025) makes
the thickness of the lines and the size of the points to be five times
the 0.005 default.
To draw points with the minimum possible radius (one pixel on typical
displays), set the pen radius to 0.0.
Pen color. All geometric shapes (such as points, lines, and circles) are drawn using the current pen color. By default, it is black. You can change the pen color with the following methods:
The first method allows you to specify colors using the RGB color system. This color picker is a convenient way to find a desired color.
The second method allows you to specify colors using the
Color data type, which is defined in Java's java.awt pacakge.
A number of colors are predefined in standard drawing:
BLACK, BLUE, CYAN, DARK_GRAY, GRAY,
GREEN, LIGHT_GRAY, MAGENTA, ORANGE,
PINK, RED, WHITE, YELLOW,
BOOK_BLUE, BOOK_LIGHT_BLUE, BOOK_RED, and
PRINCETON_ORANGE.
For example, StdDraw.setPenColor(StdDraw.MAGENTA) sets the
pen color to magenta.
Canvas size. By default, all drawing takes places in a 512-by-512 canvas. The canvas does not include the window title or window border. You can change the size of the canvas with the following method:
This sets the canvas size to be width-by-height pixels.
It also erases the current drawing and resets the coordinate system,
pen radius, pen color, and font back to their default values.
Ordinarly, this method is called only once, at the very beginning of a program.
For example, StdDraw.setCanvasSize(800, 800)
sets the canvas size to be 800-by-800 pixels.
Canvas scale and coordinate system. By default, all drawing takes places in the unit square, with (0, 0) at lower left and (1, 1) at upper right. You can change the default coordinate system with the following methods:
setXscale(double xmin, double xmax)
setYscale(double ymin, double ymax)
setScale(double min, double max)
The arguments are the coordinates of the minimum and maximum
x- or y-coordinates that will appear in the canvas.
For example, if you wish to use the default coordinate system but
leave a small margin, you can call StdDraw.setScale(-.05, 1.05).
These methods change the coordinate system for subsequent drawing commands; they do not affect previous drawings. These methods do not change the canvas size; so, if the x- and y-scales are different, squares will become rectangles and circles will become ellipses.
Text. You can use the following methods to annotate your drawings with text:
text(double x, double y, String text)
text(double x, double y, String text, double degrees)
textLeft(double x, double y, String text)
textRight(double x, double y, String text)
The first two methods write the specified text in the current font, centered at (x, y). The second method allows you to rotate the text. The last two methods either left- or right-align the text at (x, y).
The default font is a Sans Serif font with point size 16. You can use the following method to change the font:
To specify the font, you use the Font data type,
which is defined in Java's java.awt package.
This allows you to
choose the face, size, and style of the font. For example, the following
code fragment sets the font to Arial Bold, 60 point.
The import statement allows you to refer to Font
directly, without needing the fully qualified name java.awt.Font.
import java.awt.Font;
...
Font font = new Font("Arial", Font.BOLD, 60);
StdDraw.setFont(font);
StdDraw.text(0.5, 0.5, "Hello, World");
Images. You can use the following methods to add images to your drawings:
picture(double x, double y, String filename)
picture(double x, double y, String filename, double degrees)
picture(double x, double y, String filename, double scaledWidth, double scaledHeight)
picture(double x, double y, String filename, double scaledWidth, double scaledHeight, double degrees)
These methods draw the specified image, centered at (x, y). The supported image formats are JPEG, PNG, and GIF. The image will display at its native size, independent of the coordinate system. Optionally, you can rotate the image a specified number of degrees counterclockwise or rescale it to fit snugly inside a width-by-height bounding box.
Saving to a file. You save your image to a file using the File → Save menu option. You can also save a file programatically using the following method:
The supported image formats are JPEG and PNG. The filename must have either the extension .jpg or .png. We recommend using PNG for drawing that consist solely of geometric shapes and JPEG for drawings that contains pictures.
Clearing the canvas. To clear the entire drawing canvas, you can use the following methods:
The first method clears the canvas to white; the second method
allows you to specify a color of your choice. For example,
StdDraw.clear(StdDraw.LIGHT_GRAY) clears the canvas to a shade
of gray.
Computer animations and double buffering. Double buffering is one of the most powerful features of standard drawing, enabling computer animations. The following methods control the way in which objects are drawn:
By default, double buffering is disabled, which means that as soon as you
call a drawing
method—such as point() or line()—the
results appear on the screen.
When double buffering is enabled by calling enableDoubleBuffering(),
all drawing takes place on the offscreen canvas. The offscreen canvas
is not displayed. Only when you call
show() does your drawing get copied from the offscreen canvas to
the onscreen canvas, where it is displayed in the standard drawing window. You
can think of double buffering as collecting all of the lines, points, shapes,
and text that you tell it to draw, and then drawing them all
simultaneously, upon request.
The most important use of double buffering is to produce computer animations, creating the illusion of motion by rapidly displaying static drawings. To produce an animation, repeat the following four steps:
The clear(), show(), and pause(int t) methods
support the first, third, and fourth of these steps, respectively.
For example, this code fragment animates two balls moving in a circle.
StdDraw.setScale(-2, +2);
StdDraw.enableDoubleBuffering();
for (double t = 0.0; true; t += 0.02) {
double x = Math.sin(t);
double y = Math.cos(t);
StdDraw.clear();
StdDraw.filledCircle(x, y, 0.05);
StdDraw.filledCircle(-x, -y, 0.05);
StdDraw.show();
StdDraw.pause(20);
}
Keyboard and mouse inputs. Standard drawing has very basic support for keyboard and mouse input. It is much less powerful than most user interface libraries provide, but also much simpler. You can use the following methods to intercept mouse events:
The first method tells you whether a mouse button is currently being pressed. The last two methods tells you the x- and y-coordinates of the mouse's current position, using the same coordinate system as the canvas (the unit square, by default). You should use these methods in an animation loop that waits a short while before trying to poll the mouse for its current state. You can use the following methods to intercept keyboard events:
If the user types lots of keys, they will be saved in a list until you process them. The first method tells you whether the user has typed a key (that your program has not yet processed). The second method returns the next key that the user typed (that your program has not yet processed) and removes it from the list of saved keystrokes. The third method tells you whether a key is currently being pressed.
Accessing control parameters. You can use the following methods to access the current pen color, pen radius, and font:
These methods are useful when you want to temporarily change a control parameter and, later, reset it back to its original value.
Corner cases. Here are some corner cases.
null argument will throw an
IllegalArgumentException.
Double.NaN,
Double.POSITIVE_INFINITY, or Double.NEGATIVE_INFINITY
argument will throw an IllegalArgumentException.
Performance tricks. Standard drawing is capable of drawing large amounts of data. Here are a few tricks and tips:
enableDoubleBuffering() before
the sequence of drawing commands and call show() afterwards.
Incrementally displaying a complex drawing while it is being
created can be intolerably inefficient on many computer systems.
show()
only once per frame, not after drawing each individual object.
picture() multiple times with the same filename,
Java will cache the image, so you do not incur the cost of reading
from a file each time.
Known bugs and issues.
picture() methods may not draw the portion of the image that is
inside the canvas if the center point (x, y) is outside the
canvas.
This bug appears only on some systems.
Reference. For additional documentation, see Section 1.5 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 University's identity.
|
static Color |
RED
The color red.
|
static Color |
WHITE
The color white.
|
static Color |
YELLOW
The color yellow.
|
| Modifier and Type | Method and Description |
|---|---|
void |
actionPerformed(ActionEvent e)
This method cannot be called directly.
|
static 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).
|
static void |
circle(double x,
double y,
double radius)
Draws a circle of the specified radius, centered at (x, y).
|
static void |
clear()
Clears the screen to the default color (white).
|
static void |
clear(Color color)
Clears the screen to the specified color.
|
static void |
disableDoubleBuffering()
Disables double buffering.
|
static void |
ellipse(double x,
double y,
double semiMajorAxis,
double semiMinorAxis)
Draws an ellipse with the specified semimajor and semiminor axes,
centered at (x, y).
|
static void |
enableDoubleBuffering()
Enables double buffering.
|
static void |
filledCircle(double x,
double y,
double radius)
Draws a filled circle of the specified radius, centered at (x, y).
|
static 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).
|
static void |
filledPolygon(double[] x,
double[] y)
Draws a filled polygon with the vertices
(x0, y0),
(x1, y1), ...,
(xn–1, yn–1).
|
static void |
filledRectangle(double x,
double y,
double halfWidth,
double halfHeight)
Draws a filled rectangle of the specified size, centered at (x, y).
|
static void |
filledSquare(double x,
double y,
double halfLength)
Draws a filled square of the specified size, centered at (x, y).
|
static Font |
getFont()
Returns the current font.
|
static Color |
getPenColor()
Returns the current pen color.
|
static double |
getPenRadius()
Returns the current pen radius.
|
static boolean |
hasNextKeyTyped()
Returns true if the user has typed a key (that has not yet been processed).
|
static boolean |
isKeyPressed(int keycode)
Returns true if the given key is being pressed.
|
static 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.
|
static void |
line(double x0,
double y0,
double x1,
double y1)
Draws a line segment between (x0, y0) and
(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.
|
static 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.
|
static double |
mouseX()
Returns the x-coordinate of the mouse.
|
static double |
mouseY()
Returns the y-coordinate of the mouse.
|
static char |
nextKeyTyped()
Returns the next key that was typed by the user (that your program has not already processed).
|
static void |
pause(int t)
Pauses for t milliseconds.
|
static void |
picture(double x,
double y,
String filename)
Draws the specified image centered at (x, y).
|
static void |
picture(double x,
double y,
String filename,
double degrees)
Draws the specified image centered at (x, y),
rotated given number of degrees.
|
static 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.
|
static 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.
|
static void |
point(double x,
double y)
Draws a point centered at (x, y).
|
static void |
polygon(double[] x,
double[] y)
Draws a polygon with the vertices
(x0, y0),
(x1, y1), ...,
(xn–1, yn–1).
|
static void |
rectangle(double x,
double y,
double halfWidth,
double halfHeight)
Draws a rectangle of the specified size, centered at (x, y).
|
static void |
save(String filename)
Saves the drawing to using the specified filename.
|
static void |
setCanvasSize()
Sets the canvas (drawing area) to be 512-by-512 pixels.
|
static void |
setCanvasSize(int canvasWidth,
int canvasHeight)
Sets the canvas (drawing area) to be width-by-height pixels.
|
static void |
setFont()
Sets the font to the default font (sans serif, 16 point).
|
static void |
setFont(Font font)
Sets the font to the specified value.
|
static void |
setPenColor()
Sets the pen color to the default color (black).
|
static void |
setPenColor(Color color)
Sets the pen color to the specified color.
|
static void |
setPenColor(int red,
int green,
int blue)
Sets the pen color to the specified RGB color.
|
static void |
setPenRadius()
Sets the pen size to the default size (0.002).
|
static void |
setPenRadius(double radius)
Sets the radius of the pen to the specified size.
|
static void |
setScale()
Sets the x-scale and y-scale to be the default
(between 0.0 and 1.0).
|
static void |
setScale(double min,
double max)
Sets both the x-scale and y-scale to the (same) specified range.
|
static void |
setXscale()
Sets the x-scale to be the default (between 0.0 and 1.0).
|
static void |
setXscale(double min,
double max)
Sets the x-scale to the specified range.
|
static void |
setYscale()
Sets the y-scale to be the default (between 0.0 and 1.0).
|
static void |
setYscale(double min,
double max)
Sets the y-scale to the specified range.
|
static void |
show()
Copies offscreen buffer to onscreen buffer.
|
static void |
show(int t)
Deprecated.
replaced by
enableDoubleBuffering(), show(), and pause(int t) |
static void |
square(double x,
double y,
double halfLength)
Draws a square of the specified size, centered at (x, y).
|
static void |
text(double x,
double y,
String text)
Writes the given text string in the current font, centered at (x, y).
|
static 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.
|
static void |
textLeft(double x,
double y,
String text)
Writes the given text string in the current font, left-aligned at (x, y).
|
static void |
textRight(double x,
double y,
String text)
Writes the given text string in the current font, right-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_LIGHT_BLUE
public static final Color BOOK_RED
public static final Color PRINCETON_ORANGE
public static void setCanvasSize()
public static 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 static void setXscale()
public static void setYscale()
public static void setScale()
public static 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 static 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 static void setScale(double min,
double max)
min - the minimum value of the x- and y-scalesmax - the maximum value of the x- and y-scalesIllegalArgumentException - if (max == min)IllegalArgumentException - if either min or max is either NaN or infinitepublic static void clear()
public static void clear(Color color)
color - the color to make the backgroundIllegalArgumentException - if color is nullpublic static double getPenRadius()
public static void setPenRadius()
public static void setPenRadius(double radius)
radius - the radius of the penIllegalArgumentException - if radius is negative, NaN, or infinitepublic static Color getPenColor()
public static void setPenColor()
public static void setPenColor(Color color)
The predefined pen colors are
StdDraw.BLACK, StdDraw.BLUE, StdDraw.CYAN,
StdDraw.DARK_GRAY, StdDraw.GRAY, StdDraw.GREEN,
StdDraw.LIGHT_GRAY, StdDraw.MAGENTA, StdDraw.ORANGE,
StdDraw.PINK, StdDraw.RED, StdDraw.WHITE, and
StdDraw.YELLOW.
color - the color to make the penIllegalArgumentException - if color is nullpublic static 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 static Font getFont()
public static void setFont()
public static void setFont(Font font)
font - the fontIllegalArgumentException - if font is nullpublic static void line(double x0,
double y0,
double x1,
double y1)
x0 - the x-coordinate of one endpointy0 - the y-coordinate of one endpointx1 - the x-coordinate of the other endpointy1 - the y-coordinate of the other endpointIllegalArgumentException - if any coordinate is either NaN or infinitepublic static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 nullpublic static 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 nullpublic static 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 static 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 nullpublic static 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 nullpublic static 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 static 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 nullIllegalArgumentException - if x or y is either NaN or infinitepublic static 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 nullIllegalArgumentException - if x, y, or degrees is either NaN or infinitepublic static 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 nullIllegalArgumentException - if x or y is either NaN or infinitepublic static 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 nullIllegalArgumentException - if x or y is either NaN or infinite@Deprecated public static void show(int t)
enableDoubleBuffering(), show(), and pause(int t)t - number of millisecondspublic static void pause(int t)
t - number of millisecondspublic static void show()
public static void enableDoubleBuffering()
line(), circle(),
and square() will be deferred until the next call
to show(). Useful for animations.public static void disableDoubleBuffering()
line(), circle(),
and square() will be displayed on screen when called.
This is the default.public static void save(String filename)
.jpg or .png.filename - the name of the file with one of the required suffixesIllegalArgumentException - if filename is nullpublic void actionPerformed(ActionEvent e)
actionPerformed in interface ActionListenerpublic static boolean isMousePressed()
true if the mouse is being pressed; false otherwise@Deprecated public static boolean mousePressed()
isMousePressed()true if the mouse is being pressed; false otherwisepublic static double mouseX()
public static double mouseY()
public void mouseClicked(MouseEvent e)
mouseClicked in interface MouseListenerpublic void mouseEntered(MouseEvent e)
mouseEntered in interface MouseListenerpublic void mouseExited(MouseEvent e)
mouseExited in interface MouseListenerpublic void mousePressed(MouseEvent e)
mousePressed in interface MouseListenerpublic void mouseReleased(MouseEvent e)
mouseReleased in interface MouseListenerpublic void mouseDragged(MouseEvent e)
mouseDragged in interface MouseMotionListenerpublic void mouseMoved(MouseEvent e)
mouseMoved in interface MouseMotionListenerpublic static boolean hasNextKeyTyped()
true if the user has typed a key (that has not yet been processed
by nextKeyTyped(); false otherwisepublic static char nextKeyTyped()
hasNextKeyTyped() to ensure
that there is a next key to process.
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).NoSuchElementException - if there is no remaining keypublic static boolean isKeyPressed(int keycode)
This method takes the keycode (corresponding to a physical key)
as an argument. 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 key to check if it is being pressedtrue if keycode is currently being pressed;
false otherwisepublic void keyTyped(KeyEvent e)
keyTyped in interface KeyListenerpublic void keyPressed(KeyEvent e)
keyPressed in interface KeyListenerpublic void keyReleased(KeyEvent e)
keyReleased in interface KeyListenerpublic static void main(String[] args)
args - the command-line arguments