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.
![]() | Note |
---|---|
Each |
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().
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);
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 |
---|---|
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. |
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()
Copyright © 1998-2009 Dilvan Moreira