Next page | Contents page |

Graphics context

Before Java 1.2, java.awt.Graphics objects represented drawing surfaces. Java 1.2 brought subclass java.awt.Graphics2D with greater capabilities, called Java 2D. A Graphics object provided by the system can be cast to Graphics2D in order to use full facilities.

A Graphics (or Graphics2D) object is used for on-screen and off-screen drawing. It retains the state of colour, font, clipping region, etc between calls. It provides all the methods for drawing, including rendering text fonts. It is called a graphics context.

We mention this now because our Window application doesn't just want to pop up dialogues all the time: it has its own window into which we could draw shapes and write text if we knew how...

Coordinate systems

The origin for coordinates is top left and the y value increases downwards from there. Historically that is because lines are displayed on monitors scanning downwards.

For drawing on screen the units are pixels. For printers the units are points (72 per inch).

In awt, coordinates are limited to integer values but Swing allows float, for greater precision. Swing also allows coordinate space conversion for high resolution printers.

Colours

An RGB colour comprises amounts of red, green and blue, each on a scale from 0 to 255 (or a float from 0.0 to 1.0). Colour constants and methods for creating other colours are in java.awt.Color (note American spelling) - see the API documentation.

java.awt.Component

java.awt.Component has method public void paint (Graphics g) which is called by the system whenever it is necessary to redraw any portion of the component. NB: DO NOT call paint () directly.

Also repaint (), for an application to ask for painting to occur. Overloaded repaint (x, y, width, height) causes a given rectangle to be repainted. repaint () first calls update () which first clears the component and then calls paint (). For animations it can be worth overriding update () so it only calls paint (), otherwise the clearing can cause unwanted flashing.

Whatever needs drawing is put inside paint () which we override. In there we have access to the Graphics object, g. We can cast g to Graphics2D to make it fully functional for Java 2D:

	@Override
	public void paint (java.awt.Graphics g)
	{
		java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
		// ... then use methods of g2 for drawing

Basic drawing methods

Draw outlines of shapes:

awt: drawLine (), drawRect (), drawRoundRect (), drawOval (), drawArc (), drawPolyline (), drawPolygon ()

Swing: draw () with a shape parameter

Fill interiors of shapes:

awt: fillRect (), fillRoundRect (), fillOval (), fillArc (), fillPolygon (), clearRect ()

Swing: fill () with a shape parameter

Draw text: drawString ()

Draw image: drawImage ()

Text & fonts

Given Graphics g:

	Font f = new Font ("Times New Roman", 36); // 36 = Point size
	g.setFont (f);
	FontMetrics fm = new FontMetrics (f);
	Rectangle r = fm.getStringBounds ("My message", g);

The Rectangle object enables you to work out exactly where to position the text to centre it, by giving suitable x and y parameters:

	g.drawString ("My message", x, y);

(Font, FontMetrics, Graphics and Rectangle are all in java.awt)

Next page | Contents page |