Home : Course Map : Chapter 6 : Java : Supplements :
Basics Graphics - AWT
JavaTech
Course Map
Chapter 6

Introduction
AWT
Swing
Containers
  Demo 1
UI Components
  Demo 2
UI Layout
  Demo 3   Demo 4
Text Display
  Demo 5
Drawing
  Demo 6   Demo 7
Draw Polygons
  Demo 8   Demo 9
Colors
Text Draw 
  Demo 10
Images
  Demo 11
Exercises

    Supplements
AWT
  Demo 1
Drawing
  Demo 2
Text Drawing
  Demo 3
UI Components
  Demo 4

Java2D
Shapes & Areas
  Demo 1   Demo 2
Stroke & Paint
  Demo 3
Transforms
  Demo 4
Gradients&Textures
  Demo 5   Demo 6
Text
  Demo 7   Demo 8
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

Java graphics build on top of the java.awt.* packages (AWT - Another Windows Toolkit). More sophisticated and elaborate graphics came with version Java 1.2 (the javax.swing.* packages that we discuss in the main track) but they essentially extend the AWT.

In this section we give a brief introduction to purely AWT graphics techniques. This provides a background for the javax.swing graphics but also, since many browsers still provide only a Java 1.1 level JVM, you might want to program some of your applets with these techniques.

Components

The basic building block of Java graphics is the abstract Component class. (The javax.swing components are also sub-classes of Component). Concrete subclasses of Component include :.

  • Container provides for holding other sub-components.
  • Panel is a container sub-class that can provide a drawing surface but generally is intended for holding other components such as buttons and labels.
  • LayoutManager subclasses arrange the components, such as placing a group of buttons in a n x m grid arrangement.
  • Canvas is a component usually placed in a panel and used for drawing graphics.

The java.applet.Applet class is a sub-class of Panel as shown by this inheritance chart:

java.lang.Object
    |
    +--java.awt.Component
             |
             +--java.awt.Container
                      |
                      +--java.awt.Panel
                             |
                             +--java.applet.Applet

Like all Component subclasses, the applet overrrides the paint(Graphics g) method and it is usually where drawing commands are put. For example, :

import java.applet.Applet;
import java.awt.Graphics;

/** Example demonstrating drawPolyline(). **/
public class
SimpleDrawApplet extends Applet
{
  public void paint (Graphics g) {
    g.drawRect (13,13,24,24);
    g.drawOval (15,15,35,35);
  }
} // class
SimpleDrawApplet

To display a component, the JVM graphics engine will invoke the paint method (or indirectly via an invokation of the update method, which we will discuss later). If the component is a Container sub-class, such as panel, then it in turn will call the paint methods of all the components that it holds. Note that all the components have paint() commands but typcially, you only override paint(Graphics) for Panel and Canvas classes.

Graphics Context

The paint(Graphics g)method includes an instance of the Graphics class as an argument . The graphics context represents a drawing surface and all of its graphics settings such as the current foreground and background colors, the fonts to be used for strings, etc. graphical user interface (GUI). The class is referred to as the graphics context class since it provides the context under which all graphics commands operate.

The graphics context does not need to represent a visible component but can be an off-screen image as we will see later in the double buffering discussion.

Graphics Commands

Here are some of the drawing methods in the Graphics class that

  • drawLine(int x1, int y1, int x2, int y2)
    Draw a line between points (x1,y1) and (x2,y2)

  • drawArc(int x,int y,int width, int height,
            int startAngle, int arcAngle)
  • setColor(Color c)
    Set the current pen color, where c has several standard choices such as
    Color.black, Color.blue, Color.red
    , etc.

  • fillRect(int x, int y, int width, int height)
    Fills the specified rectangle with the current color.

See the API Specifications for other methods in the Graphics class.

Note that the the coordinate system goes as follows:

  • Origin (0,0)- top left hand corner
  • x (in pixels) - increases towards the right
    • Maximum x = width - 1
  • y (in pixels) - increases towards the bottom
    • Maximum y = height -1

where width and height values come from the getSize() method, which returns an instance of the Dimension class, and getWidth() and getHeight() methods, which became available as of version 1.2.

So, for example in the instruction

   drawRect(x,y,width,height)

(x,y) are the coordinates of the top left corner and the bottom right corner will be at (x+width,y+height).

Similarly,

  drawOval(x,y,width,height)

draws an oval bounded by the rectangle specified by these arguments.

 

Latest update: Oct. 27, 2004

            Tech
Java Tech Graphics
Starting to Plot
  Demo 1
Drawing Panel
  Demo 2
Histogram Display

  Demo 3
Exercises

           Physics
Display Text Data
  Demo 1
Plot Data
  Demo 2
Find Max/Min
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.