import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
 
public class Palette extends Canvas{
	private static final Color bgcolor = new Color(0,0,0);
        private Image im;
        
        public int imax;
        
	private Image im_palette;
	private MemoryImageSource mis;
	private int rgbPixels[];
	private int rgb_palette_Pixels[];
	private int new_width;
	private Color colorArray[]; 
        private Color colorArray2[];
        
        //9,50,606,576
        
	private static final int width=590;
	private static final int height=564;
        
	private static final int palette_width=10, palette_height=150;
	private static final int LeftMargin=10, RightMargin=20, TopMargin=20, BottomMargin=10;
	private static final int LeftImageIndent=8, LeftPaletteIndent=20;
        
        private int Npoints;
        
	private Oblique_State state;
	
        public Palette(Oblique_State state){
		super();
		this.state = state;
                setBackground(bgcolor);
		new_width = (int)(width+1);
		rgbPixels = new int[(width+1) * (height+1)];
		rgb_palette_Pixels = new int[palette_width * palette_height];
		//colorArray = new Color[512];
                //colorArray2 = new Color[512];
                colorArray = new Color[256];
                colorArray2 = new Color[256];
                
                //Initialize the color palette
                
                for(int i = 0; i < 127; i++) {
		    colorArray[i] = new Color(2*i,2*i,255);
		}
		
		for(int i = 127; i < 256; i++) {
		    colorArray[i] = new Color(255,255-(i-127),255-(i-127));
		}
	}
	
	public void paint(Graphics g){
	                
          //-----------------------------------------
            drawPalette(g);
          //-----------------------------------------
        }   
	
	private void drawPalette(Graphics g){
	    int i, j, map, imax;
	    FontMetrics fm = g.getFontMetrics();
	    imax = state.palettereference;
                
                for(int ii = 0; ii < 127; ii++) {
		    colorArray2[ii] = new Color(2*ii,2*ii,255);
		}
		
                for(int ii = 0; ii < 4; ii++) {
		    colorArray2[ii] = new Color(0,0,255);
		}
            
		for(int ii = 127; ii < 256; ii++) {
		    colorArray2[ii] = new Color(255,255-(ii-127),255-(ii-127));
		}
            
                for(int ii = 253; ii < 256; ii++) {
		    colorArray2[ii] = new Color(255,0,0);
		}
            
            for(i = 0; i < palette_width; i++){
		for(j = 0; j < palette_height; j++){
                    map = (palette_height-1-j)*255/(palette_height-1);
                    
                    if(map < 0) { map = 0;}
		    if(map >255) { map = 255; }
                    rgb_palette_Pixels[j*palette_width+i] = colorArray2[map].getRGB();
		}
	    }
            
	    im_palette = createImage(new MemoryImageSource(palette_width,palette_height,
				rgb_palette_Pixels,0,palette_width));
	    g.drawImage(im_palette,5,10,this);
	    
            g.setColor(Color.white);
	    g.drawString("+",6,10);
            g.drawString("_",7,165);
    	    g.drawLine(2,85,getSize().width - 3,85);
            g.drawRect(0,0,getSize().width-1,getSize().height-1);
	}
	
	public void setWidth(double ratio){
	    this.new_width=(int)(width/ratio);
	    this.rgbPixels=new int[(int)(width/ratio*height)];
	}
	
	public void update(Graphics g){
		drawPalette(g);
	}	
}	

