Abstract Window Toolkits (AWT) and Applet

(Visit http://java.sun.com/j2se/1.5.0/docs/api/ if you need any help.)

Major Graphic User Interface (GUI) Objects


Class Hierarchy in Package java.awt

                        java.lang.Object                    
       -------------------------------------------------------
          /                   |                   \ 
         /                    |                    \
        /                     |                  java.awt.Graphics (abstract)
       /                      |                  java.awt.Color
java.util.EventObject   java.awt.Component       java.awt.Font
     /                   (abstract)              java.awt.Image
    /                      /     \               java.awt.BorderLayout
   /                      /       \              java.awt.MenuComponent
  /                      /         \
 /                      /           \ 
java.awt.AWTEvent    Container    Button,Canvas,Checkbox,Choice, 
                     (abstract)   Label,List,Scrollbar,TextComponent
                      /   \       Label,List,Scrollbar,   TextComponent
                     /     \                                  /  \
                    /       \                                /    \
                 Panel    Window                            /      \
                  /           \                            /        \ 
                 /             \                          /          \
                /             Frame                   TextField     TextArea
               /   
           Applete       
     (not in the package)

Contains all of the classes for creating user interfaces and for painting graphics and images.


Major Classes in Package Java.awt


AWTEvent The root event class for all AWT events.
Button This class creates a labeled button.
BorderLayout
A border layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center.
Canvas A Canvas component represents a blank rectangular area of the screen onto which the application can draw or from which the application can trap input events from the user.
Checkbox A check box is a graphical component that can be in either an "on" (true) or "off" (false) state.
CheckboxGroup The CheckboxGroup class is used to group together a set of Checkbox buttons.
Color The Color class is used to encapsulate colors in the default sRGB color space or colors in arbitrary color spaces identified by a ColorSpace.
Component A component is an object having a graphical representation that can be displayed on the screen and that can interact with the user.
Container A generic Abstract Window Toolkit(AWT) container object is a component that can contain other AWT components.
Font The Font class represents fonts, which are used to render text in a visible way.
FlowLayout
A flow layout arranges components in a directional flow, much like lines of text in a paragraph.
Frame A Frame is a top-level window with a title and a border.
Graphics The Graphics class is the abstract base class for all graphics contexts that allow an application to draw onto components that are realized on various devices, as well as onto off-screen images.
Image The abstract class Image is the superclass of all classes that represent graphical images.
Label A Label object is a component for placing text in a container.
List The List component presents the user with a scrolling list of text items.
Menu A Menu object is a pull-down menu component that is deployed from a menu bar.
MenuBar The MenuBar class encapsulates the platform's concept of a menu bar bound to a frame.
MenuComponent The abstract class MenuComponent is the superclass of all menu-related components.
MenuItem All items in a menu must belong to the class MenuItem, or one of its subclasses.
Panel Panel is the simplest container class.
Polygon The Polygon class encapsulates a description of a closed, two-dimensional region within a coordinate space.
Rectangle A Rectangle specifies an area in a coordinate space that is enclosed by the Rectangle object's top-left point (xy) in the coordinate space, its width, and its height.
Scrollbar The Scrollbar class embodies a scroll bar, a familiar user-interface object.
ScrollPane A container class which implements automatic horizontal and/or vertical scrolling for a single child component.
TextArea A TextArea object is a multi-line region that displays text.
TextComponent The TextComponent class is the superclass of any component that allows the editing of some text.
TextField A TextField object is a text component that allows for the editing of a single line of text.
TexturePaint The TexturePaint class provides a way to fill a Shape with a texture that is specified as a BufferedImage.
Window A Window object is a top-level window with no borders and no menubar.

Java Applet


Example:
import java.awt.*;
import java.applet.*;


public class AppletTest extends Applet {

  int x;

  public void init() {
      x = 5;
  }

  public void paint (Graphics g) {
      g.setColor(Color.blue);
      g.drawRect(10,10,50,50);

      g.setColor(Color.red);
      g.fillRect(10,70,50,50);

      g.setColor(Color.yellow);
      g.drawRoundRect(70,10,50,50,25,25);

      g.setColor(Color.blue);
      g.fillRoundRect(70,70,50,50,25,25);

      g.setColor(Color.cyan);
      g.draw3DRect(130,10,70,50,false);

      g.setColor(Color.green);
      g.fill3DRect(130,70,70,50,true);

      g.setColor(Color.red);
      g.drawString("x="+x,100,150);
  }

}

Embedding an applet in a HTML page (AppletTest.html):
<html>
<head>
  <title>Applet Test</title>
</head>
<body>

    <applet code="AppletTest.class" width="400" height="400"> </applet> 

</body>
</html>

After you generate file AppletTest.class, you need to run command
      chmod 755 AppletTest.class.
However, you can not see the applet with browsers stalled in the machines in SH118. The browsers are all need to be upgraded. You need to use command
          appletviewer AppletTest.html to see the output. The application appletviewer simulates the browser, but only interprets the embedded applet.




How to create a HTML page?
  1. Login in a machine in SH118.
  2. Type command cd ..
  3. Type command chmod 711 yourloginname
  4. Type command cd yourloginname
  5. Type command mkdir public_html
  6. Type command chmod 755 public_html
  7. Type command cd public_html
  8. Create a file HtmlTest.html as follows:
    <html>   
    <head>
     <title>Title of the page </title>
    </head>
    <body>
     
    Put whatever you want to show on the page here, possiblly 
    you may need to embed an applet.
    
    </body>
    </html>   
       
  9. Type command chmod 755 HtmlTest.html
  10. Then use a browser to load your page via url:
    http://www.cs.nmsu.edu/~yourloginname/HtmlTest.html


The major graphic user interface (GUI) objects showed at the very beginning of this lecture, was produced by an applet.
Click here to view the two classes used.