Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Monday, November 19, 2007

explain java.awt.Graphics Class - Basic drawing methods

Draw on a JComponent or JPanel by overriding paintComponent(). Assume g is a Graphics object, and all variables are type int unless otherwise declared. Angles (int startAngle, arcAngle) are in degrees counterclockwise from 3 o'clock. These methods use (x,y) at the top, left, corner and a width and height of the bounding box (except drawString() and drawLine()).

g.drawLine(x1, y1, x2, y2);
g.drawRect(x, y, width, height); // (x,y) is upper left corner
g.fillRect(x, y, width, height);
g.drawOval(x, y, width, height);
g.fillOval(x, y, width, height);
g.drawArc( x, y, width, height, startAngle, arcAngle);
g.fillArc( x, y, width, height, startAngle, arcAngle);
g.drawString(String s, x, y); // Draws s with the left base at (x,y)

g.setColor(Color c); // All drawing after this uses the Color c.
g.setFont(Font f); // All drawing after this uses the Font f.

g.drawPolyline(int[] xPoints, int[] yPoints, nPoints); // Draws line
g.drawPolygon( int[] xPoints, int[] yPoints, nPoints); // Draws polygon
g.drawPolygon( poly); // draws polygon, same for fillPolygon.
g.fillPolygon( int[] xPoints, int[] yPoints, nPoints); // Fills polygon

No comments:

Post a Comment

Archives