package bezierdemo;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

/**
 * <p>Title: Bezier Demo</p>
 * <p>Description: demonstrates the basic drawing techniques used to render Beziers (2D)</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: Carleton University</p>
 * @author Pat Beirne
 * @version 1.0
 */

public class BezierDemo extends Applet implements ItemListener {
  private boolean isStandalone = false;
  Label lab;
  BezierPanel canvas; // the drawing space

  //Get a parameter value
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  //Construct the applet
  public BezierDemo() {
    LayoutManager lm = new BorderLayout();
    this.setLayout(lm);

    // lay out the options
    JPanel rightPane = new JPanel();
    rightPane.setLayout(new GridLayout(14,1));

    ButtonGroup cbg = new ButtonGroup();
    JRadioButton cb = new JRadioButton("By polyline",true);
    cb.addItemListener(this);
    cbg.add(cb);
    rightPane.add(cb);
    cb = new JRadioButton("By line-halfing",false);
    cb.addItemListener(this);
    cbg.add(cb);
    rightPane.add(cb);
    cb = new JRadioButton("By curve of pursuit",false);
    cb.addItemListener(this);
    cbg.add(cb);
    rightPane.add(cb);

    //skip
    rightPane.add(new JSeparator());

    cbg = new ButtonGroup();
    cb = new JRadioButton("Draw depth:1",false);
    cb.addItemListener(this);
    cbg.add(cb);
    rightPane.add(cb);
    cb = new JRadioButton("Draw depth:2",false);
    cb.addItemListener(this);
    cbg.add(cb);
    rightPane.add(cb);
    cb = new JRadioButton("Draw depth:3",false);
    cb.addItemListener(this);
    cbg.add(cb);
    rightPane.add(cb);
    cb = new JRadioButton("Draw depth:4",false);
    cb.addItemListener(this);
    cbg.add(cb);
    rightPane.add(cb);
    cb = new JRadioButton("Draw depth:5",true);
    cb.addItemListener(this);
    cbg.add(cb);
    rightPane.add(cb);

    //skip
    rightPane.add(new JSeparator());

    cbg = new ButtonGroup();
    cb = new JRadioButton("Cubic Bezier",true);
    cb.addItemListener(this);
    cbg.add(cb);
    rightPane.add(cb);
    cb = new JRadioButton("Quadratic Bezier",false);
    cb.addItemListener(this);
    cbg.add(cb);
    rightPane.add(cb);

//    rightPane.invalidate();
    this.add(rightPane, BorderLayout.EAST);

    canvas = new BezierPanel();
    this.add(canvas, BorderLayout.CENTER);

//    lab = new Label("hi world");
//    this.add(lab);
    canvas.addMouseListener(canvas);
    canvas.addMouseMotionListener(canvas);
  }
  //Initialize the applet
  public void init() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception {
  }
  //Start the applet
  public void start() {
  }
  //Stop the applet
  public void stop() {
  }
  //Destroy the applet
  public void destroy() {
  }
  //Get Applet information
  public String getAppletInfo() {
    return "Applet Information";
  }
  //Get parameter info
  public String[][] getParameterInfo() {
    return null;
  }


  /********* check box listener **************/
  public void itemStateChanged(ItemEvent e) {
    Object i = e.getItem();
    if (i instanceof JRadioButton) {
      JRadioButton jrb = (JRadioButton) i;
      if (jrb.getText().indexOf("polyline")>0)
        canvas.setDrawMode(BezierPanel.POLYLINE);
      else if (jrb.getText().indexOf("half")>0)
        canvas.setDrawMode(BezierPanel.LINESPLIT);
      else if (jrb.getText().indexOf("pursuit")>0)
        canvas.setDrawMode(BezierPanel.COP);

      if (jrb.getText().indexOf("1")>0)
        canvas.setDrawDepth(1);
      if (jrb.getText().indexOf("2")>0)
        canvas.setDrawDepth(2);
      if (jrb.getText().indexOf("3")>0)
        canvas.setDrawDepth(3);
      if (jrb.getText().indexOf("4")>0)
        canvas.setDrawDepth(4);
      if (jrb.getText().indexOf("5")>0)
        canvas.setDrawDepth(5);

      if (jrb.getText().indexOf("Cubic")>=0)
        canvas.setDrawCubic(true);
      else if (jrb.getText().indexOf("Quad")>=0)
        canvas.setDrawCubic(false);
    }
  }

  //Main method
  public static void main(String[] args) {
    BezierDemo applet = new BezierDemo();
    applet.isStandalone = true;
    Frame frame;
    frame = new Frame();
    frame.setTitle("Applet Frame");
    frame.add(applet, BorderLayout.CENTER);
    applet.init();
    applet.start();
    frame.setSize(400,320);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
    frame.setVisible(true);
  }
}