import java.awt.*;        // Java visual tools
import java.awt.event.*;  // Java visual events
import java.applet.*;     // Java applet classes

// This class demonstrates ways that HTML or a JavaScript
// program can communicate with an applet.

public class Sketch1 extends Applet
                    implements MouseMotionListener,
                               MouseListener {

  private int lines= 0; // The number of line segments so far.

  private int[] X1 = new int[1000];
  private int[] Y1 = new int[1000];
  private int[] X2 = new int[1000];
  private int[] Y2 = new int[1000];

  private int lastX = 0;
  private int lastY = 0;

  private Color currentColor = new Color(0, 0, 0);

  public void init() {
    addMouseMotionListener(this);
    addMouseListener(this);

    // This code reads parameters in the <APPLET> tag from the
    // web page. Each parameter is in the form
    // <PARAM NAME="parametername" VALUE="parametervalue">
    // Since each is passed as a string, we must first
    // parse it using Integer.parseInt before using as a number
    // to define a color.

    int redLevel = Integer.parseInt(getParameter("red"));
    int greenLevel = Integer.parseInt(getParameter("green"));
    int blueLevel = Integer.parseInt(getParameter("blue"));

    // We construct a new Color object from the RGB parameters
    // passed, and use that color to set the background of
    // the applet.

    setBackground(new Color(redLevel, greenLevel, blueLevel));
   
    }

  public void paint(Graphics g) {

    // Note that whenever we draw, we now set the drawing to the
    // current color by using setColor on the graphics component.

    g.setColor(currentColor);
    for (int i = 0; i < lines; i++) {
      g.drawLine(X1[i], Y1[i], X2[i], Y2[i]);
      }
    }

  public void mousePressed(MouseEvent e) {
    lastX = e.getX();
    lastY = e.getY();
    }
  public void mouseEntered(MouseEvent e) {
    lastX = e.getX();
    lastY = e.getY();
    }

  public void mouseReleased(MouseEvent e) {}
  public void mouseClicked(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}


  public void mouseDragged(MouseEvent e) {

    if (lines < 1000) {
      int x = e.getX(); // Get current coordinates
      int y = e.getY();

      Graphics g = getGraphics();     // Get the Graphics component

      // Note that whenever we draw, we now set the drawing to the
      // current color by using setColor on the graphics component.

      g.setColor(currentColor);

      g.drawLine(lastX, lastY, x, y); // and draw the line.

      X1[lines] = lastX;
      Y1[lines] = lastY;
      X2[lines] = x;
      Y2[lines] = y;
      lines++;

      lastX = x;
      lastY = y;
      }
    }

  public void mouseMoved(MouseEvent e) {}

  // Thes are additional methods we define for the JavaScript to call.

  // Clear the drawing by setting the number of lines to 0 and
  // redrawing the sketch.

  public void clear() {
    lines = 0;
    repaint();
    }

  // Change the color by creating a new Color object from the
  // parameters, storing it in a state variable, and repainting.

  public void changeColor(int r, int g, int b) {
    currentColor = new Color(r, g, b);
    repaint();
    }

  // Return the percentage of the array filled when requested by
  // the JavaScript (this will be used to inform the user when the
  // array is full).

  public double getPercentage() {
    return lines/1000;
    }
  }

