// Copyright Andy Deck 2003, GNU Public License 2
// With the notable provision that any commercial use
// must be approved by the copyright holder.
import java.awt.*;

public class toolbar extends Canvas { // implements MouseListener{
 collabyrinth ic;
 int oldTool=-1;
 int curRollover = 0;

  public toolbar(collabyrinth i){
        this.ic = i;
	setBackground(Color.white);
  }
  public void update(Graphics g){
	paint(g);
  }
  public void paint(Graphics g){
	if(oldTool != ic.tool){
        	g.fillRect(1,ic.tool*21,29,21); // selected tool
		g.setColor(Color.white);
        	if(oldTool>=0) g.fillRect(1,oldTool*21,29,21); 
                g.setColor(Color.black);
	}
        for(int i=0;i<5;i++){ 
		if(i==ic.tool){
                  g.setColor(Color.white);
		       if(i==0) rectTool(g);
		  else if(i==1) lineTool(g);
		  else if(i==2) bucketTool(g);
		  else if(i==3) pencilTool(g);
		  else          dropperTool(g);
                  g.setColor(Color.black);
		}else{
		       if(i==0) rectTool(g);
		  else if(i==1) lineTool(g);
		  else if(i==2) bucketTool(g);
		  else if(i==3) pencilTool(g);
		  else          dropperTool(g);
		}	
		g.drawRect(0,i*21,30,21); // selected tool
	}
  }

