Standard Graphics
Home Up Layout Managers Standard Graphics Images Event Handling UI Components

 

This page summarizes some of the methods that are useful for performing simple graphical operations.

Useful Tricks

int height = getSize().height;    // returns the height of the display window in pixels
int width = getSize().width;      // returns the width of the display window in pixels

setForeground(Color.whatever);    // sets the drawing color to whatever (use in init)
setBackground(Color.whatever);    // sets the background of the display window to whatever              
g.setColor(Color.whatever);         // sets the drawing color to whatever (use in paint)

Colors

Colors can be invoked in one of two ways.  First, one can use one of the 13 predefined colors in statements such as setBackground(Color.black);.   The 13 predefined colors are: black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, and yellow.

Second, one can define a custom color by specifying the red, green, and blue values in ranges from 0 through 255.  For example, setBackground(new Color(0, 255, 0)); sets the background color to green.

There are many other methods that support the "Color" type that the interested reader might want to explore independently.

Lines

g.drawLine(int x1, int y1, int x2, int y2);

The "drawLine" method must be invoked in the graphics context.  (x1, y1) is the coordinate of one end of the line, (x2, y2) is the coordinate of the other end.   Recall that the top left corner of the display window is designated by (0, 0).

Arcs

g.drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) ;
g.fillArc (int x, int y, int width, int height, int startAngle, int arcAngle);

The reader must independently learn about arcs.

Rectangles

g.drawRect(int x, int y, int width, int height);
g.fillRect(int x, int y, int width, int height);

The difference between "draw" and "fill" is that the former draws only the boundaries of a rectangle while the latter produces a solid rectangle.  The arguments are the (x,y) coordinate of the upper, left corner of the rectangle, and the "width" and the "height" of the rectangle.

g.draw3DRect(int x, int y, int width, int height, boolean raised);
g.fill3DRect(int x, int y, int width, int height, boolean raised);

A 3-dimension rectangle differs from a regular one in that it has a 3-dimensional appearance.  The 3-d effect can come from either raising (raised = true) or lowering (raised = false) the rectangle.

g.drawRoundRect(int x, int y, int width, int height, int arcWidthDegree, int arcHeightDegree)
g.fillRoundRect(int x, int y, int width, int height, int arcWidthDegree, int arcHeightDegree)

A rounded rectangle differs from a regular one in that both the horizontal and vertical sides of the rectangle can be curved to an arbitrary degree.  The "arcWidthDegree" indicates how many degrees to curve the horizontal lines of the rectangle and the "arcHeightDegree" indicates how many degrees to curve the vertical lines of the rectangle.

Ovals

g.drawOval(int x, int y, int width, int height);
g.fillOval(int x, int y, int width, int height);

An oval is specified by the (x,y) coordinates of the bounding rectangle as well as the width and the height of the bounding rectangle.

Polygons

g.drawPolygon(int xPoints[], int yPoints[], int nPoints);
g.fillPolygon(int xPoints[], int yPoints[], int n Points);

Polygons are specified by an array of x points, a corresponding array of y points, and an integer that specifies the number of points that make up the polygon.  The polygon is a closed polygon, so a line appears between the nth pair of points and the first pair of points.

Fonts and Text

g.drawString(String str, int x, int y);

This method draws a string at the specified (x, y) coordinate using the currently specified font.  A font can be changed via

g.setFont(new  Font(String name, int style, int size));

where name can take on one of the following five values: "SansSerif", "Serif", "Monospaced", "Dialog", "DialogInput", style can take on one or more of the following values: Font.BOLD, Font.ITALIC, Font.PLAIN (combinations are formed using a +), and size indicates the point size of the text (12 is common).