// Fig. 11.20: GridBagDemo.java
// Demonstrating GridBagLayout.
import java.applet.Applet;
import java.awt.*;

public class GridBagDemo extends Applet { 
  private Canvas c;
  private Choice cb;
  private TextArea ta;
  private TextField tf;
  private Button b1, b2, b3;
  private GridBagLayout gbLayout;
  private GridBagConstraints gbConstraints; 
    
  public void init()
  {
    gbLayout = new GridBagLayout();
    setLayout( gbLayout );   // applet

    // instantiate gridbag constraints
    gbConstraints = new GridBagConstraints();

    ta = new TextArea( "Text Area", 5, 10 );
    cb = new Choice( );
    cb.add( "Linden" );
    cb.add( "Birch" );
    cb.add( "Ceder" );
    tf = new TextField( "TextField" );
    b1 = new Button( "Button 1" );
    b2 = new Button( "Button 2" );
    b3 = new Button( "Button 3" );
    c = new Canvas();
    c.setBackground( Color.blue );
    c.setSize( 10, 5 );

    // text area
    // weightx and weighty are both 0: the default
    // anchor for all components is CENTER: the default
    gbConstraints.fill = GridBagConstraints.BOTH;
    addComponent( ta, 0, 0, 1, 3 );    
       
    // button b1
    // weightx and weighty are both 0: the default
    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
    addComponent( b1, 0, 1, 2, 1 );
      
    // choice button
    // weightx and weighty are both 0: the default
    // fill is HORIZONTAL
    addComponent( cb, 2, 1, 2, 1 );             

    // button b2
    gbConstraints.weightx = 1000;  // can grow wider
    gbConstraints.weighty = 1;     // can grow taller
    gbConstraints.fill = GridBagConstraints.BOTH;
    addComponent( b2, 1, 1, 1, 1 );
       
    // button b3
    // fill is BOTH
    gbConstraints.weightx = 0;
    gbConstraints.weighty = 0;    
    addComponent( b3, 1, 2, 1, 1 );
       
    // textfield
    // weightx and weighty are both 0: fill is BOTH
    addComponent( tf, 3, 0, 2, 1 );

    // canvas
    // weightx and weighty are both 0: fill is BOTH
    addComponent( c, 3, 2, 1, 1 );    
  }

  // addComponent is programmer defined
  private void addComponent( Component c, int row, int column,
			     int width, int height )
  {
    // set gridx and gridy 
    gbConstraints.gridx = column;
    gbConstraints.gridy = row;

    // set gridwidth and gridheight
    gbConstraints.gridwidth = width;   
    gbConstraints.gridheight = height;

    // set constraints
    gbLayout.setConstraints( c, gbConstraints );  
    add( c );      // add component to applet
  }
}
