Saturday, September 21, 2013

Programming in Java: AWT classes 01

AWT classes
The Abstract Window Toolkit (AWT) contains numerous classes and methods that allow you to create and manage windows. It is also the foundation upon which Swing is built. The AWT classes are contained in the java.awt package. It is one of Java’s largest packages. Although a common use of the AWT is in applets, it is also used to create stand-alone windows that run in a GUI environment, such as Windows.

Window Fundamentals
Window Hierarchy
The two most common windows are those derived from Panel, which is used by applets, and those derived from Frame, which creates a standard application window. Figure shows the class hierarchy for Panel and Frame.
A short description of these classes is given below:

Component
At the top of the AWT hierarchy is the Component class. Component is an abstract class that encapsulates all of the attributes of a visual component. All user interface elements that are displayed on the screen and that interact with the user are subclasses of Component. A Component object is responsible for remembering the current foreground and background colours and the currently selected text font.

Container
The Container class is a subclass of Component. It has additional methods that allow other Component objects to be nested within it. A container is responsible for laying out (that is, positioning) any components that it contains. It does this through the use of various layout managers.

Panel
The Panel class is a concrete subclass of Container. It doesn’t add any new methods; it simply implements Container. Panel is the superclass for Applet. When screen output is directed to an applet, it is drawn on the surface of a Panel object.  In essence, a Panel is a window that does not contain a title bar, menu bar, or border.
Other components can be added to a Panel object by its add( ) method (inherited from Container). Once these components have been added, you can position and resize them manually using the setLocation( ), setSize( ), setPreferredSize( ), or setBounds( ) methods defined by Component.

Window
The Window class creates a top-level window. A top-level window is not contained within any other object; it sits directly on the desktop. Generally, you won’t create Window objects directly. Instead, you will use a subclass of Window called Frame.

Frame
Frame encapsulates what is commonly thought of as a “window.” It is a subclass of Window and has a title bar, menu bar, borders, and resizing corners.

Creating Frame window
The following are the three steps to be followed in creating any Frame window:
1.      Create an object of any class derived from Frame
2.      Set the dimensions of the window using the object created
3.      Set the visibility of the window

Creating the Object
First we need to create a class extending Frame. Then the object of this class needs to be created from the main method. Here are two of Frame’s constructors:
Frame( )
Frame(String title)
The first form creates a standard window that does not contain a title. The second form creates a window with the title specified by title. They can be used in the constructor of our class using super().

Setting the dimensions
The setSize( ) method is used to set the dimensions of the window. Its signature is shown here:
void setSize(int newWidth, int newHeight)
void setSize(Dimension newSize)
The new size of the window is specified by newWidth and newHeight, or by the width and height fields of the Dimension object passed in newSize. The dimensions are specified in terms of pixels.
The getSize( ) method is used to obtain the current size of a window. Its signature is shown here:
Dimension getSize( )
This method returns the current size of the window contained within the width and height fields of a Dimension object.

Setting the visibility
After a frame window has been created, it will not be visible until you call setVisible( ).
Its signature is shown here:
void setVisible(boolean visibleFlag)
The component is visible if the argument to this method is true. Otherwise, it is hidden.

Setting a Window’s Title
You can change the title in a frame window using setTitle( ), which has this general form:
void setTitle(String newTitle)
Here, newTitle is the new title for the window.

Closing a Frame Window
When a Frame window is closed it can be removed from the screen by calling setVisible(false). But this does not close the window instead makes it invisible.
To intercept a window-close event, we need to implement the windowClosing() method of the WindowListener interface.
Inside windowClosing(), the program can be terminated by calling System.exit(0).
The example below illustrates this.

Simple Frame Example:
import java.awt.*;
import java.awt.event.*;

class Frame1 extends Frame implements WindowListener{
    Frame1(){
                  super();
    }
    Frame1(String title){
                  super(title);
                  setSize(800,400);                     // Set dimensions
                  setVisible(true);                       // Set Visibility
                  addWindowListener(this);
    }

    /* **Implement all the methods of WindowListener Interface** */
    public void windowClosing(WindowEvent we){
                  System.exit(0);
    }
    public void windowActivated(WindowEvent we){
    }
    public void windowClosed(WindowEvent we){
    }
    public void windowDeactivated(WindowEvent we){
    }
    public void windowIconified(WindowEvent we){
    }
    public void windowDeiconified(WindowEvent we){
    }
    public void windowOpened(WindowEvent we){
    }
}

class Frame1Demo{
    public static void main(String[] args){
                  Frame1 f1 = new Frame1("My First Frame");
    }

}

This program can be compiled and run like any other java program. When run it produces the following output.

Handling events
Since Frame is a subclass of Component, it inherits all the capabilities defined by Component. This means that you can use and manage a frame window just like you manage an applet’s main window. For example, you can override paint( ) to display output, call repaint( ) when you need to restore the window, and add event handlers.

Following example demonstrates this:
import java.awt.*;
import java.awt.event.*;

class FrameEvent2 extends Frame{
         String msg = "Welcome";
    FrameEvent2(){
    }
    FrameEvent2(String title){
                  super(title);
                  addWindowListener(new MyWindowAdapter());
                  addMouseListener(new MyMouseListener(this));
                  setSize(600,300);
                  setVisible(true);
                  repaint();
    }
    public void paint(Graphics g){
                  g.drawString(msg,300,150);
    }
}

class FrameEventDemo{
    public static void main(String[] args){
                  FrameEvent2 fe1 = new FrameEvent2("Frame Events Demo");
    }
}

class MyWindowAdapter extends WindowAdapter{
    MyWindowAdapter(){
    }
    public void windowClosing(WindowEvent we){
                  System.exit(0);
    }
}

class MyMouseListener implements MouseListener{
    FrameEvent2 fr;
    MyMouseListener(FrameEvent2 f){
                  fr = f;
    }
    public void mouseClicked(MouseEvent me){
                  fr.msg = "Mouse Clicked";
                  fr.repaint();
    }
         public void mouseEntered(MouseEvent me){
                  fr.msg = "Mouse Entered";
                  fr.repaint();
         }
         public void mouseExited(MouseEvent me){
                  fr.msg = "Mouse Exited";
                  fr.repaint();
         }
         public void mousePressed(MouseEvent me){
                  fr.msg = "Mouse Pressed";
                  fr.repaint();
         }
         public void mouseReleased(MouseEvent me){
                  fr.msg = "Mouse Released";
                  fr.repaint();
         }
}
The above code creates a Frame window which detects the user’s mouse actions and displays the appropriate message. Here we have used four classes. 
FrameEvent2: which is derived from Frame. This represents our window.
FrameEventDemo: which contains the main() method. This method just creates an object of the FrameEvent2 class.
MyMouseListener:The class that implements the MouseListener Interface. All mouse events are handled by this class. We can see the statement addMouseListener(new MyMouseListener(this)); Here we register the Mouse Listener saying that the methods are implemented in MyMouseListener class. this is passed as an argument to the constructor of MyMouseListener because we need an object of the current frame window to call the repaint() method of that window.
MyWindowAdapter: This class is used to implement the windowClosing() method so that the window gets closed when user tries for it.


The output of the above program is:


No comments:

Post a Comment