LevSelector.com New York
home > Java AWT components

AWT Components
 
 
AWT packages home - top of the page -

AWT (Abstract Windowing Toolkit) - original old java library for building GUI interfaces.  Now there is a better library (Swing), but AWT is still used in Applets.
Packages:
  java.awt - set of 95 interfaces, classes, exceptions and errors
  Other packages starting with java.awt.  :
     color, datatransfer, dnd (drag and drop), event, font, geom, im (input method), im.spi (input method framework specification), image, image.renderable, print
 
 
java.awt.Component home - top of the page -

java.awt.Component - abstract class
  setBounds( ), setLocation( ), setSize( ), Rectangle getBounds( ), boolean contains( ),
  setBackground( ), setForeground( ), setFont( ), setCursor( ),
  setEnabled( ), setVisible( )

  long mask = AWTEvent.ACTION_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK
  void disableEvents(long bitmask)
  void enableEvents(long bitmask)

Subclasses of Component:
Button
Canvas
Checkbox (with CheckboxGroup will appear as radio buttons)
Choice (pop-up menu)
Container (Window, Frame, Applet, Panel, Dialog, ScrollPane)
Label
List - scrollable list of selectable items
Scrollbar
TextComponent
   setEditable( ), setText( ), getText( ), getSelectedText( ) - can be enabled, but not editable (user can move cursor and select text)

Interfaces of Component:
ImageObserver -
MenuContainer -
Serializable -
 
Window, Frame home - top of the page -

Window - extends Container - top-level objects without menu bar or window controls.
  2 subclasses: Frame & Dialog
  Window should be constructed from a Frame or another Window ( Window(Frame owner) or Window(Window owner))

java.awt.Frame class
 
import java.awt.*;
import java.awt.event.*;

class FrameDemo extends Frame {

  public static void main (String[] args) {
    new FrameDemo().show( );
  }

  // constructor 
  FrameDemo( ) {
    super("Example Frame");
    setBounds(10,10,200,150); // on the screen
    addWindowListener( new WindowAdapter( ) {
      public void windowClosing( WindowEvent e ) {
        setVisible( false );
        dispose( );
        System.exit(0);
      }
    } );  // end calling addWindowListener method 
  } // end constructor
} // end class

java.awt.Dialog - can be modal or not  (FileDialog - always modal)
Dialog constructor requires Frame or another dialog owner.
For applet the browser provides a Frame - here how to get it:
 
public void showDlg( ) {
  Container c = getParent( );
  while( c != null ) {
    if ( c instanceof Frame ) {
      myDlg = new TestDialog((Frame) c );
      myDlg.show( ); return;
    }
    c = c.getParent( ); // try next parent
  }
}

 
Menu home - top of the page -

Almost everything can have a menu, because Component, Frame, Menu, MenuBar - all implement MenuContainer interface.

java.lang.Object
  |
  +--java.awt.MenuComponent
        |
        +--java.awt.MenuItem
              |
              +--java.awt.Menu

All Implemented Interfaces:
     Accessible, MenuContainer, Serializable

Direct Known Subclasses:
     PopupMenu

To create standard menu:
  create a MenuBar object
     add Menu objects that contain MenuItem objects.  (MenuItem objects create ActionEvent)
MenuBar mb = new MenuBar( );

  Menu fileMenu = new Menu("File");
  fileMenu.add( new Menuitem("New") );
  fileMenu.addSeparator( );
  fileMenu.add( new Menuitem("Exit") );
  mb.add( fileMenu );

  Menu helpMenu = new Menu("Help");
  helpMenu.add( new Menuitem("About") );
  mb.add( helpMenu );

  setMenuBar ( mb );


 
Geometric Utility Classes home - top of the page -

Dimension, Point, Rectangle, Polygon
 
 
Graphics home - top of the page -

java.awt.Color, Cursor, Font, FontMetrics, Graphics, Graphics2D, GraphicsEnvironment

Font myFont = new Font( "Serif", Font.BOLD, 50 );
  // Serif, SansSerif, Monospaced

Graphics object - to draw: Component, clipping Rectangle, current drawing Color, current Font
public void update( Graphics g ) {
  g.clearRect(0,0,width,height);
  paint(g);
}

Other methods in Graphics class:
  drawLine(x1,y1,x2,y2)
  drawRect(x,y,w,h), fillRect(x,y,w,h)
  drawOval(x,y,w,h), drawArc(x,y,w,h,startAngle, endAngle), fillArc(x,y,w,h,startAngle, endAngle)
  drawPolygon( Polygon p), fillPolygon( Polygon p)
  boolean drawImage(img,x,y,obs) // where obs - ImageObserver
  boolean drawImage(img,x,y,w,h,obs) // scales to w,h
  Graphics getGraphics( ) // can call only after something is shown on the sceen
  dispose( )

Double buffering:
Image imgA = createimage(200,150); // method in Component
Graphicsc gr = imgA.getGraphics( );
// now draw this off-screen image
...
// now draw it on the screen
getGraphics().drawImage(imgA, x,y, this);

Example of paint method:
public void paint( Graphics g ) {
  String str = "Hello";
  g.setColor( Color.darkgray );
  g.drawString( str, 30, 120 );
}

repaint method in Component class = update-paint sequence
public void run( ) {
  while( true ) {
    doAnimationMoves( );
    animCanvas.repaint( );
    try {
      Thread.yield( ); // to allow to paint
    } catch(InterruptedException e) {}
  }
}

 
Layouts home - top of the page -

2 interfaces: java.awt.LayoutManager & LayoutManager2

public void setLayout(LayoutManager m);
Panel p2 = new Panel( new BorderLayout() );
LayoutManager getLayout( );
void addLayoutComponent(name, component );
void removeLayoutComponent( component );
void layoutContainer(parent)
Dimension minimumLayoutSize(parent)
Dimension preferredLayoutSize(parent)

LayoutManager2 interface:
void addLayoutComponent(Component c, Object constraints)
Borderlayout
CardLayout
GridBagLayout
...

Panel, Applet - FlowLayout
Window,Frame,Dialog - BorderLayout

FlowLayout f3 = new FlowLayout (FlowLayout.RIGHT, 10, 2);  // 10 - horizontal gap, 2 - vertical gap
GridLayout g3 = new GridLayout(rows, cols, hgap, vgap);

GridBagLayout( )
gridBagConstraints( )  //  gridx, gridy, gridwidth, gridheight, insets (Insets object), weightx, weighty

setlayout( null );  // working without layout manager