import java.awt.*;
import java.applet.*;
import java.util.*;



public class Line1 extends Applet implements Runnable {

    Thread design;
//	Canvas hereCanvas;
//	Button touchMe;
	int w, h;
	Image ink;
	Color white, black;

    Vector line1 = new Vector(500);
	long cTime = 0;
    Image oImage;
    Graphics oGraphics;

    public void init() {

		Dimension d = size();
		w=d.width;
		h=d.height;

        oImage = createImage(w, h);
        oGraphics = oImage.getGraphics();

		white =  Color.white;
		black =  Color.black;

        setBackground(white);

		ink = this.getImage(this.getDocumentBase(), this.getParameter("image"));

/*		GridBagLayout theLayout	= new GridBagLayout();
		this.setLayout(theLayout);

		hereCanvas = new Canvas();
		touchMe = new Button("[]");

		theLayout.setConstraints(hereCanvas, GBconstraints.constrain(0,  0, 1, 1, 1.0, 0.99, 1, 10, 0,0,0,0));
		theLayout.setConstraints(touchMe,   GBconstraints.constrain(0,  1, 1, 1, 1.0, 0.01, 1, 10, 0,0,0,0));

		add(hereCanvas);
		add(touchMe);
*/		
		repaint();

    }

    public void start() {
		repaint();
		if (design == null) {
			design = new Thread(this);
			design.start();
		}

    }

    public void stop() {
		if ((design != null) && design.isAlive())
			design.stop();
		design = null;
	} 

	  
	public void destroy() {
		if ((design != null) && design.isAlive())
			design.stop();
		design = null;
	} 


	public void run() {
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

   
        while (Thread.currentThread() == design) {
            cTime = System.currentTimeMillis();
            try {
				Thread.sleep(50);
			} catch (InterruptedException e) {};
            repaint();
        }
    }


    public boolean mouseDrag(Event e, int x, int y) {
       Graphics g = this.getGraphics();
       if (line1.size() > 0) {
            aPoint joey = (aPoint)line1.lastElement();
   //         g.drawLine(joey.x, joey.y, x, y);
        }

	   line1.addElement(new aPoint(e.when, x, y));
        return true;
    }


    public boolean mouseDown(Event e, int x, int y) {
		Graphics g = this.getGraphics();

		line1.removeAllElements();
        design = new Thread(this);
        design.start();	
		this.repaint();

        return true;
    }

/*	public boolean action(Event e, Object arg) {
               
	   if (e.target == touchMe) {        
		   Graphics g = hereCanvas.getGraphics();
		   g.setColor(white);			
		   g.fillRect(0, 0, w, h);			
		   return true; 
	   } else return false;
        
   }  */

    public void paint( Graphics g ) {

        update(g);
    }

    public void update(Graphics g) {
		oGraphics.setColor(black);
		oGraphics.fillRect(0,0,w,2);

        oGraphics.setColor(Color.white);
        oGraphics.fillRect(0, 2, w, h-2);

        for(int i=0; i<line1.size(); i++) {
            aPoint point1;
            point1 = (aPoint)line1.elementAt(i);
 
            if (i>0) {
                aPoint point2;
                point2 = (aPoint)line1.elementAt(i-1);

     			oGraphics.setColor(black);
			//	oGraphics.fillRect(0,0,w,3);

	/*			oGraphics.drawLine((int)((point1.x)*(1-(cTime - point1.time)/2000))-1, 
									(int)((point1.y)*(1-(cTime - point1.time)/1000)),
                                    (int)((point2.x)*(1-(cTime - point2.time)/2000))+1, 
									(int)((point2.y)*(1-(cTime - point2.time)/1000)));


				oGraphics.drawLine((int)((point1.x)*(1-(cTime - point1.time)/2000)), 
									(int)((point1.y)*(1-(cTime - point1.time)/1000))-1,
                                    (int)((point2.x)*(1-(cTime - point2.time)/2000)), 
									(int)((point2.y)*(1-(cTime - point2.time)/1000))+1);			

		  
		//		oGraphics.drawString(""+ (cTime - point1.time), point1.x, point1.y);

				oGraphics.drawLine((int)((point1.x)*(1-(cTime - point1.time)/3000))-2, 
									(int)((point1.y)*(1-(cTime - point1.time)/2000)),
                                    (int)((point2.x)*(1-(cTime - point2.time)/3000))+2, 
									(int)((point2.y)*(1-(cTime - point2.time)/2000)));

				oGraphics.drawLine((int)((point1.x)*(1-(cTime - point1.time)/3000)), 
									(int)((point1.y)*(1-(cTime - point1.time)/2000))-2,
                                    (int)((point2.x)*(1-(cTime - point2.time)/3000)), 
									(int)((point2.y)*(1-(cTime - point2.time)/2000))+2);		   */


				oGraphics.drawLine((int)((point1.x)*(1-(cTime - point1.time)/3000))+1, 
									(int)((point1.y)*(1-(cTime - point1.time)/2000)),
                                    (int)((point2.x)*(1-(cTime - point2.time)/3000))-1, 
									(int)((point2.y)*(1-(cTime - point2.time)/2000)));

				

				oGraphics.drawLine((int)((point1.x)*(1-(cTime - point1.time)/3000)), 
									(int)((point1.y)*(1-(cTime - point1.time)/2000))+1,
                                    (int)((point2.x)*(1-(cTime - point2.time)/3000)), 
									(int)((point2.y)*(1-(cTime - point2.time)/2000))-1);
            }

        } 
	//	oGraphics.drawImage(ink, 0, 328, this);

        g.drawImage(oImage, 0, 4, this);
		

    }
}



class aPoint extends java.lang.Object {
    long time;
	int x, y;

    public aPoint(long time, int x, int y) {
        this.time = time;
		this.x = x;
        this.y = y;
    }
}

class GBconstraints {

  	public static GridBagConstraints constrain(int x, int y, int w, int h, 
		double wtx, double wty, int fill, int anchor, int top, int left, 
		int bottom, int right) {
		GridBagConstraints c = new GridBagConstraints();
		c.gridx=x;
		c.gridy=y;
		c.gridwidth=w;
		c.gridheight=h;
		c.weightx=wtx;
		c.weighty=wty;
		c.fill=fill;
		c.anchor=anchor;
		if (top+left+bottom+right>0) 
			c.insets = new Insets (top, left, bottom, right);

		return c;
	}

}
