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 standard drawing, you must have StdDraw.class in your Java classpath. If you used our autoinstaller, you should be all set. Otherwise, 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 radius)
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 that is discussed in Chapter 3. Until then,
you can use this method with one of these predefined colors in standard drawing:
BLACK
, BLUE
, CYAN
, DARK_GRAY
, GRAY
,
GREEN
, LIGHT_GRAY
, MAGENTA
, ORANGE
,
PINK
, RED
, WHITE
, and YELLOW
.
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 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 ellipsoidal.
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:
You use the Font
data type to specify the font. 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.
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 width)
picture(double x, double y, String filename, double width, 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 inside a width-by-height pixel 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. Most often, these two methods are used in conjunction with animation mode.
Animations. Animation mode is one of the trickier features of standard drawing. The following two methods control the way in which objects are drawn:
By default, animation mode is off, which means that as soon as you
call a drawing
method—such as point()
or line()
—the
results appear on the screen. StdDraw.show()
turns off
animation mode.
You can call show(int t)
to turn on animation mode. This
defers all drawing to the screen until you are aready to display them.
Once you are ready to display them,
you call show(int t)
again, which transfer the offscreen
drawing to the screen and waits for the specified number of milliseconds.
In conjuction with clear()
, you can create the illusion
of movement by iterating the following three steps:
Waiting for a short while is essential; otherwise, the drawing will appear and disappear so quickly that your animation will flicker.
Here is a simple example of an animation:
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 reset it back to its original value.
Corner cases. To avoid clutter, the API doesn't explicitly refer to arguments that are null, infinity, or NaN.
null
argument will throw a
NullPointerException
.
Double.NaN
,
Double.POSITIVE_INFINITY
, and Double.NEGATIVE_INFINITY
as arugments. An object drawn with an x- or y-coordinate
that is NaN will behave as if it is outside the canvas, and will not be visible.
Performance tricks. Standard drawing is capable of drawing large amounts of data. Here are a few tricks and tips:
StdDraw.show(0)
before
and after the sequence of drawing commands.
The bottleneck operation is not drawing the geometric
shapes but rather drawing them to the screen. By using animation
mode, you draw all of the shapes to an offscreen buffer, then copy
them all at once to the screen.
show()
only once per frame, not after drawing each 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.
setFont()
in an animation loop (unless you really
need to change the font in each iteration). It can cause flicker.
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.
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 |
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 |
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 |
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 an ellipse with the specified semimajor and semiminor axes,
centered at (x, y).
|
static void |
filledPolygon(double[] x,
double[] y)
Draws a 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.
|
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()
Returns true if the mouse is being pressed.
|
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 |
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()
Set 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()
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 halfLength)
Draws a square of side length 2r, centered at (x, y).
|
static void |
text(double x,
double y,
String text)
Write the given text string in the current font, centered at (x, y).
|
static void |
text(double x,
double y,
String text,
double degrees)
Write 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)
Write the given text string in the current font, left-aligned at (x, y).
|
static void |
textRight(double x,
double y,
String text)
Write 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 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 width
and
height
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)
public 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)
public 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)
public static void clear()
public static void clear(Color color)
color
- the color to make the backgroundpublic static double getPenRadius()
public static void setPenRadius()
public static void setPenRadius(double radius)
radius
- the radius of the penIllegalArgumentException
- if radius
is negativepublic 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 penpublic 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 fontpublic 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 endpointpublic static void point(double x, double y)
x
- the x-coordinate of the pointy
- the y-coordinate of the pointpublic 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 negativepublic 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 negativepublic 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 negativepublic 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 negativepublic 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 negativepublic 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 negativepublic 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 negativepublic 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 negativepublic 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 negativepublic 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 lengthpublic 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 lengthpublic 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 invalidpublic 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 invalidpublic 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 pixelsscaledHeight
- the height of the scaled image in pixelsIllegalArgumentException
- if either scaledWidth
or scaledHeight
is negativeIllegalArgumentException
- if the image filename is invalidpublic 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 pixelsscaledHeight
- the height of the scaled image in pixelsdegrees
- 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 writepublic 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 counterclockwisepublic static void textLeft(double x, double y, String text)
x
- the x-coordinate of the texty
- the y-coordinate of the texttext
- the textpublic 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 writepublic static void show(int t)
line()
, circle()
, and square()
will not be displayed on screen until the next call to show()
.
This is useful for producing animations (clear the screen, draw a bunch of shapes,
display on screen for a fixed amount of time, and repeat). It also speeds up
drawing a huge number of shapes (call show(0)
to defer drawing
on screen, draw the shapes, and call show(0)
to display them all
on screen at once).t
- number of millisecondspublic static void show()
line()
, circle()
,
and square()
will be displayed on screen when called.
This is the default.public static 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 static boolean mousePressed()
public static double mouseX()
public static 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 static boolean hasNextKeyTyped()
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 pressedkeycode
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 static void main(String[] args)