Java Graphics  «Prev  Next»

Lesson 2(AWT) Abstract Windowing Toolkit
ObjectiveGet acquainted with the Abstract Windowing Toolkit.

Graphics and Abstract Windowing Toolkit (AWT)

Even though the AWT includes many different graphics classes, there is one class that is particularly important, that is the Graphics class.
The Graphics class represents an abstract drawing surface called a graphics context, which is logically equivalent to a piece of paper. Java uses graphics contexts to help abstract the drawing process. This abstraction allows you to use the same drawing code to draw to different graphical devices such as monitors and printers.
To draw graphics in Java, you must draw them on a graphics context using the Graphics class. This drawing typically takes place in an applet's paint() method, which is passed a Graphics object as its only parameter:

public void paint(Graphics g) {
}

Inside the Graphics class

The Graphics class contains a few attributes that dictate how certain types of graphics are drawn: color, background color, and font:

1) Drawing Graphics 1 2) Drawing Graphics 2 3) Drawing Graphics 3
  1. The color attribute specifies the color used by a Graphics object to draw lines and fill rectangles, among other things.
  2. The background color attribute specifies the background color of the graphic context onto which you are drawing.
  3. The font attribute specifies the font used to draw text onto the graphics context.

Java AWT
When you draw graphics to the screen, you are really drawing graphics to a component.
A component is a generic window represented by the Component class that forms the basis for all other graphical elements in the Java AWT.
The Abstract Window Toolkit (AWT) is Java's original platform-dependent windowing, graphics, and user-interface widget toolkit. The AWT is part of the Java Foundation Classes (JFC), the standard API for providing a graphical user interface (GUI) for a Java program. AWT is also the GUI toolkit for a number of Java ME profiles. For example, Connected Device Configuration profiles require Java runtimes on mobile telephones to support AWT.