Basics
- Java uses Graphics to perform a drawing (painting / rendering) operation.
- Every object that can be painted has a Graphics object that handles the painting of the object.
- Objects that can be painted, have a message method getGraphics() to get a hold of
their Graphics objects.
Important methods for color manipulation
Suppose
- g is a Graphics object;
- c is a Color;
- g.getColor() returns the current rendering color of its object.
- g.setColor( c ) set c as the current rendering color of its obect.
Important methods for drawing outlines
- Suppose
- g is a Graphics object;
- x, y, w, h, a1, a2, x1, y1, x2, and y2 are int values;
- xa and ya are lists (arrays) of int values;
- s is a String;
- g.drawLine( x1, y1, x2, y2 ) uses the current color to draw on its object, a line from (x1, y1) to (x2, y2).
- g.drawRect( x, y, w, h ) uses the current color to draw on its object, the outline of a rectangle with width w and height h, and with an upper left hand corner at location (x, y).
- g. drawString( s, x, y ) uses the current color to write on its object, string s at location (x, y).
- g.drawOval( x, y, w, h ) uses the current color to draw on its object, the outline of an oval of width w and height h at location (x, y). (The origin of an oval is the upper left hand corner of the smallest rectangle that can surround (bound) the oval).
- g.drawPolygon( xa, ya, n ) uses the current color to draw on its object, the outline of an n-pointed polygon whose endpoint coordinates are given by xa and ya.
- g.drawArc( x, y, w, h, a1, a2 ) uses the current color to paint on its object, an arc of width w and height h starting and ending angles a1 and a2, and with an upper left hand corner at location (x, y).
Important methods for painting regions
- Suppose
- g is a Graphics object;
- x, y, w, h, a1, a2, x1, y1, x2, and y2 are int values;
- xa and ya are lists (arrays) of int values;
- g. fillArc( x, y, w, h, a1, a2 ) uses the current color to paint on its object, a filled elliptical arc with width w, height h, starting angle a1, and ending angle a2, at location (x, y).
- g. fillOval(int x, int y, int w, int h) uses the current color to paint on its object, a filled oval of width w and height h at location (x, y).
- g.fillPolygon(int[] x, int[] y, int n) Renders the outline of a filled, closed polygon in the current color whose endpoint coordinates are specified by x and y.
- fillRect(int x, int y, int w, int h) uses the current color to paint on its object, a rectangular region with width w and height h, and with an upper left hand corner at location (x, y).
Important method for setting a rendering offset
- Suppose
- g is a Graphics object;
- dx and dy are int values;
- g. translate( dx, dy) moves the origin of its object by distances dx and dy respectively on the x- and y-axes.
Important method for rendering an image
- Suppose
- g is a Graphics object;
- x and y are int values;
- i is an Image;
- o is an ImageObserver.
- g.drawImage( i, x, y, o) paints on its object, image i at location (x, y) using observer o.