Java Graphics
and
Applets
Java Graphics
Two main Graphics libraries:
java.awt (Abstract Window Toolkit)
older
major versions:
1.0: Old original, accepted by most web browsers
1.1: Accepted by Internet Explorer, but not yet entirely by    Netscape
java.swing
newer
more portable (less sensitive to window system)
might not be support on your browser, without plugins

We will use 1.0 awt
relatively simple to use
works on most browsers
will generate a ÒdeprecationÓ warning when compiling with Java 1.2:
Usually this can be ignored.
1.1 graphics avoids the warnings, but will not run on all browsers

class Frame
Drawing on Objects
In order to draw in a frame or other objects, an Object of class
Graphics
must first be obtained for the frame. This is referred to as a graphics context. This is done using the call
getGraphics()
which returns a Graphics object.

class Graphics
Think of a Graphics object as being like a canvas on which drawing takes place.
The actual display of what is drawn is usually done behind the scenes, without giving an explicit command to display the Graphics object.
Graphics objects have other uses too, e.g. printing.

A Simple Graphics Program
Desired Result
Slide 9
Slide 10
Typical awt Drawing Methods
void drawLine(int x1, int y1, int x2, int y2)
void drawRect(int x, int y, int width, int height)
void drawOval(int x, int y, int width, int height)
void drawPolygon(int[] xPoints, int[] yPoints,
                 int nPoints)
void drawArc(int x, int y, int width, int height,
                int startAngle, int arcAngle)
void drawString(String str, int x, int y)
void drawImage(Image img, int x, int y,
               Color bgcolor, ImageObserver observer)
Blue bullets also have fill instead of draw.

Frame Buffering
I can almost guarantee
this will be somewhat confusing.
Follow the lead of canned examples when in doubt, which may  be most of the time.
This is a legacy from the Java designers.

paint() method
Flicker Prevention 1
Clearing the background in update() can create lots of flicker in the application.
It is customary to over-ride update() in the customized frame as follows:
public void update(Graphics g)
  {
  paint(g);
  }

Flicker Prevention 2
Instead of painting the background, then drawing on it, it is better to ÒpaintÓ a complete image covering the visible area of the Frame.
This image is known as an

off-screen buffer

because the buffer is not part of the Frame.
The buffer is drawn on prior to update().
The buffer contents is then painted in update().

Slide 16
Flicker Prevention Summary
repaint()
When the programmer wants to force repainting of the Frame, she calls
repaint()
This causes the system to schedule a call to update().
repaint() has no arguments.
The programmer normally does not call paint() directly, outside of update().

repaint() -> paint()
Applets
ÒAppletÓ means Òsmall applicationÓ
Run in one of two ways:
In a web browser
Using a program appletviewer
Applet runs in a specialized Frame (type of window)
No main(); instead: init(), run().

Viewing Applets on the Web or using appletviewer
Web Applet Restrictions
CanÕt load files on server or client
Can only load content of URLÕs, and then only from the same server that contains the applet code

Examples
appletGraphics example on our web pages
and others