7. AWT Graphics

7.1. Graphics Object

7.1.1. Definition

Graphics objects are the key to all drawing. They support the two basic kinds of drawing: primitive graphics (such as lines, rectangles, and text) and images. They also provide a drawing context by maintaining state such as the current drawing area and the current drawing color.

7.1.2. The Coordinate System

Figure 10.34. Coordinate System

Coordinate System

[Note]Note

Each Component has its own integer coordinate system, ranging from (0, 0) to (width - 1, height - 1).

7.1.3. Drawing Shapes

The Graphics class defines methods for drawing the following kinds of shapes:

  • Lines: drawLine(), which draws a line in the Graphics object's current color, which is initialized to the Component's foreground color.

  • Rectangles: drawRect(), fillRect(), and clearRect() -- where fillRect() fills a rectangle with the Graphics object's current color, and clearRect() fills a rectangle with the Component's background color.

  • Raised or lowered rectangles: draw3DRect() and fill3DRect().

  • Round-edged rectangles: drawRoundRect() and fillRoundRect().

  • Ovals: drawOval() and fillOval().

  • Arcs: drawArc() and fillArc().

  • Polygons: drawPolygon() and fillPolygon().

Audio in Portuguese

7.1.4. A Shape Sampler

Figure 10.35. Shape Sampler

Shape Sampler

Code:

Color bg = getBackground();
Color fg = getForeground();
. . .
// drawLine()
g.drawLine(x, y + rectHeight, x + rectWidth, y); // x1, y1, x2, y2
. . .
// drawRect()
g.drawRect(x, y, rectWidth, rectHeight); // x, y, width, height
. . .
// draw3DRect() 
g.setColor(bg);
g.draw3DRect(x, y, rectWidth, rectHeight, true);
g.setColor(fg);
. . .
// drawArc() 
g.drawArc(x, y, rectWidth, rectHeight, 90, 135); // x, y, w, h
. . .
// drawPolygon() 
Polygon polygon = new Polygon();
polygon.addPoint(x, y);
polygon.addPoint(x+rectWidth, y+rectHeight);
polygon.addPoint(x, y+rectHeight);
polygon.addPoint(x+rectWidth, y);
//polygon.addPoint(x, y); //don't complete; fill will, draw won't
g.drawPolygon(polygon); 
. . .
// fillRect() 
g.fillRect(x, y, rectWidth, rectHeight); // x, y, width, height
. . .
// fill3DRect() 
g.setColor(bg);
g.fill3DRect(x, y, rectWidth, rectHeight, true);
g.setColor(fg);
. . .
// fillArc() 
g.fillArc(x, y, rectWidth, rectHeight, 90, 135); // x, y, w, h
. . .
// fillPolygon() 
Polygon filledPolygon = new Polygon();
filledPolygon.addPoint(x, y);
filledPolygon.addPoint(x+rectWidth, y+rectHeight);
filledPolygon.addPoint(x, y+rectHeight);
filledPolygon.addPoint(x+rectWidth, y);
//filledPolygon.addPoint(x, y);
g.fillPolygon(filledPolygon); 

Audio in Portuguese

7.1.5. DrawingText

Figure 10.36. Drawing Text

Drawing Text

7.1.5.1. Graphics Methods for drawing text
public void drawString(String data, int x, int y);
public void drawChars(char data[], int offset, int length, int x, int y);
public void drawBytes(byte data[], int offset, int length, int x, int y);
[Note]Note

For the text drawing methods, x and y are integers that specify the position of the lower left corner of the text. To be precise, the y coordinate specifies the baseline of the text.

7.1.5.2. FontMetrics objects are used to get information about a font
boolean textFits = false;
Font font = g.getFont();
FontMetrics fontMetrics = g.getFontMetrics();
while (!textFits) {
    if ((fontMetrics.getHeight() <= maxCharHeight) &&
        (fontMetrics.stringWidth("drawRoundRect()") <= gridWidth)) {
        textFits = true;
    } else {
        g.setFont(font = new Font(font.getName(), font.getStyle(),
                                  font.getSize() - 1));
        fontMetrics = g.getFontMetrics();
    }
}

Methods that return information about a font's vertical size:

getAscent(), getMaxAscent(), getDescent(), getMaxDescent(), getHeight(), getLeading()

Audio in Portuguese