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

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)

 

Content Index

Content Page # 4

‘Graphics’ class

A Graphics object has two important functions: 

  • To store the current graphics context (see later this unit)
  • To provide primitive (basic) drawing methods. 

Any AWT object can call getGraphics() to get a Graphics object. A Graphics object is passed to the paint() method. 

The Graphics class is very important in applications that draw graphical figures. It contains a large number of methods for drawing lines, shapes, text, and images. In addition, a Graphics object stores the current graphics context (described later in this unit).

The most common way of using a Graphics object — one that you have seen already — is to use the method paint() in an applet. The Java system automatically provides a Graphics object to this method. The program can call any of the methods in the object to do whatever drawing it needs. The paint() method is normally defined like this:

public void paint (Graphics g)
{
  // drawing statements go here…
}

We don't have to call the object g (in fact, this is a rather meaningless name), but it has become conventional to do so.

You will already have seen at least one method in the Graphics class: the drawString() method. This displays a String of text at a specific position.

Back to top

basicline.gif (169 bytes)

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