Graphics Context
Home ] Up ] JFrame Internal Structure ] [ Graphics Context ] Displaying Text ] Text & Fonts ] Mixing Fonts ] Finding & Displaying Fonts ]

 

 

Before we go any further, one concept we need to understand is that of a graphics context, which in Java is represented by the Graphics class.

What is a graphics context?

Basically, it provides a context within which you can draw.  As the Graphics class documents (see the Java API javadocs), it encapsulates state information needed for the basic rendering operations that Java supports. This state information includes the following properties:

  • The Component object on which to draw.
  • A translation origin for rendering and clipping coordinates.
  • The current clip.
  • The current color.
  • The current font.
  • The current logical pixel operation function (XOR or Paint).
  • The current XOR alternation color (see later).

We will see how you can draw inside Java Swing frames, panels, and other GUI components.  If you ever venture into printing from Java, you will find the same concept there, except that the context is focused not on a GUI window, but on a printable page.  We won't get into printing from Java in this course -- that's a more advanced topic.

For drawing on the screen, Java originally provided only the Graphics class, which is actually an abstract base class, from which all graphics context classes extend.  You can see what it (or rather, a class that extends it) provides by going to its javadoc page.

Java 1.2 and beyond adds the Java 2D library, which augments the ability to draw to a considerable degree.  It uses Graphics2, which is another abstract class that extends the Graphics class.  You can find the details at its javadoc page.

Graphics2 provides much more sophisticated control over geometry, coordinate transformations, color management, and text layout.  Thoroughly covering the Java 2D library could take up an entire semester course, so we won't be going into detail on Java 2D.  We may show some examples of Java 2D later.

We'll be seeing the use of a graphics context in lots of subsequent drawing examples.

 

This page was last modified on 02 October, 2007