package howto;

import java.awt.*;

public class FigOval extends java.applet.Applet implements Figure
{
   private TextField tfx, tfy, tfw, tfh, tfsa, tfaa;

   private TextField tfdox, tfdoy, tfdow, tfdoh;
   private TextField tffox, tffoy, tffow, tffoh;
   private TextField tfdax, tfday, tfdaw, tfdah, tfdasa, tfdaaa;
   private TextField tffax, tffay, tffaw, tffah, tffasa, tffaaa;

   private Panel p;

   private CardLayout cl;

   private NewCanvas nc;

   private Choice ch;

   public void init()
   {
      setBackground(Color.white);

      setLayout(new BorderLayout());

      Panel p1;

      p = new Panel();

      p.setLayout(cl = new CardLayout());

      p.add("Oval", p1 = new Panel());

      p1.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));

      p1.add(new Label("drawOval ("));
      p1.add(tfdox = new TextField(2));
      p1.add(new Label(","));
      p1.add(tfdoy = new TextField(2));
      p1.add(new Label(","));
      p1.add(tfdow = new TextField(2));
      p1.add(new Label(","));
      p1.add(tfdoh = new TextField(2));
      p1.add(new Label(")"));

      p.add("Filled Oval", p1 = new Panel());

      p1.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));

      p1.add(new Label("fillOval ("));
      p1.add(tffox = new TextField(2));
      p1.add(new Label(","));
      p1.add(tffoy = new TextField(2));
      p1.add(new Label(","));
      p1.add(tffow = new TextField(2));
      p1.add(new Label(","));
      p1.add(tffoh = new TextField(2));
      p1.add(new Label(")"));

      p.add("Arc", p1 = new Panel());

      p1.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));

      p1.add(new Label("drawArc ("));
      p1.add(tfdax = new TextField(2));
      p1.add(new Label(","));
      p1.add(tfday = new TextField(2));
      p1.add(new Label(","));
      p1.add(tfdaw = new TextField(2));
      p1.add(new Label(","));
      p1.add(tfdah = new TextField(2));
      p1.add(new Label(","));
      p1.add(tfdasa = new TextField(2));
      p1.add(new Label(","));
      p1.add(tfdaaa = new TextField(2));
      p1.add(new Label(")"));

      p.add("Filled Arc", p1 = new Panel());

      p1.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));

      p1.add(new Label("fillArc ("));
      p1.add(tffax = new TextField(2));
      p1.add(new Label(","));
      p1.add(tffay = new TextField(2));
      p1.add(new Label(","));
      p1.add(tffaw = new TextField(2));
      p1.add(new Label(","));
      p1.add(tffah = new TextField(2));
      p1.add(new Label(","));
      p1.add(tffasa = new TextField(2));
      p1.add(new Label(","));
      p1.add(tffaaa = new TextField(2));
      p1.add(new Label(")"));

      add("North", p);

      nc = new NewCanvas(this);

      add("Center", nc);

      p1 = new Panel();

      p1.add(new Button("Draw"));

      ch = new Choice();

      ch.addItem("Oval");
      ch.addItem("Filled Oval");
      ch.addItem("Arc");
      ch.addItem("Filled Arc");

      p1.add(ch);

      add("South", p1);

      tfx = tfdox;
      tfy = tfdoy;
      tfw = tfdow;
      tfh = tfdoh;
      tfsa = tfdasa;
      tfaa = tfdaaa;

      tfx.setText("10");
      tfy.setText("10");
      tfw.setText("100");
      tfh.setText("100");
      tfsa.setText("90");
      tfaa.setText("110");
   }

   private int parseTextField(TextField tf)
   {
      int n;

      try
      {
         n = Integer.parseInt(tf.getText());
      }
      catch (NumberFormatException nfe)
      {
         tf.setText("0");
         n = 0;
      }

      return n;
   }

   public void paintCallback(Graphics g)
   {
      int x = parseTextField(tfx);
      int y = parseTextField(tfy);
      int w = parseTextField(tfw);
      int h = parseTextField(tfh);
      int sa = parseTextField(tfsa);
      int aa = parseTextField(tfaa);

      if (ch.getSelectedItem().equals("Oval"))
         g.drawOval(x, y, w, h);
      else if (ch.getSelectedItem().equals("Filled Oval"))
         g.fillOval(x, y, w, h);
      else if (ch.getSelectedItem().equals("Arc"))
         g.drawArc(x, y, w, h, sa, aa);
      else if (ch.getSelectedItem().equals("Filled Arc"))
         g.fillArc(x, y, w, h, sa, aa);
   }

   public boolean action(Event e, Object o)
   {
      if (o.equals("Draw"))
      {
         nc.repaint();

         return true;
      }
      else if (o.equals("Oval"))
      {
         tfdox.setText(tfx.getText());
         tfdoy.setText(tfy.getText());
         tfdow.setText(tfw.getText());
         tfdoh.setText(tfh.getText());

         tfx = tfdox;
         tfy = tfdoy;
         tfw = tfdow;
         tfh = tfdoh;

         cl.show(p, (String)o);

         nc.repaint();

         return true;
      }
      else if (o.equals("Filled Oval"))
      {
         tffox.setText(tfx.getText());
         tffoy.setText(tfy.getText());
         tffow.setText(tfw.getText());
         tffoh.setText(tfh.getText());

         tfx = tffox;
         tfy = tffoy;
         tfw = tffow;
         tfh = tffoh;

         cl.show(p, (String)o);

         nc.repaint();

         return true;
      }
      else if (o.equals("Arc"))
      {
         tfdax.setText(tfx.getText());
         tfday.setText(tfy.getText());
         tfdaw.setText(tfw.getText());
         tfdah.setText(tfh.getText());
         tfdasa.setText(tfsa.getText());
         tfdaaa.setText(tfaa.getText());

         tfx = tfdax;
         tfy = tfday;
         tfw = tfdaw;
         tfh = tfdah;
         tfsa = tfdasa;
         tfaa = tfdaaa;

         cl.show(p, (String)o);

         nc.repaint();

         return true;
      }
      else if (o.equals("Filled Arc"))
      {
         tffax.setText(tfx.getText());
         tffay.setText(tfy.getText());
         tffaw.setText(tfw.getText());
         tffah.setText(tfh.getText());
         tffasa.setText(tfsa.getText());
         tffaaa.setText(tfaa.getText());

         tfx = tffax;
         tfy = tffay;
         tfw = tffaw;
         tfh = tffah;
         tfsa = tffasa;
         tfaa = tffaaa;

         cl.show(p, (String)o);

         nc.repaint();

         return true;
      }

      return false;
   }

   public Dimension preferredSize()
   {
      return new Dimension(500, 200);
   }

   public static void main(String [] args)
   {
      Frame f = new Frame("Rectangle");

      FigOval fo = new FigOval();

      fo.init();

      f.add("Center", fo);

      f.pack();
      f.show();
   }
}
