package canvas;

import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;

public class ViewFrame extends JFrame {
  private JPanel contentPane;
  private XYLayout xYLayout1 = new XYLayout();
  private JTextArea jTextArea1 = new JTextArea();
  private JPanel jPanel1 = new JPanel();
  private DrawArea theArea = new DrawArea();
  private XYLayout xYLayout2 = new XYLayout();

  private int count = 0;
  private Rectangle2D.Double square1 = new  Rectangle2D.Double(20,20,50,50)   ;
  private Ellipse2D.Double circle1 = new Ellipse2D.Double(100,100, 140,140);
  private JButton jButton1 = new JButton();

  //Construct the frame
  public ViewFrame() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    //setIconImage(Toolkit.getDefaultToolkit().createImage(ViewFrame.class.getResource("[Your Icon]")));
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(xYLayout1);
    this.setSize(new Dimension(500, 400));
    this.setTitle("Object Selector");
    jTextArea1.setText("click on canvas to select an object");
    theArea.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        theArea_mouseClicked(e);
      }
    });
    theArea.setBackground(Color.pink);
    theArea.setBorder(BorderFactory.createRaisedBevelBorder());
    theArea.setLayout(xYLayout2);
    jButton1.setText("Add");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    contentPane.add(jTextArea1,   new XYConstraints(18, 335, 342, 36));
    contentPane.add(theArea,   new XYConstraints(28, 24, 446, 286));
    contentPane.add(jButton1,  new XYConstraints(387, 335, 72, 37));
  }
  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

  void theArea_mouseClicked(MouseEvent e) {
      Point pt = e.getPoint();
      String msg = "";
      if ((count==2) & square1.contains(pt)) msg = " square1 ";
      if ((count==2) & circle1.contains(pt)) msg = " circle1 ";
      jTextArea1.setText( pt.toString()+ msg);
  }

  void jButton1_actionPerformed(ActionEvent e) {
     if (count == 1) {theArea.add(circle1); count = 2;}
     if (count == 0) {theArea.add(square1); count = 1;};
  }
}
