import java.applet.Applet;
import java.awt.*;
import java.io.*;
import sbk.image.GifImageEncoder;

/**
 * En liten applet som man kan rita på.
 * Copyright 1998 Björn Andersson
 *
 * @author Björn Andersson
 * @version 1.0
 */
public class DrawApplet extends Applet implements Runnable {
  Image    offImage;
  Graphics offGraphics;
  Image   undoImage;
  Thread t;
  int mouseX, mouseY, lineWidth = 2, w, h;
  boolean advanced = true;

  public void init () {
    Dimension d = size();
    w = d.width;
    h = d.height;
    offImage = createImage(w, h);
    offGraphics = offImage.getGraphics();
    offGraphics.setColor(Color.white);
    offGraphics.fillRect(0, 0, w, h);
  }

  public void start() {
    t = new Thread(this);
    t.start();
  }
  public void stop() {
    t.stop();
  }
  public void run() {
    /* Här har vi inget att göra... */
  }

  public void update(Graphics g) {
    g.drawImage(offImage, 0, 0, null);
  }
  public void paint(Graphics g) {
    g.drawImage(offImage, 0, 0, null);
  }

  /**
    Lite föråldrade saker så att det fungerar hos folk... :P
    */
  public boolean mouseDown(Event e, int x, int y) {
    Dimension d = size();
    undoImage = createImage(d.width, d.height);
    (undoImage.getGraphics()).drawImage(offImage, 0, 0, null);

    if ((e.modifiers & Event.ALT_MASK) != 0 ||
	(e.modifiers & Event.META_MASK) != 0)
      offGraphics.setColor(Color.white);
    else
      offGraphics.setColor(Color.black);
    offGraphics.fillOval(x-lineWidth/2, y-lineWidth/2, lineWidth, lineWidth);
    repaint();
    mouseX = x;
    mouseY = y;
    return true;
  }
  public boolean mouseDrag(Event e, int x, int y) {
    if (lineWidth>3) {
      if (advanced) {
	int xk1[] = { mouseX-lineWidth/2, mouseX+lineWidth/2, x+lineWidth/2, x-lineWidth/2 };
	int yk1[] = { mouseY-lineWidth/2, mouseY+lineWidth/2, y+lineWidth/2, y-lineWidth/2 };
	int xk2[] = { mouseX-lineWidth/2, mouseX+lineWidth/2, x+lineWidth/2, x-lineWidth/2 };
	int yk2[] = { mouseY+lineWidth/2, mouseY-lineWidth/2, y-lineWidth/2, y+lineWidth/2 };
	offGraphics.fillPolygon(xk1, yk1, 4);
	offGraphics.fillPolygon(xk2, yk2, 4);
      }
      offGraphics.fillOval(x-lineWidth/2, y-lineWidth/2, lineWidth, lineWidth);
    }
    else {
      offGraphics.drawLine(mouseX, mouseY, x, y);
      if (lineWidth>1) {
	offGraphics.drawLine(mouseX+1, mouseY, x+1, y);
	offGraphics.drawLine(mouseX, mouseY+1, x, y+1);
	offGraphics.drawLine(mouseX+1, mouseY+1, x+1, y+1);
	if (lineWidth>2) {
	  offGraphics.drawLine(mouseX-1, mouseY, x-1, y);
	  offGraphics.drawLine(mouseX, mouseY-1, x, y-1);
	  offGraphics.drawLine(mouseX-1, mouseY-1, x-1, y-1);
	  offGraphics.drawLine(mouseX+1, mouseY-1, x+1, y-1);
	  offGraphics.drawLine(mouseX-1, mouseY+1, x-1, y+1);
	}
      }
    }
    repaint();
    mouseX = x;
    mouseY = y;
    return true;
  }
  public boolean keyDown(Event e, int key) {
    if (key == 'c') {
      offGraphics.setColor(Color.white);
      offGraphics.fillRect(0, 0, w, h);
      repaint();
      return true;
    }
    else if (key == 'a') {
      advanced = !advanced;
      return true;
    }
    else if (key == 'u') {
      offGraphics.drawImage(undoImage, 0, 0, null);
      repaint();
      return true;
    }
    else if (key == 'h') {
      this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
      return true;
    }
    /*
    else if (key == 's') {
      try {
	GifImageEncoder gie = new GifImageEncoder(offImage);
        File fil = new File("test.gif");
	FileOutputStream fos = new FileOutputStream(fil);
	gie.encodeTo(fos);
	fos.close();
      } catch (Exception ex) { offGraphics.setColor(Color.blue); }
      return true;
    }
    */
    else if (key < 58 && key > 48) {
      lineWidth = key - 48;
      return true;
    }
    else if (key == '0') {
      lineWidth = 10;
      return true;
    }
    else {
      return false;
    }
  }
}
