import java.awt.*;
import java.util.Vector;

public class toolPicker extends Panel {
drawPanel dp;
demoStroke[] tool = new demoStroke[20];
Dimension tpDim;
CardLayout cl;
String names[] = { "one", "two"};

public toolPicker(drawPanel dp) {
 super();
 demoStroke ds;
 Panel p;
 int k=0;
 this.dp = dp;
 int tabht = 10;
 tab[] c = new tab[2];
 setLayout(cl=new CardLayout());
  for(int j=0;j<=1;j++){
	p =new Panel();
	p.setLayout(null);
	p.reshape(0,0,68,183);
	c[j] = new tab(this);
	p.add(c[j]);
	c[j].reshape(0,0,72,tabht);
	p.add(tool[k]=  new demoStroke(this,k++));
	p.add(tool[k]=  new demoStroke(this,k++));
	p.add(tool[k]=  new demoStroke(this,k++));
	p.add(tool[k]=  new demoStroke(this,k++));
	p.add(tool[k]=  new demoStroke(this,k++));
	p.add(tool[k]=  new demoStroke(this,k++));
	p.add(tool[k]=  new demoStroke(this,k++));
	p.add(tool[k]=  new demoStroke(this,k++));
	p.add(tool[k]=  new demoStroke(this,k++));
	p.add(tool[k]=  new demoStroke(this,k++));
	for(int i=j*10;i<k;i+=2){
		 tool[i].reshape(0,tabht+i/2*34-j*170,32,32);
		 tool[i+1].reshape(36,tabht+i/2*34-j*170,32,32);
	}
	add(names[j],p);
  }
  setTool(dp.tType);
}

	public void setTool(int num){
	    int old = dp.tType;
	        dp.curStroke = (Stroke) Env.strokes[num]; 
		dp.tType = dp.curStroke.getTool();
		tool[old].repaint();
	}
	
	public byte getTool(){
		return dp.tType;
	}
	
}


class demoStroke extends Canvas{
Stroke tmp;
int num;
toolPicker tp;
byte icon[];
boolean down = false;

		public demoStroke(toolPicker t, int num){
			setBackground(Color.darkGray);
			this.num = num;
			tp = t;
                        tmp = (Stroke)Env.strokes[num] ; 
			icon = tmp.getIcon();
		}
	
		public void paint(Graphics g){
			g.setColor(Color.darkGray);
		        g.fillRect(0,0,32,32);
			if(tp.getTool()==num){
				g.setColor(Color.gray);
				//g.draw3DRect(0,0,31,31,true);
				g.drawRect(0,0,31,31);
			}
			if(down){
			     if(num==12) g.setColor(Color.darkGray);
			     else	g.setColor(Color.gray);
			}
			else {
		        	if(num==12) g.setColor(Color.lightGray);
				else   g.setColor(Color.white);
			}
			icon = tmp.getIcon();
			drawDemostroke(g);
		}

		public void drawDemostroke(Graphics g){
		   Stroke st = Env.strokes[tp.getTool()];
		   Item d = new  Item((byte)0,(byte)9,0,(int)icon[1],(int)icon[2],0,0,0);
                   tmp.init(d);
		for(int i=1;i<icon.length;i+=2){
                d.press = icon[0];
                d.x = (short)(icon[i]);
                d.y = (short)(icon[i+1]);
		g.clipRect(0,0,32,32);
                tmp.fill(g,d);
                }      
		}

	public boolean handleEvent(Event e){
		if(e.id == Event.MOUSE_DOWN){
			tp.setTool(num);
		}
		else if(e.id == Event.MOUSE_ENTER){
			down = true;
			repaint();
		}
		else if(e.id == Event.MOUSE_EXIT || e.id == Event.MOUSE_UP){
			down = false;	
			repaint();
		}
		return true;
	}	
}
