Applets and GUI Libraries

Two Java libraries java.applet and java.awt provide the mechanisms for building graphical user interfaces (GUI) and applets. GUI's allow programs to communicate with users using bitmap displays and gestures with a mouse (as opposed to a textual, command-line approach). Applets are GUI applications that are part of HTML documents and are delivered over the internet using WWW browsers.

The java.applet library is small; to build applets requires not only the applet library, but also the awt library--the abstract window toolkit. The java.awt is large, with over 42 classes.

Applet Overview

An applet is not a typical, self-contained main program under control of the user. Rather it is invoked by a WWW browser and is under its control. The WWW browser allocates a rectangular region of the document to the applet.

HelloApplet

Here the source code for a simple applet. When run by a WWW browser it displays a message in the space allocated by the browser.
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;

public class HelloApplet extends Applet {

   public void init() {
      setBackground (Color.white); // Make applet window stand out
   }

   public void paint(Graphics g) {
      g.drawString ("Hello world!", 40, 20);
   }

}

Applet running in browswer

Applet running in appletviewer

For purposes of developing and debugging applets it is convenient to have a program that just runs applets--a stripped down version of a browser. The appletviewer is such a program.

Applet Basics

An applet is embedded in an HTML document much like other elements. The document controls the allocation of a rectangular area for the applet using the applet tag.

The "applet" tag

An example:
<applet code="ConicApplet" width=400 height=400></applet>

Attributes

The attributes width and height are required and contol the size in pixels of the rectangle allocated by the browser to contain the applet.

ALIGN tag controls "flow"

The attribute align controls the way the main body of the text flows around the applet in the same was as the HTML img tag. The possible values for the attribute are: top, bottom, left, and right.

Passing parameters to applets

The HTML param tag and be used in an applet tag to pass information to the Java applet.
import java.applet.Applet;
import java.awt.*;

public class Text extends Applet {
   private String text;

   public void init() {
      setBackground (Color.white); // Make applet window stand out
      text = getParameter("text");
      if (text == null) {
         text = "Hello world!";
      }
   }

   public void paint (Graphics g) {
      // Put text in the center of the window
      Dimension d = this.size();   // Get the size of the window
      FontMetrics m = g.getFontMetrics();
      int a = m.getAscent();
      int baseline = a + (d.height - (a + m.getDescent()))/2;
      g.drawString (text, (d.width-m.stringWidth(text))/2, baseline);
   }

}

Java Class: Applet

Applet lifecycle and methods

Introduction to GUI

Fonts

Dialog
Helvitica
TimesRoman
Courier
DialogInput
ZapfDingbats
style:  PLAIN, BOLD, ITALIC, Font.BOLD|Font.ITALIC

Color

Constants in class Color:
    Color white     = new Color(255, 255, 255);
    Color lightGray = new Color(192, 192, 192);
    Color gray      = new Color(128, 128, 128);
    Color darkGray  = new Color(64, 64, 64);
    Color black     = new Color(0, 0, 0);
    Color red       = new Color(255, 0, 0);
    Color pink      = new Color(255, 175, 175);
    Color orange    = new Color(255, 200, 0);
    Color yellow    = new Color(255, 255, 0);
    Color green     = new Color(0, 255, 0);
    Color magenta   = new Color(255, 0, 255);
    Color cyan      = new Color(0, 255, 255);
    Color blue      = new Color(0, 0, 255);
brighter is RGB / 0.7  (ie., 1.42 * RGB)
  darker is RGB * 0.7

Coordinate System

coordinate system

Shapes

java.awt.Graphics

public abstract class Graphics extends Object {
   public void draw3DRect (int x, int y, int width, int height, boolean raised)
   public void drawArc (int x, int y, int width, int height, int startAngle, int arcAngle)
   public void drawLine (int x1, int y1, int x2, int y2)
   public void drawOval (int x, int y, int width, int height)
   public void drawPolygon (int[] xs, int[] ys, int n)
   public void drawRect (int x, in y, int width, int height)
   public void drawString (string str, int x, in y)
   public void drawImage (image img, int x, int y, int width, int height, ImageObserver obs

drawing rectangle

Images

GUI building with java.awt

paint

The method paint of class Component is called under three circumstances:
  1. whenever you're component is resized (in particular, when it is first mapped to the screen),
  2. whenever the component is fully or partially exposed (that is, when a window that was covering it is moved out of the way), and
  3. if you don't override the component's update method, paint will get called whenever the update method gets called (that is, whenever you call repaint()).

It is usually better to call repaint() then to call paint() directly, because if you call repaint() 10 times before the screen updater actually gets around to doing the painting, your paint method will only get called once, which is more efficient.

Components

Button, Checkbox, Choice, Label, List, TextComponent, Scrollbar, Canvas
MenuComponent: MenuBar, MenuItem
Container:  Panel, Window

Component Hierarchy

Layout

layout classes

Classes BorderLayout, CardLayout, FlowLayout, GridBagLayout, GridLayout implement interface LayoutManager.

Border Layout

border layout

Anchor in GridBag Layout

grid bag anchor

Fill in GridBag Layout

grid bag fill

Events

Convenience Methods

Called by default event handler in the class java.awt.Component.

handle event

Default behavior of handleEvent()

Action

One of the convenience methods
public boolean action (Event e, Object arg)
List of all ways "action" event generate and meaning of the argument.
Component Class     Argument Type   argument value
Button              String          button label
Checkbox            Boolean         checkbox state (after)
Choice              String          menu choice
List                String          list item
TextField           String          field contents

Examples

Mu Puzzle

Conic Section


Ryan Stansifer <ryan@cs.fit.edu>
Last modified: Wed Sep 9 09:00:24 EDT 1998