// 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 palette extends Canvas { 
asciiJam aj;
colorBut cb;
Color[] palette;
Image img=null;

  public palette(asciiJam aj) {
     palette = new Color[216];
     this.aj = aj;
     //setLayout(new GridLayout(36,6));
     //setBackground(Color.gray);
     //int values[] = { 0, 51, 102, 153, 204, 255 };
     int i=0;	
     for(int r = 0; r<6;r++){
        for(int g = 0; g<6;g++){
           for(int b = 0; b<6;b++){
           	  //cb = new colorBut(new Color(r*51,g*51,b*51),i);
           	  //add(cb);
           	  //add(new colorBut(new Color(r*51,g*51,b*51),i));
           	  palette[i] = new Color(r*51,g*51,b*51);
			  i++;
	       }
		}
	 }

  }

   public boolean handleEvent(Event e){
	 if(e.id==Event.MOUSE_DOWN){
            //if(e.target instanceof colorBut){
		   if(e.x>=0&&e.x<=60&&e.y>=0&&e.y<=215){
             aj.curColorIndex = e.x/10 + (e.y/6)*6; //((colorBut)e.target).getIndex();
             aj.curCol = palette[aj.curColorIndex]; //((Component)e.target).getBackground();
		   //g.fillRect((i*10)%60,(i/6)*7,8,7);
		     repaint();
		   }
            //}                             
     }
	 else if(e.id==Event.MOUSE_ENTER){
	 	   env.setCursor(env.DEFAULT_CURSOR,aj);
	 }
	 else if(e.id==Event.MOUSE_EXIT){
	 	   env.setCursor(aj.tool,aj);
	 }
     return true; 
   } 

   public void update(Graphics g){
   	paint(g);
   }
   public void paint(Graphics g){
     if(img==null){
	 	img = aj.createImage(60,215);
		Graphics ig = img.getGraphics();
   	    for(int i = 0; i<palette.length;i++){
	    	ig.setColor(palette[i]);
	  	    ig.fillRect((i*10)%60,(i/6)*6,10,6);
	    }
	    ig.dispose();
	 }
	 g.drawImage(img,0,0,null);
	 Dimension d = size();
	 g.setColor(aj.curCol);
	 g.fill3DRect(1,d.height-24,d.width-1,23,true);
   }

}                     