  public void dropperTool(Graphics g){
/* this is ugly because Sun's VM (1.4.1)
 * under WindowsXP wouldn't deal correctly with images
 * and Apple's Quartz rendering doesn't deal correctly
 * with non-anti-aliased diagonal lines
 */
      //g.drawLine(9,97,15,91);
      g.fillRect(9,97,1,1);
      g.fillRect(10,96,1,1);
      g.fillRect(11,95,1,1);
      g.fillRect(12,94,1,1);
      g.fillRect(13,93,1,1);
      g.fillRect(14,92,1,1);
      g.fillRect(15,91,1,1);

      //g.drawLine(17,93,11,99);
      g.fillRect(17,93,1,1);
      g.fillRect(16,94,1,1);
      g.fillRect(15,95,1,1);
      g.fillRect(14,96,1,1);
      g.fillRect(13,97,1,1);
      g.fillRect(12,98,1,1);
      g.fillRect(11,99,1,1);

      //g.drawLine(7,100,8,101);
      g.fillRect(8,101,1,1);
      g.fillRect(7,100,1,1);

      g.fillRect(8,98,1,2);
      g.fillRect(9,100,2,1);
      g.fillRect(16,92,1,1);
      g.fillRect(14,90,4,1);
      g.fillRect(18,91,1,4);
      g.fillRect(19,91,2,1);
      g.fillRect(20,90,2,1);
      g.fillRect(21,88,1,2);
      g.fillRect(18,87,3,1);
      g.fillRect(17,88,2,1);
      g.fillRect(17,89,1,2);
  }
  public void pencilTool(Graphics g){
      g.fillRect(12,78,3,2);
      g.fillRect(12,77,2,1);
      g.fillRect(17,67,3,1);
      g.fillRect(17,70,1,1);


      g.fillRect(16,77,1,1);
      g.fillRect(16,76,1,1);
      g.fillRect(17,75,1,1);
      g.fillRect(17,74,1,1);
      g.fillRect(18,73,1,1);
      g.fillRect(18,72,1,1);
      g.fillRect(19,71,1,1);
      g.fillRect(19,70,1,1);
      g.fillRect(20,69,1,1);
      g.fillRect(20,68,1,1);

      g.fillRect(12,76,1,1);
      g.fillRect(12,75,1,1);
      g.fillRect(13,74,1,1);
      g.fillRect(13,73,1,1);
      g.fillRect(14,72,1,1);
      g.fillRect(14,71,1,1);
      g.fillRect(15,70,1,1);
      g.fillRect(15,69,1,1);
      g.fillRect(16,68,1,1);
      g.fillRect(16,67,1,1);

      // damn that mac graphite renderer
      //g.drawLine(12,76,16,67);
      //g.drawLine(16,77,20,68);

      g.fillRect(12,78,4,1);
      g.fillRect(12,79,3,1);
      g.fillRect(12,80,2,1);
      g.fillRect(12,81,1,1);
  }
  public void rectTool(Graphics g){
      g.drawRect(8,6,15,10);
  }
  public void lineTool(Graphics g){
/* this is ugly because Sun's VM (1.4.1)
 * under WindowsXP wouldn't deal correctly with images
 * and Apple's Quartz rendering doesn't deal correctly
 * with non-anti-aliased diagonal lines
 */
      //g.drawLine(10,37,21,26);
	for(int i=0;i<11;i++){
          g.fillRect(10+i,37-i,1,1);
	}
  }
  void bucketTool(Graphics g){
      g.fillRect(20,51,2,6);
      g.drawLine(20,50,20,57);
      g.drawLine(11,47,11,51);
      g.fillRect(14,52,1,1);
      g.fillRect(15,53,1,1);
      g.fillRect(16,52,1,1);
      g.drawLine(15,47,15,51);
      g.drawLine(12,46,14,46);

/* this is ugly because Sun's VM (1.4.1)
 * under WindowsXP wouldn't deal correctly with images
 * and Apple's Quartz rendering doesn't deal correctly
 * with non-anti-aliased diagonal lines
 */
      //g.drawLine(16,48,20,52);
      g.fillRect(16,48,1,1);
      g.fillRect(17,49,1,1);
      g.fillRect(18,50,1,1);
      g.fillRect(19,51,1,1);
      g.fillRect(20,52,1,1);

      //g.drawLine(20,52,13,59);
      g.fillRect(20,52,1,1);
      g.fillRect(19,53,1,1);
      g.fillRect(18,54,1,1);
      g.fillRect(17,55,1,1);
      g.fillRect(16,56,1,1);
      g.fillRect(15,57,1,1);
      g.fillRect(14,58,1,1);
      g.fillRect(13,59,1,1);

      //g.drawLine(8,54,15,47);
      g.fillRect(8,54,1,1);
      g.fillRect(9,53,1,1);
      g.fillRect(10,52,1,1);
      g.fillRect(11,51,1,1);
      g.fillRect(12,50,1,1);
      g.fillRect(13,49,1,1);
      g.fillRect(14,48,1,1);
      g.fillRect(15,47,1,1);

      //g.drawLine(8,55,12,59);
      g.fillRect(8,55,1,1);
      g.fillRect(9,56,1,1);
      g.fillRect(10,57,1,1);
      g.fillRect(11,58,1,1);
      g.fillRect(12,59,1,1);

      //g.drawLine(18,49,20,51);
      g.fillRect(18,49,1,1);
      g.fillRect(19,50,1,1);
      g.fillRect(20,51,1,1);
  }
  public boolean handleEvent(Event e){
      if(e.id==Event.MOUSE_DOWN){
	 if(e.x>0 && e.x<31 && e.y>0 && e.y<106){
                if(e.y<21){
			oldTool=ic.tool;
                        ic.tool = 0;
                }else if(e.y<42){
			oldTool=ic.tool;
                        ic.tool = 1;
                }else if(e.y<63){
			oldTool=ic.tool;
                        ic.tool = 2;
                }else if(e.y<84){
			oldTool=ic.tool;
                        ic.tool = 3;
                }else if(e.y<105){
			oldTool=ic.tool;
                        ic.tool = 4;
                }
		repaint();
        }
      }else if(e.id==Event.MOUSE_UP||e.id==Event.MOUSE_EXIT){
      }else if(e.id==Event.MOUSE_MOVE){

	        if(e.y<22){
                        if(curRollover!=0) ic.usrLab.setText("Rectangle tool");
                        curRollover = 0;
                }else if(e.y<44){
                        if(curRollover!=1) ic.usrLab.setText("Line tool");
                        curRollover = 1;
                }else if(e.y<66){
                        if(curRollover!=2) ic.usrLab.setText("Fill tool");
                        curRollover = 2;
                }else if(e.y<88){
                        if(curRollover!=3) ic.usrLab.setText("Draw tool");
                        curRollover = 3;
                }else if(e.y<106){
                        if(curRollover!=4) ic.usrLab.setText("Color picker tool");
                        curRollover = 4;
                }
        	ic.setCursor(Frame.HAND_CURSOR);
      }
      return true;
  }
}
