import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
 
public class Draw2DCanvasB 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=400;
	private static final int height=400;
	private static final int palette_width=20, palette_height=500;
	private static final int LeftMargin=5, RightMargin=20, TopMargin=10, BottomMargin=10;
	private static final int LeftImageIndent=5, LeftPaletteIndent=670;
        
        private int Nx = 400;
        private int Ny = 400;
        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 Draw2DCanvasB(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];
                
                //Initialize the color palette
                
                /* old code
                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));
		}
                */
		
                imax = state.palettereference;
                if(state.IsThreshold){
                
                    for(int i = 0; i < 246; i++) {
                       colorArray[i] = new Color(0,0,255);
                    }

                    for(int i = 246; i < 502; i++) {
                       colorArray[i] = new Color((i-246)/16*16,(i-246)/16*16,255);
                    }

                    for(int i = 502; i < 522; i++) {
                       colorArray[i] = new Color(255,255,255);
                    }

                    // red
                    for(int i = 522; i < 778; i++) {
                        colorArray[i] = new Color(255,240-(((i-522)/16)*16),240-(((i-522)/16)*16));
                    }

                    for(int i = 778; i < 1024; i++) {
                       colorArray[i] = new Color(255,0,0);
                    }
                }
                else{
                
                    for(int i = 0; i < 256; i++) {
                       colorArray[i] = new Color(0,0,255);
                    }

                    for(int i = 256; i < 512; i++) {
                       colorArray[i] = new Color(i-256,i-256,255);
                    }

                    for(int i = 512; i < 768; i++) {
                        colorArray[i] = new Color(255,255-(i-512),255-(i-512));
                    }

                    for(int i = 768; i < 1024; i++) {
                       colorArray[i] = new Color(255,0,0);
                    }
                 }
                 
                //updatePixels();
		//mis = new MemoryImageSource(new_width,height,rgbPixels,0,new_width);
		//mis.setAnimated(true);
		//mis.setFullBufferUpdates(false);
		//im = createImage(mis);
	}
	
        public void redopalette(){
                if(state.IsThreshold){
                 if(!state.IsCoarser){   
                    //8 x 8 palette
                     for(int ii = 0; ii < 251; ii++) {
                       colorArray[ii] = new Color(0,0,255);
                    }

                    for(int ii = 251; ii < 507; ii++) {
                       colorArray[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++) {
                       colorArray[ii] = new Color(255,255,255);
                    }

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

                    for(int ii = 772; ii < 1024; ii++) {
                       colorArray[ii] = new Color(255,0,0);
                    }
                 }
                 else{
                 
                    for(int i = 0; i < 247; i++) {
                       colorArray[i] = new Color(0,0,255);
                    }

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

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

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

                    for(int i = 776; i < 1024; i++) {
                       colorArray[i] = new Color(255,0,0);
                    }
                 }
              }
              else{
                
                    for(int i = 0; i < 256; i++) {
                       colorArray[i] = new Color(0,0,255);
                    }

                    for(int i = 256; i < 512; i++) {
                       colorArray[i] = new Color(i-256,i-256,255);
                    }

                    for(int i = 512; i < 768; i++) {
                        colorArray[i] = new Color(255,255-(i-512),255-(i-512));
                    }

                    for(int i = 768; i < 1024; i++) {
                       colorArray[i] = new Color(255,0,0);
                    }
              }
        }
        
	public void paint(Graphics g){
	    //drawImage(g);
	    updatePixels(g);
            //System.out.println(state.Dipole_Current);
	    //drawPalette(g);
	    //drawLabels(g);
	    //drawLabels2(g);
	    //drawAxis(g);
            
	}   
	
	private void updatePixels(Graphics g){
	    int i=0, j=0, map=0, istar = 0, jstar = 0;
	    double x, y, min, max;
	    min =  0.0; 
	    max =  1.0e-20;
	    double myarray[][] = new double[width+1][height+1];
            int myslit[][] = new int[width+1][height+1];
            min = -300.0;
            max = 300.0;
            for(i = 1; i < width+1; i++){
		for(j = 1; j < height+1; j++){
                    
                    myarray[i][j] = state.Ex2[i][j];
		    myslit[i][j] = 0;
                    
                    if(myarray[i][j] > max){myarray[i][j]=max;}
                    if(myarray[i][j] < min){myarray[i][j]=min;}
                                        
                    //if(min > myarray[i][j]){min = myarray[i][j];}
		    //if(max < myarray[i][j]){max = myarray[i][j];}
		}
	    }
	    for(i = 0; i < width+1; i++){
		for(j = 0; j < height+1; j++){ // actual values
		  if(myslit[i][j] == 0){
                        
                        map = (int)MaestroA.mapper(myarray[i][j],1023.0,0.0,max,min);
                        
                      /*
                        if(myarray[i][j] >= 0.0){                        
                            if(myarray[i][j] < 10.0){map = (int)MaestroA.mapper(myarray[i][j],170.0,127.5,10.0,0);}
                            else if(myarray[i][j] < 100.0){map = (int)MaestroA.mapper(myarray[i][j],212.5,170.0,100.0,10.0);}
                            else if(myarray[i][j] < 1000.0){map = (int)MaestroA.mapper(myarray[i][j],255.0,212.5,1000.0,100.0);}
                            else if(myarray[i][j] > 1000.0){map = (int)MaestroA.mapper(1000.0,255.0,212.5,1000.0,100.0);}
                        }
                        else{
                            if(myarray[i][j] > -10.0){map = (int)MaestroA.mapper(myarray[i][j],85.0,127.5,85.0,-10.0);}
                            else if(myarray[i][j] > -100.0){map = (int)MaestroA.mapper(myarray[i][j],85.0,42.5,-10.0,-100.0);}
                            else if(myarray[i][j] > -1000.0){map = (int)MaestroA.mapper(myarray[i][j],42.5,0.0,-100.0,-1000.0);}
                            else if(myarray[i][j] < -1000.0){map = (int)MaestroA.mapper(-1000.0,42.5,0.0,-100.0,-1000.0);}
                        }
                        
                       */
                      //System.out.println(map+"   "+colorArray[map]);
                        rgbPixels[j*new_width+i] = colorArray[map].getRGB();
                  }
                  else if(myslit[i][j] == 1){ // green
                        map = 255;
                        rgbPixels[j*new_width+i] = new Color(0,255,0).getRGB();
                  }
                  else if(myslit[i][j] == 2){ // yellow
                        map = 255;
                        rgbPixels[j*new_width+i] = new Color(255,255,0).getRGB();
                  }
                  
                  rgbPixels[(height/2)*new_width+(width/2)] = new Color(255,255,255).getRGB();

                    //rgbPixels[j*new_width+i] = colorArray[map].getRGB();  
		}
	    } 
            
            myarray = null;
	    mis = new MemoryImageSource(new_width,height,rgbPixels,0,new_width);
	    mis.setAnimated(true);
	    mis.setFullBufferUpdates(false);
            im = createImage(mis);
            g.drawImage(im,LeftImageIndent,(getSize().height-height)/2,this);
	}
	
	private void drawImage(Graphics g){
	   g.drawImage(im,LeftImageIndent,(getSize().height-height)/2,this);
	}
	
	
	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){
		updatePixels(g);
		mis.newPixels(0,0,new_width,height);
		drawImage(g);
	}
		
}	

