您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)

 

Content Index

Content Page # 6

Graphics context

The graphics context:

  • stores the current drawing attributes: line colour, style, thickness, text font, etc
  • manages the co-ordinate system

The 'graphics context' is not exclusively a Java concept, but is important in most computer systems that use a graphical user interface.

Why is the concept of a graphics context important?

When a program calls the method drawString()to display a line of text, it does not specify the font, the size or the colour. These are all part of the graphics context. Without the use of a graphics context, every call to drawString() would have to specify all these attributes. Now in some applications this might be appropriate (if the display was very complex), but usually most of the text in a window will be of the same colour, size, etc.

So if we wanted to draw three lines of text all in red, we would write something like this:

g.setColor (Color.red);

g.drawString ("This is the first line", 50, 50);

g.drawString ("This is the second line", 50, 70);

g.drawString ("This is the third line", 50, 90);

The first line sets the current colour to red, and the other lines all use that colour. If is possible for two graphics contexts to be active in the same part of the display, each with completely different attributes.

The other important feature of the graphics context is the management of the co-ordinate system. This is described in more detail later.

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg