In: Computer Science
How does one use the standard draw class?
The 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:
point(double x, double y)
line(double x1, double y1, double x2, double
y2)
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)