import java.awt.*;
import java.lang.Math;
import java.awt.event.*;
import java.awt.Graphics;
import java.applet.Applet;

public class draw extends Applet implements ActionListener,
MouseListener, MouseMotionListener {
  // declare 'bLine', 'bOval', 'bRect' and 'bRounded' as
  // type 'Button'
  Button bLine, bOval, bRect, bRounded;
  Point start, end;

  boolean mouseUp = false;
  boolean line = false;
  boolean oval = false;
  boolean rectangle = false;
  boolean rounded = false;

  public void init() {
    bLine = new Button("Draw Lines");
    bOval = new Button("Draw Ovals");
    bRect = new Button("Draw Rectangles");
    bRounded = new Button("Draw Rounded Rects");
    // initialise bLine as a Button with text "Draw Lines"
    // initialise bOval as a Button with text "Draw Ovals"
    // initialise bRect as a Button with text "Draw Rectangles"
    // initialise bRounded as a Button with text "Draw Rounded Rects"

    add(bLine);
    add(bOval);
    add(bRect);
    add(bRounded);

    // add action listeners to the four buttons
    bLine.addActionListener(this);
    bOval.addActionListener(this);
    bRect.addActionListener(this);
    bRounded.addActionListener(this);

    addMouseListener(this);
    addMouseMotionListener(this);
  }

  public void mousePressed(MouseEvent e) {
    mouseUp = false;
    start = new Point(e.getX(), e.getY());
  }

  public void mouseReleased(MouseEvent e) {
    if(line) {
      end = new Point(e.getX(), e.getY());
    } else {
      end = new Point(Math.max(e.getX(), start.x), Math.max(e.getY(),    
      start.y));
      start = new Point(Math.min(e.getX(), start.x), 
      Math.min(e.getY(), start.y));
    }
    mouseUp = true;
    repaint();
  }

  public void mouseClicked(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}
  public void mouseMoved(MouseEvent e) {}
  public void mouseDragged(MouseEvent e) {}

  public void paint(Graphics g) {
    if(mouseUp){
      int width = end.x - start.x;
      int height = end.y - start.y;
      if(line){
        // draw a line using the aforementioned variables
        g.drawLine(start.x, start.y, end.x, end.y);
      } else if(oval) {
        // draw an oval using the aforementioned variables
        g.drawOval(start.x, start.y, width, height);
      } else if(rectangle) {
        // draw a rectangle using the aforementioned variables
        g.drawRect(start.x, start.y, width, height);
      } else if(rounded) {
        // draw a rounded rectangle using the aforementioned 
        // variables
        g.drawRoundRect(start.x, start.y, width, height, 10, 10);
      }
    }
  }

  public void actionPerformed(ActionEvent e) {
    // use the setFlagsFalse code to reset the flags (see below)
    if(e.getSource() == bLine)line = true;
    if(e.getSource() == bOval)oval = true;
    if(e.getSource() == bRect)rectangle = true;
    if(e.getSource() == bRounded)rounded = true;
  }

  void setFlagsFalse() {
    rounded = false;
    line = false;
    oval = false;
    rectangle = false;
  }
}

