/* Copyright Andy C. Deck 1999 All rights reserved. */    
import java.awt.*;
import java.awt.image.PixelGrabber;
import java.applet.Applet;

public class drawPanel extends Panel{
Color col=Color.white;
Graphics g=null;
int tSize=8;
Image bufImg = null;
byte tType=Env.CAPGUN;
boolean penDown = false;
Stroke curStroke;
readAnim cgiReader = null;
play playButton;
Item curItem = null;
connection con;
openStudio ap;
int cnt = 0;
itemList iList = null;

    public drawPanel(openStudio a,Image img){
	 super();
	 ap = a;
	 bufImg = img;
	 g = myGetGraphics();
	 g.setColor(Color.black);
	 g.fillRect(0,0,img.getWidth(null),img.getHeight(null));
	 g.setColor(Color.white);
	 curStroke = (Stroke) Env.strokes[Env.CROSSHAIR];
	 curItem = new Item();
	 iList = new itemList(); 
	} 

    public void update(Graphics graph){
	paint(graph);
    }

    public void paint(Graphics g) {
	g.drawImage(bufImg,0,0,(java.awt.image.ImageObserver)null);
    }

   /* public boolean mouseEnter(Event e, int x, int y){
       requestFocus();
	return true;
    }*/

    public void stopDownload()
    {
        if(cgiReader!=null) cgiReader.stop();
	//cgiReader = null;
	playButton.stop();
    }

    public void play(String name){
	penDown = false;
	if(cgiReader!=null) cgiReader.stop();
	//cgiReader = null;
	cgiReader = new readAnim(this.size().width,this.size().height,ap,playButton,name);
    }

    public boolean mouseDown(Event e,int x, int y){ 
	penDown = true;
	cnt = iList.init(x,y,e.id);
	curItem.basePoint(x,y);
	curItem.type = tType;
	curItem.id   = (byte)0; 
	((Stroke)curStroke).init(curItem);
	curItem.r  = (byte)col.getRed();
	curItem.g  = (byte)col.getGreen();
	curItem.b  = (byte)col.getBlue(); 
	curItem.press = (byte)(e.id==0?0:tSize); 
        return mouseDrag(e,x,y);
    }

    public boolean mouseDrag(Event e,int x, int y){ 
	curItem.x = (short)x; 
	curItem.y = (short)y;
        if(e.id==0)  cnt = iList.addPoint(cnt,x,y,curItem.press);
        else         cnt = iList.addPoint(cnt,x,y);
	Rectangle rect;
	// Could synchronize the graphics here, but this
        // probably would do more harm than good for now.
	//synchronized(g){
	g.setColor(col);
        rect = curStroke.fill(g,curItem);
        repaint(rect.x-1,rect.y-1,rect.width+2,rect.height+2);   
        return true;
   }

   public Graphics myGetGraphics(){
	if(g==null) {
		Graphics bufg = bufImg.getGraphics();
		if(bufg!=null) return bufg;
		else return getGraphics();
	}
	else return g;
   }

   public boolean mouseUp(Event e,int x, int y){ 
	penDown = false;
	g = myGetGraphics();
	iList.export(cnt,con,curItem);
	repaint();
	return true;
   }
}
