import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/** DemoPosition - demo of component positioning (very similar to
* <code>DemoSize</code>)<p>
*
* Instead of outputting component sizes, we output component positions.
* All positions are with respect to 0,0 at the top left-hand corner of the
* component's container.
*
* As with <code>DemoSize</code>, a command-line argument determines
* which layout manager is used ("f" = <code>FlowLayout</code>,
* "g" = <code>GridLayout</code>).<p>
*
* The following output is generated
* if the <code>FlowLayout</code> layout manager is used:<p>
*
* <pre>
*     b1 position: x=0 y=0
*     b2 position: x=0 y=0
*     label position: x=0 y=0
*     tf1 position: x=0 y=0
*     tf2 position: x=0 y=0
*     tf3 position: x=0 y=0
*     ta position: x=0 y=0
*     panel position: x=0 y=0
*     -----
*     End of Constructor!
*     -----
*     b1 position: x=5 y=34
*     b2 position: x=105 y=34
*     label position: x=159 y=39
*     tf1 position: x=193 y=37
*     tf2 position: x=202 y=37
*     tf3 position: x=240 y=37
*     ta position: x=355 y=5
*     panel position: x=0 y=0
*     -----
* </pre>
*
* @see <a href="DemoPosition.java">source code</a>
* @author Scott MacKenzie, 2002
*/
public class DemoPosition
{
   public static void main(String[] args)
   {
      if (args.length != 1 || !(args[0].equals("f") || args[0].equals("g")))
      {
         usage();
         System.exit(0);
      }

      // use look and feel for my system (Win32)
      try {
         UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
      } catch (Exception e) {}

      DemoPositionFrame frame = new DemoPositionFrame(args[0]);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("DemoPosition");
      frame.pack();
      frame.show();
   }

   private static void usage()
   {
      System.out.println("usage: java DemoPosition arg1\n\n" +
         "where 'arg1' is one of\n" +
         "    f = use FlowLayout\n" +
         "    g = use GridLayout");
   }
}

class DemoPositionFrame extends JFrame implements ComponentListener
{
   JButton b1;
   JButton b2;
   JLabel label;
   JTextField tf1;
   JTextField tf2;
   JTextField tf3;
   JTextArea ta;
   JPanel panel;

   JComponent[] c = new JComponent[8];

   public DemoPositionFrame(String layoutArg)
   {
      // ----------------------------------
      // construct and configure components
      // ----------------------------------

      b1 = new JButton("Button One");
      b2 = new JButton("B2");
      label = new JLabel("Hello");
      tf1 = new JTextField();
      tf2 = new JTextField("Hello");
      tf3 = new JTextField("Hello", 10);
      ta = new JTextArea(5, 10);
      panel = new JPanel();

      b1.setName("b1");
      b2.setName("b2");
      label.setName("label");
      tf1.setName("tf1");
      tf2.setName("tf2");
      tf3.setName("tf3");
      ta.setName("ta");
      panel.setName("panel");

      c[0] = b1;
      c[1] = b2;
      c[2] = label;
      c[3] = tf1;
      c[4] = tf2;
      c[5] = tf3;
      c[6] = ta;
      c[7] = panel;

      // -----------------
      // install listeners
      // -----------------

      this.addComponentListener(this);

      // ------------------
      // arrange components
      // ------------------
      // Note: 'panel' instantiated above

      // set the layout manager, as per command line argument

      if (layoutArg.equals("f"))
         panel.setLayout(new FlowLayout());

      else if (layoutArg.equals("g"))
         panel.setLayout(new GridLayout(1, 7));

      panel.add(b1);
      panel.add(b2);
      panel.add(label);
      panel.add(tf1);
      panel.add(tf2);
      panel.add(tf3);
      panel.add(ta);
      this.setContentPane(panel);

      dumpPositions();

      System.out.println("End of Constructor!");
      System.out.println("-----");
   }

   // ----------------------------------
   // implement ComponentListener method
   // ----------------------------------

   public void componentHidden(ComponentEvent ce) {}
   public void componentMoved(ComponentEvent ce) {}
   public void componentShown(ComponentEvent ce) {}
   public void componentResized(ComponentEvent ce)
   {
      dumpPositions();
   }

   private void dumpPositions()
   {
      for (int i = 0; i < c.length; ++i)
      {
         String name = c[i].getName();
         int x = c[i].getX();
         int y = c[i].getY();
         System.out.println(name + " position: x=" + x + " y=" + y);
      }
      System.out.println("-----");
   }
}

