import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/** DemoFlowLayout - program to demonstrate the
* <code>FlowLayout</code> class.<p>
*
* Usage:<p>
*
* <pre>
*     java DemoFlowLayout arg1 arg2
*
*     where 'arg1' = strut size in pixels
*
*     and 'arg2' is one of
*        c = center alignment
*        l = left alignment
*        r = right alignment
* </pre>
*
* Invocations:<p>
*
* <code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;java DemoFlowLayout 5 c</code><br>
* <center><img src="DemoFlowLayout-1.gif"></center><p>
* 
* <code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;java DemoFlowLayout 10 r   (after resizing)</code><br>
* <center><img src="DemoFlowLayout-2.gif"></center><p>
*
* @see <a href="DemoFlowLayout.java">source code</a>
* @author Scott MacKenzie, 2002
*/
public class DemoFlowLayout
{
   public static void main(String[] args)
   {
      // check for correct invocation (2 command-line args required)

      if (args.length != 2)
      {
         usage();
         return;
      }

      // get 1st argument (strut size)

      int strutSize = Integer.parseInt(args[0]);

      // get 2nd arg (alignment)

      int alignment = -1;
      if (args[1].equals("c")) alignment = FlowLayout.CENTER;
      if (args[1].equals("l")) alignment = FlowLayout.LEFT;
      if (args[1].equals("r")) alignment = FlowLayout.RIGHT;

      if (alignment == -1)
      {
         usage();
         return;
      }

      // use look and feel for my system (Win32)
      try {
         UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
      } catch (Exception e) {}

      DemoFlowLayoutFrame frame = new DemoFlowLayoutFrame(strutSize, alignment);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("DemoFlowLayout");
      frame.pack();
      frame.show();
   }

   private static void usage()
   {
      System.out.println(
         "usage: java DemoFlowLayout arg1 arg2\n\n" +
         "where 'arg1' = strut size in pixels\n\n" +
         "and 'arg2' is one of\n" +
         "   c = center alignment\n" +
         "   l = left alignment\n" +
         "   r = right alignment"
      );
      return;
   }
}

class DemoFlowLayoutFrame extends JFrame
{
   private JButton b1;
   private JButton b2;
   private JButton b3;
   private JButton b4;
   private JButton b5;

   public DemoFlowLayoutFrame(int strutArg, int alignArg)
   {
      // ----------------------------------
      // construct and configure components
      // ----------------------------------

      b1 = new JButton("Carrot");
      b2 = new JButton("Yam");
      b3 = new JButton("Broccoli");
      b4 = new JButton("Brussel Sprouts");
      b5 = new JButton("Zucchini");

      // -----------------
      // layout components
      // -----------------

      // use a flow layout with 'align', 'hgap', and 'vgap' as per command-line

      FlowLayout layout = new FlowLayout(alignArg, strutArg, strutArg);
      JPanel panel = new JPanel(layout);
      panel.add(b1);
      panel.add(b2);
      panel.add(b3);
      panel.add(b4);
      panel.add(b5);

      // for in-class demo...
      //panel.setPreferredSize(new Dimension(200, 200));

      // for in-class demo...
      //System.out.println("panel : " + panel.getPreferredSize());

      // make panel this JFrame's content pane

      this.setContentPane(panel);
   }
}

