import java.awt.*;

import javax.swing.*;

/**
 * @author Remi Forax
 *
 */
public class MyOwnLayoutExample implements LayoutManager {

  public void addLayoutComponent(String name, Component comp) {
  }
  
  public void removeLayoutComponent(Component comp) {
  }

  public void layoutContainer(Container parent) {
    int width=parent.getWidth()/2;
    int height=parent.getHeight()/2;
      
    int count=parent.getComponentCount();
    for(int i=0;i<count;i++) {
      double angle=2*Math.PI*i/count;
      int x=(int)(width+0.8*width*Math.cos(angle));
      int y=(int)(height+0.8*height*Math.sin(angle));
      
      Component c=parent.getComponent(i);
      Dimension preferred=c.getPreferredSize();
      
      c.setBounds(x-preferred.width/2,y-preferred.height/2,
        preferred.width,preferred.height);
    }
  }
  
  public Dimension preferredLayoutSize(Container parent) {
    int width=0,height=0;
    
    int count=parent.getComponentCount();
    for(int i=0;i<count;i++) {
      Component c=parent.getComponent(i);
      Dimension preferred=c.getPreferredSize();
      width+=preferred.getWidth();
      height+=preferred.getHeight();
    }
    
    return new Dimension(width,height);
  }

  public Dimension minimumLayoutSize(Container parent) {
    return preferredLayoutSize(parent);
  }
  
  public static void main(String[] args) {
    JFrame frame=new JFrame("MyOwnLayoutExample");
    
    final JPanel panel=new JPanel(new MyOwnLayoutExample());
    for(int i=0;i<8;i++) {
      JButton button=new JButton("button "+i);
      panel.add(button);
    }
      
    frame.setContentPane(panel);
    frame.pack();
    frame.show();
  }

  
}
