Graphics Functions
Graphics Functions
h
This interface provides access to a simple graphics library that makes it possible to draw lines,
rectangles, ovals, arcs, polygons, images, and strings on a graphical window.
Functions
initGraphics() Creates the graphics window on the
initGraphics(width, height) screen.
drawArc(bounds, start, sweep) Draws an elliptical arc inscribed in
drawArc(x, y, width, height, start, sweep) a rectangle.
fillArc(bounds, start, sweep) Fills a wedge-shaped area of an
fillArc(x, y, width, height, start, sweep) elliptical arc.
drawImage(filename, pt)
Draws the image from the specified
drawImage(filename, x, y)
drawImage(filename, bounds) file with its upper left corner at the
drawImage(filename, x, y, width, height) specified point.
getColor()
Returns the current color as a string
in the form "#rrggbb".
Function detail
void initGraphics();
void initGraphics(int width, int height);
Creates the graphics window on the screen. The first form creates a window with a default size
of 500x300; the second allows the client to specify the size of the window. The call
to initGraphics must precede any console output or calls to other functions in this interface.
Usage:
initGraphics();
initGraphics(width, height);
Usage:
Usage:
Usage:
drawImage(filename, pt);
drawImage(filename, x, y);
drawImage(filename, bounds);
drawImage(filename, x, y, width, height);
Usage:
Usage:
drawLine(p0, p1);
drawLine(x0, y0, x1, y1);
Usage:
Usage:
drawOval(bounds);
drawOval(x, y, width, height);
Usage:
fillOval(bounds);
fillOval(x, y, width, height);
Usage:
drawRect(bounds);
drawRect(x, y, width, height);
Usage:
fillRect(bounds);
fillRect(x, y, width, height);
Usage:
drawPolygon(polygon);
drawPolygon(polygon, pt);
drawPolygon(polygon, x, y);
Usage:
fillPolygon(polygon);
fillPolygon(polygon, pt);
fillPolygon(polygon, x, y);
Usage:
drawString(str, pt);
drawString(str, x, y);
Usage:
Usage:
setFont(font);
string getFont();
Returns the current font.
Usage:
The case of the individual letters in the color name is ignored, as are spaces and underscores, so
that the Java color DARK_GRAY could be written as "Dark Gray".
setColor(color);
string getColor();
Returns the current color as a string in the form "#rrggbb". In this string, the values rr, gg,
and bb are two-digit hexadecimal values representing the red, green, and blue components of the
color, respectively.
Usage:
void saveGraphicsState();
Saves the state of the graphics context. This function is used in conjunction
with restoreGraphicsState() to avoid changing the state set up by the client.
Usage:
saveGraphicsState();
void restoreGraphicsState();
Restores the graphics state from the most recent call to saveGraphicsState().
Usage:
restoreGraphicsState();
double getWindowWidth();
Returns the width of the graphics window in pixels.
Usage:
double getWindowHeight();
Returns the height of the graphics window in pixels.
Usage:
void repaint();
Issues a request to update the graphics window. This function is called automatically when the
program pauses, waits for an event, waits for user input on the console, or terminates. As a result,
most clients will never need to call repaint explicitly.
Usage:
repaint();
Usage:
pause(milliseconds);
void waitForClick();
Waits for a mouse click to occur anywhere in the window.
Usage:
waitForClick();
Usage:
setWindowTitle(title);
string getWindowTitle();
Returns the title of the primary graphics window.
Usage:
void exitGraphics();
Closes the graphics window and exits from the application without waiting for any additional
user interaction.
Usage:
exitGraphics();