import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
 
public class DrawPalette extends Canvas{
	//private static final Color bgcolor = new Color(216,216,191);
	//private static final Color bgcolor = new Color(216,230,230);
        private static final Color bgcolor = new Color(0,0,0);
                
	private static final Color acolor = new Color(152,152,152);
	private static final Color ccolor = new Color(230,230,230);
	private Image im;
        
        public int imax, is_new;
        
	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[];
	private static final int width=300;
	private static final int height=300;
	private static final int palette_width=20, palette_height=350;
	private static final int LeftMargin=5, RightMargin=20, TopMargin=10, BottomMargin=10;
	private static final int LeftImageIndent=5, LeftPaletteIndent=27;
        
        private int Nx = 300;
        private int Ny = 300;
        private int Nratio = 1; // Since Nt = 6000, Nratio can be 10, 15, 20, 24
        
        private double w[][];
        
        private int Npoints;
        private double ymax, ymin, xmax, xmin, ypmax, ypmin;
        private double[] x, y, v;
        private int[] xx, yy, yen;
        
	private Trans_State state;
	private boolean DirtyFlag;
	
        public DrawPalette(Trans_State state){
		super();
		this.state = state;
                setBackground(bgcolor);
		new_width = (int)(width);
		rgbPixels = new int[(width+1) * (height+1)];
		rgb_palette_Pixels = new int[palette_width * palette_height];
		colorArray = new Color[1024];
                colorArray2 = new Color[1024];
                
		DirtyFlag = true;
                
                xmin = 0.0;
                ymax = 1.0;
                ymin = -1.0;
                ypmax = 1.0;
                ypmin = 0.0;
                
                //Npoints = 251;
                Npoints = state.Nt/Nratio + 1;
                
                try{
                    x = new double[Npoints];
                    y = new double[Npoints];
                    xx = new int[Npoints];
                    yy = new int[Npoints];
                    yen = new int[Npoints];
                }
                catch(Exception e){e.printStackTrace();}
                for(int i = 0; i < x.length; i++){
                    x[i] = (double)i;
                    y[i] = state.pulse[i*Nratio];
                }

                w = new double[Nx+1][Ny+1];
                
                
                imax = state.palettereference;
                
	}
	
	public void paint(Graphics g){
	    drawPalette(g);
	    
	}   
	
	private void drawImage(Graphics g){
	   g.drawImage(im,LeftImageIndent,(getSize().height-29-height)/2,this);
	}
	
