import java.awt.*;

class XYLayoutManager
  implements LayoutManager
{
  public XYLayoutManager() {
    myMaxSize=20;
    mySize=0;
    myData=new Rectangle[myMaxSize];
  }

  public void addLayoutComponent(String name, Component comp) {
    System.out.println("addLayoutComponent:"+name+":"+comp);
  }

  public void layoutContainer(Container parent) {
  // assume that this layout manager will be used for one container
    Rectangle rect;
    Dimension dim;
    int w,h,i;

    Insets insets=parent.getInsets();
    Component [] components=parent.getComponents();
    int size=components.length;

    checkSize(size);

    // start laying out
    for(i=0;i<size;i++) {
      rect=myData[i];
      if(rect!=null) {
        w=rect.width;
        h=rect.height;

        if(w==0 || h==0 ) {
          dim=components[i].getPreferredSize();
          if(w==0)
            w=dim.width;
          if(h==0)
            h=dim.height;
        }

        components[i].setBounds(rect.x+insets.left,rect.y+insets.top,w,h);        
      }
    }

//    System.out.println("layoutContainer:"+parent);
  }

  public Dimension minimumLayoutSize(Container parent) {
    System.out.println("minimumLayoutSize:"+parent);
    return preferredLayoutSize(parent);
  }

  public Dimension preferredLayoutSize(Container parent) {
  // assume that this layout manager will be used for one container
    Rectangle rect;
    Dimension dim;
    int x,y,w,h,i;
    int maxX=0;
    int maxY=0;

    Insets insets=parent.getInsets();
    Component [] components=parent.getComponents();
    int size=components.length;

    checkSize(size);

    // start laying out
    for(i=0;i<size;i++) {
      rect=myData[i];
      if(rect!=null) {
        w=rect.width;
        h=rect.height;

        if(w==0 || h==0 ) {
          dim=components[i].getPreferredSize();
          if(w==0)
            w=dim.width;
          if(h==0)
            h=dim.height;
        }

        x=insets.left+rect.x+w+insets.right;
        y=insets.top+rect.y+h+insets.bottom;
        
        if(x>maxX)
          maxX=x;
        if(y>maxY)
          maxY=y;
      }
    }

    System.out.println("preferredLayoutSize:"+parent+":"+maxX+":"+maxY);
    return new Dimension(maxX,maxY);
  }

  public void removeLayoutComponent(Component comp) {
    System.out.println("removeLayoutComponent:"+comp);
  }

  private void checkSize(int size) {
    // ensure room in array
    if(size>=myMaxSize) {
      myMaxSize=myMaxSize*2;
      Rectangle rTemp[]=myData;
      myData=new Rectangle[myMaxSize];

      for(int i=0;i<mySize;i++) {    // assume that mySize is most important - not size
        myData[i]=rTemp[i];
      }
    }
  }

  public void setNext(int x, int y, int width, int height) {
    checkSize(mySize);
    myData[mySize]=new Rectangle(x,y,width,height);
    mySize++;  
  }

  private Rectangle myData[];
  private int myMaxSize;
  private int mySize;
   
  public static void main(String args[]) {
    Frame f=new Frame();

    XYLayoutManager xy=new XYLayoutManager();
    Panel p=new Panel();
    f.add(p,"South");
    p.setLayout(xy);

    Button b=new Button("Button1");
    xy.setNext(10,10,60,0);
    p.add(b);

    xy.setNext(10,40,1,1);
    p.add(new Panel());
    
    xy=new XYLayoutManager();
    p=new Panel();
    f.add(p,"Center");
    p.setLayout(xy);

    b=new Button("Button2");
    xy.setNext(10,10,0,45);
    p.add(b);

    f.setSize(200,180);
    f.show();

    System.out.println("XY all the way");
  }
}