	private void drawPalette(Graphics g){
	    int i, j, map, imax;
	    FontMetrics fm = g.getFontMetrics();
	    imax = state.palettereference;
            
            if(state.IsThreshold){
               if(state.IsCoarser){     
                     
                    // 16 x 16 palette
                    for(int ii = 0; ii < 247; ii++) {
                       colorArray2[ii] = new Color(0,0,255);
                    }

                    for(int ii = 247; ii < 503; ii++) {
                       colorArray2[ii] = new Color((ii-247)/16*16,(ii-247)/16*16,255);
                    }

                    for(int ii = 503; ii < 520; ii++) {
                       colorArray2[ii] = new Color(255,255,255);
                    }

                    // red
                    for(int ii = 520; ii < 776; ii++) {
                        colorArray2[ii] = new Color(255,247-(((ii-520)/16)*16),247-(((ii-520)/16)*16));
                    }

                    for(int ii = 776; ii < 1024; ii++) {
                       colorArray2[ii] = new Color(255,0,0);
                    }
                    
               }
               else{
                    //8 x 8 palette
                     for(int ii = 0; ii < 251; ii++) {
                       colorArray2[ii] = new Color(0,0,255);
                    }

                    for(int ii = 251; ii < 507; ii++) {
                       colorArray2[ii] = new Color((ii-251)/8*8,(ii-251)/8*8,255);
                       //System.out.println(ii+"   "+colorArray2[ii]);
                    }

                    for(int ii = 507; ii < 516; ii++) {
                       colorArray2[ii] = new Color(255,255,255);
                    }

                    // red
                    for(int ii = 516; ii < 772; ii++) {
                        colorArray2[ii] = new Color(255,251-(((ii-516)/8)*8),251-(((ii-516)/8)*8));
                    }

                    for(int ii = 772; ii < 1024; ii++) {
                       colorArray2[ii] = new Color(255,0,0);
                    }
               }
                    /*
                    //4 x 4 palette
                     for(int ii = 0; ii < 253; ii++) {
                       colorArray2[ii] = new Color(0,0,255);
                    }

                    for(int ii = 253; ii < 508; ii++) {
                       colorArray2[ii] = new Color((ii-253)/4*4,(ii-253)/4*4,255);
                       //System.out.println(ii+"   "+colorArray2[ii]);
                    }

                    for(int ii = 508; ii < 515; ii++) {
                       colorArray2[ii] = new Color(255,255,255);
                    }

                    // red
                    for(int ii = 515; ii < 768; ii++) {
                        colorArray2[ii] = new Color(255,253-(((ii-514)/4)*4),253-(((ii-514)/4)*4));
                    }

                    for(int ii = 768; ii < 1024; ii++) {
                       colorArray2[ii] = new Color(255,0,0);
                    }
                    */
                    /* 
                    // 16 x 16 palette
                    for(int ii = 0; ii < 247; ii++) {
                       colorArray2[ii] = new Color(0,0,255);
                    }

                    for(int ii = 247; ii < 503; ii++) {
                       colorArray2[ii] = new Color((ii-247)/16*16,(ii-247)/16*16,255);
                    }

                    for(int ii = 503; ii < 520; ii++) {
                       colorArray2[ii] = new Color(255,255,255);
                    }

                    // red
                    for(int ii = 520; ii < 776; ii++) {
                        colorArray2[ii] = new Color(255,247-(((ii-520)/16)*16),247-(((ii-520)/16)*16));
                    }

                    for(int ii = 776; ii < 1024; ii++) {
                       colorArray2[ii] = new Color(255,0,0);
                    }
                     */
            }
            else{
                
                for(int ii = 0; ii < 256; ii++) {
		   colorArray2[ii] = new Color(0,0,255);
		}
                
                for(int ii = 256; ii < 512; ii++) {
		   colorArray2[ii] = new Color(ii-256,ii-256,255);
		}
		
		for(int ii = 512; ii < 768; ii++) {
		    colorArray2[ii] = new Color(255,255-(ii-512),255-(ii-512));
		}
                
                for(int ii = 768; ii < 1024; 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)*1023/(palette_height-1);
                    
                    if(map < 0) { map = 0;}
		    if(map >1023) { map = 1023; }
                    rgb_palette_Pixels[j*palette_width+i] = colorArray2[map].getRGB();
		}
	    }
            int leftshift = 15;
	    im_palette = createImage(new MemoryImageSource(palette_width,palette_height,
				rgb_palette_Pixels,0,palette_width));
	    g.drawImage(im_palette,LeftPaletteIndent-leftshift,(getSize().height-palette_height)/2,this);
	    
	    g.setColor(Color.white);
            g.drawString("-max",LeftPaletteIndent+palette_width-25-leftshift,(getSize().height+palette_height)/2+14);
    	    g.drawString("max",LeftPaletteIndent+palette_width-22-leftshift,(getSize().height-palette_height)/2-8);
	    g.drawRect(LeftPaletteIndent-1-leftshift,(getSize().height)/2-palette_height/2-1,
			palette_width+1,1+palette_height);
            
            g.drawLine(LeftPaletteIndent+palette_width-leftshift, (getSize().height)/2,
                       LeftPaletteIndent+palette_width-leftshift +5,(getSize().height)/2);
            g.drawLine(LeftPaletteIndent-leftshift, (getSize().height)/2,
                       LeftPaletteIndent-leftshift - 5,(getSize().height)/2);
            
            g.drawString("0",LeftPaletteIndent+palette_width-leftshift +8,(getSize().height)/2+5);
        }
	
	private void drawCanvas(Graphics g){
	    g.setColor(bgcolor.darker());
	    g.fillRect(0,getSize().height-2,getSize().width,2);
	    g.fillRect(getSize().width-2,0,2,getSize().height);
	    g.setColor(bgcolor.brighter());
	    g.fillRect(0,0,2,getSize().height-1);
	    g.fillRect(0,0,getSize().width-2,2);
	}	
	
	public void setDirtyFlag(boolean DirtyFlag){
	    this.DirtyFlag = DirtyFlag;
	}
	
	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);               
	}
		
}	

