import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.net.*;
import java.awt.event.*;

 
public class RectWaveGuideCrossCanvas extends Canvas{
	private static final Color bgcolor = new Color(216,230,230);
	private Image im;
	private Image im_palette;
	private MemoryImageSource mis;
	private int rgbPixels[];
	private int rgb_palette_Pixels[];
	private int new_width;
        private double max, min;
	private Color colorArray[];
        
        private int mapxy[][];
        public  double myarray[][];
        private double myarray_x[][];
        private double myarray_y[][];
        private double lean[][];
        private double maximoE = 0.0;
	
        private Graphics buf;
    
        private static final int width=400;
	private static final int height=400;
	private static final int palette_width=20, palette_height=340;
        //private static final int width = 350;
	//private static final int height = 350;
	//private static final int palette_width=20, palette_height=290;
        
	private static final int LeftMargin=25, TopMargin=10, BottomMargin=20;
	private static final int LeftImageIndent=35, LeftPaletteIndent=460;
        
        public int resolution, res1, res2;
        public boolean linecolor_ON = false;
        public boolean lineblack_ON = false;
        public boolean backcolor_ON = true;
        
        private RectWaveGuide_State state;
	private boolean DirtyFlag;
        
        public boolean IsX, IsY, IsTot, IsS, IsTE, IsTM;
        
	public RectWaveGuideCrossCanvas(RectWaveGuide_State state){
		super();
		this.state = state;
                setBackground(bgcolor);
                
                IsTot = true;
                IsX = false;
                IsY = false;
                IsS = false;
                IsTE = true;
                IsTM = false;
                
		new_width = (int)(width/state.a_to_b_ratio);
		rgbPixels = new int[new_width * height];
		rgb_palette_Pixels = new int[palette_width * palette_height];
		colorArray = new Color[256];
                mapxy = new int[new_width][height];
                myarray = new double[new_width][height];
                myarray_x = new double[new_width][height];
                myarray_y = new double[new_width][height];
                lean = new double[new_width][height];
                
		DirtyFlag = true;
                // HighRes = calculate value at each pixel
                // Medium Res = value every four pixels (almost as good visually)
                // Low Res = value every sixteen pixels (a bit grainy)
                // The best compromise is to use Medium Res
                resolution = 2; //1 = HighestRes; 2 = Medium Res; 3 = Low Res
                res1 = 35; //(470-height)/2;
                res2 = (470+height)/2-15;
                
                //Initialize the color palette
		// old 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));
		}
                */
                //new palette
                
                for(int i = 0; i < 128; i++) {
		    colorArray[i] = new Color((2550000/128*i)/10000,(2550000/128*i)/10000,255);
		}
		
		for(int i = 128; i < 256; i++) {
		    colorArray[i] = new Color(255,255-2*(i-128),255-2*(i-128));
		}
                                
		//mis = new MemoryImageSource(new_width,height,rgbPixels,0,new_width);
		//mis.setAnimated(true);
		//mis.setFullBufferUpdates(false);
		//im = createImage(mis);
	}
	
    @Override
	
        public void paint(Graphics g){
            if(im == null){
                im = createImage(getSize().width,getSize().height);
                buf = im.getGraphics();
                drawGraph(buf);
            }
            else{
                drawGraph(buf);
            }
            g.drawImage(im,0,0,null);
        }
            
        public void drawGraph(Graphics g){
            //g.draw3DRect(0,0,getSize().width-1,getSize().height-1,false);
            updatePixels(buf);
            drawLabels(buf);
	    
	    drawAxis(buf);
            if(!lineblack_ON || IsS){
                drawPalette(buf);
            }
            drawLabels2(buf);
        }
	@Override
	public void update(Graphics g){      
            paint(g);
	}
		
	private void updatePixels(Graphics g){
	    int i=0, j=0, map=0;
	    double x, y; // min, max;
	    //min =  state.getPoyntingMagnitude(state.a/2.0,state.b/2.0);
            //min = state.getFieldMagnitude(state.a/2.0,state.b/2.0);
            min = 0;
	    max =  min;
	    myarray = new double[new_width][height];
            myarray_x = new double[new_width][height];
            myarray_y = new double[new_width][height];
            mapxy = new int[new_width][height];
            lean = new double[new_width][height];
            maximoE = 0.0;
            
	    for(i = 0; i < new_width; i++){
		y = (new_width-1-i) * state.b/(new_width-1.0);
		for(j = 0; j < height; j++){
		    x = (height-1-j) * state.a/(height-1.0);
		    //myarray[i][j] = state.getPoyntingMagnitude(x,y);
                    
                    if(state.cut_off_frequency_for_mode > state.frequency){
                        myarray[i][j] = 0.0;
                        myarray_x[i][j] = 0.0;
                        myarray_y[i][j] = 0.0;
                        lean[i][j] = 0.0;
                    }
                    else{
                        if(IsTE){
                            if(IsX){
                                myarray[i][j] = state.getEFieldX(x,y);
                                myarray_x[i][j] = myarray[i][j];
                                myarray_y[i][j] = 0.0;
                                lean[i][j] = Math.abs(myarray_x[i][j]);   
                            }
                            else if(IsY){
                                myarray[i][j] = state.getEFieldY(x,y);
                                myarray_x[i][j] = 0.0;
                                myarray_y[i][j] = myarray[i][j];
                                lean[i][j] = Math.abs(myarray_y[i][j]);
                            }
                            else if(IsTot){
                                myarray[i][j] = state.getEFieldMagnitude(x,y);
                                myarray_x[i][j] = state.getEFieldX(x,y);
                                myarray_y[i][j] = state.getEFieldY(x,y);
                                lean[i][j] = Math.sqrt(myarray_x[i][j] * myarray_x[i][j] +
                                                       myarray_y[i][j] * myarray_y[i][j]);
                            }
                            else if(IsS){
                                myarray[i][j] = state.getPoyntingMagnitude(x,y);
                                lean[i][j] = myarray[i][j];
                            }
                        }
                        else if(IsTM){
                            if(IsX){
                                myarray[i][j] = state.getHFieldX(x,y);
                                myarray_x[i][j] = state.getHFieldX(x,y);
                                myarray_y[i][j] = 0.0;
                                lean[i][j] = Math.sqrt(myarray_x[i][j]);
                            }
                            else if(IsY){
                                myarray[i][j] = state.getHFieldY(x,y);
                                myarray_x[i][j] = 0.0;
                                myarray_y[i][j] = state.getHFieldY(x,y);
                                lean[i][j] = Math.sqrt(myarray_x[i][j]);
                            }
                            else if(IsTot){
                                myarray[i][j] = state.getHFieldMagnitude(x,y);
                                myarray_x[i][j] = state.getHFieldX(x,y);
                                myarray_y[i][j] = state.getHFieldY(x,y);
                                lean[i][j] = Math.sqrt(myarray_x[i][j] * myarray_x[i][j] +
                                                       myarray_y[i][j] * myarray_y[i][j]);
                            }
                            else if(IsS){
                                myarray[i][j] = state.getPoyntingMagnitude(x,y);
                                lean[i][j] = myarray[i][j];
                            }
                        }
                    }
		    //if(IsS || IsTot){
                      //  if(min > myarray[i][j]){min = myarray[i][j];}
                      //  if(max < myarray[i][j]){max = myarray[i][j];}
                    //}
                    //else{
                      //  if(max < myarray[i][j]){max = myarray[i][j];}
                    //}
		}
	    }
            
            min = MaestroA.getMin(myarray, new_width, height);
            max = MaestroA.getMax(myarray, new_width, height);
            maximoE = MaestroA.getMax(myarray, new_width, height);
            
            if(min == max){
                min = -1.0;
                max = 1.0;
            }
            //else{
              //  if(IsX || IsY){min = - max;}
            //}
                       
            //System.out.println("MAXIMO E = "+maximoE+"   min = "+min+"   max = "+max);
	    //for(i = 0; i < new_width; i++){
		//for(j = 0; j < height; j++){
		  ////map = (int)MaestroA.mapper(myarray[i][j],255.0,0.0,max,min);
		  ////rgbPixels[j*new_width+i] = colorArray[map].getRGB();
                  ////mapxy[i][j] = (int)MaestroA.mapper(myarray[i][j],255.0,0.0,max,min);
                  
                  ////map = (int)MaestroA.mapper(leano/maximoE,255.0,0.0,1.0,0.0);
                  ////System.out.println(map+"  "+colorArray[map].getRGB());
                  ////g.setColor(new Color((int)(Math.random()*225),(int)(Math.random()*225),(int)(Math.random()*225)));
                  
                  //map = (int)MaestroA.mapper(lean[i][j]/maximoE,255.0,0.0,1.0,0.0);
                      //if(map < 128){
                        //g.setColor(new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255));                        
                      //}
                      //else{
                        //g.setColor(new Color(255,255-2*(map-128),255-2*(map-128)));
                      //}
                  //g.fillRect(i,j,1,1);
		//}
	    //} 
	    
              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,res1,this);
	}
	
	private void drawImage(Graphics g){
	   g.drawImage(im,LeftImageIndent,res1,this);
	}
	
	public void drawPalette(Graphics g){
	    int i, j, map;
            
            g.setColor(Color.black);
            
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            
	    FontMetrics fm = g.getFontMetrics();
	    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] = colorArray[map].getRGB();
		}
	    }
	    im_palette = createImage(new MemoryImageSource(palette_width,palette_height,
				rgb_palette_Pixels,0,palette_width));
	    g.drawImage(im_palette,LeftPaletteIndent,res1+13,this);
	    
            //g.setColor(bgcolor);
            //g.fillRect(LeftPaletteIndent+palette_width+3,getSize().height/2-height/2-17,25,205);
            g.setColor(Color.black);
            
            MaestroG.subscripter(" +","","",g,14,LeftPaletteIndent+palette_width+3,res1+20);
	    if(IsTot){ // Color means magnitude of the Field
                //MaestroG.subscripter(" 0","","",g,12,LeftPaletteIndent+palette_width+8,getSize().height/2-7);
                //MaestroG.subscripterB("_","","",g,14,LeftPaletteIndent+palette_width+8,res2-37);
                MaestroG.subscripter("0","","",g,14,LeftPaletteIndent+palette_width+8,res2-32);
                if(IsTE){
                    MaestroG.subscripter("| E |","","",g,14,LeftPaletteIndent+palette_width-21,res2-10);
                }
                else if(IsTM){
                    MaestroG.subscripter("| H |","","",g,14,LeftPaletteIndent+palette_width-21,res2-10);
                }
            }
            else if(IsS){
               MaestroG.subscripter("0","","",g,12,LeftPaletteIndent+palette_width+8,res2-32); 
            }
            else{}
            
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
            
            // Black frame for palette 
            g.drawRect(LeftPaletteIndent,res1+13,
			palette_width,palette_height);
            
	}
	
	private void drawLabels(Graphics g){
	    g.clearRect(2,2,getSize().width-2,50);
            Graphics2D g2d = (Graphics2D)g;
            g.setFont(new Font("SanSerif",Font.PLAIN,12));
	    FontMetrics fm = g.getFontMetrics();
	    g.setColor(Color.black);
	    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            if(IsS){
                g.drawString("Power Density Magnitude",150,5);
            }
            else if(IsTot){
                if(IsTE){
                    g.drawString("Transverse Electric Field",(getSize().width-fm.stringWidth("Transverse Electric Field"))/2,getSize().height-20);
                }
                else if(IsTM){
                    g.drawString("Transverse Magnetic Field",(getSize().width-fm.stringWidth("Transverse Magnetic Field"))/2,getSize().height-20);
                }
            }
	}
	
	private void drawLabels2(Graphics g){
	    Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            
            g.setFont(new Font("SanSerif",Font.BOLD,12));
	    FontMetrics fm = g.getFontMetrics();
	    //g.setColor(Color.black);
	    //g.clearRect((getSize().width-fm.stringWidth("TM"+state.mode_number1+" "+state.mode_number2))-15,getSize().height-2*BottomMargin-33,80,30);
	    
            if(state.IsTEmode){
		g.setColor(Color.red.darker());
		MaestroG.subscripterB("TE",""+state.mode_number1+","+state.mode_number2,"",g,14,(getSize().width-fm.stringWidth("TM"+state.mode_number1+" "+state.mode_number2))-20,25);
	    }
	    else{
		g.setColor(Color.blue.darker());
		MaestroG.subscripterB("TM",""+state.mode_number1+","+state.mode_number2,"",g,14,(getSize().width-fm.stringWidth("TM"+state.mode_number1+" "+state.mode_number2))-20,25);
	    }
            
            g.setFont(new Font("SanSerif",Font.BOLD,11));
            if(IsS){
                g.setColor(Color.magenta.darker());
                MaestroG.subscripterAlt("Power Density Magnitude - S ","z","",g,11,(getSize().width-fm.stringWidth("Power Density Magnitude - S ( x, y )"))/2,getSize().height-5);
            }
            else if(IsTot){
                if(IsTE){
                    g.setColor(Color.red.darker());
                    g.drawString("Transverse Electric Field - E ( x, y )",(getSize().width-fm.stringWidth("Transverse Electric Field - E ( x, y )"))/2,getSize().height-5);
                }
                else if(IsTM){
                    g.setColor(Color.blue.darker());
                    g.drawString("Transverse Magnetic Field - H ( x, y )",(getSize().width-fm.stringWidth("Transverse Magnetic Field - H ( x, y )"))/2,getSize().height-5);
                }
            }
	}
	
	
	private void drawAxis(Graphics g){
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            
            Color linecolor;
            linecolor = Color.black;
	    //g.clearRect(2,50,getSize().width-60,getSize().height-70);
            int i, j, map;
            int begin, end, btest, brest, bnum1, bnum2, istep, start1, start2, startmap1, startmap2;
            
            
            //  ORIGINAL SETTING------------------------------------------------
            istep = 19; //15;
            startmap1 = LeftImageIndent-10 + istep/2;
            startmap2 = res1-30 + istep/2;
            
            start1 = LeftImageIndent-10 + istep/2;
            start2 = res1-30 + istep/2;
            
            //------------------------------------------------------------------
                                    
            btest = (new_width/istep)*istep;
            brest = (new_width - btest)/2;
            bnum1 = btest/istep;
            bnum2 = height/istep;
            
            g.setColor(bgcolor);
            g.fillRect(0,0,getSize().width,getSize().height);
            
            // Black Background when E or H field lines are displayed - Not for Poynting vector        
            if(linecolor_ON && !backcolor_ON && !IsS){
                g.setColor(Color.black);
                g.fillRect(LeftImageIndent-4,res1-25,new_width+8,height+8);
            }
            else if (lineblack_ON && !backcolor_ON && !IsS){
                g.setColor(Color.white);
                g.fillRect(LeftImageIndent-4,res1-25,new_width+8,height+8);
            }
            
            if(resolution == 1){
                for(i = 0; i < (new_width); i++){
                    for(j = 0; j < (height); j++){
                      
                      map = (int)MaestroA.mapper(lean[i][j]/maximoE,255.0,0.0,1.0,0.0);
                          if(map < 128){
                              {
                                    g.setColor(new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255));                        
                              }
                          }
                          else{
                            g.setColor(new Color(255,255-2*(map-128),255-2*(map-128)));
                          }
                      g.fillRect(LeftImageIndent+i,startmap2+j+1,1,1);
                    }
                }
            }
            else if(resolution == 2){
                map = 0;
                for(i = 0; i < (new_width/2+1); i++){
                    for(j = 0; j < (height/2+1); j++){
                      
                      if(i>=new_width/2 && j>=height/2){
                          map = (int)MaestroA.mapper(lean[(i-1)*2+1][(j-1)*2+1]/maximoE,255.0,0.0,1.0,0.0);
                      }
                      else if(i>=new_width/2){
                          map = (int)MaestroA.mapper(lean[(i-1)*2+1][j*2]/maximoE,255.0,0.0,1.0,0.0);
                      }
                      else if(j>=height/2){
                          map = (int)MaestroA.mapper(lean[i*2][(j-1)*2+1]/maximoE,255.0,0.0,1.0,0.0);
                      }
                      else if(i<new_width/2 && j<height/2){
                          map = (int)MaestroA.mapper(lean[i*2][j*2]/maximoE,255.0,0.0,1.0,0.0);
                      }
                      
                          if(map < 128 && map>0){
                            g.setColor(new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255));                        
                          }
                          else if(map == 0){
                              g.setColor(Color.blue);
                          }
                          else{
                            if(map < 0){
                                g.setColor(Color.green);
                            }
                            if(map> 255){
                                g.setColor(Color.magenta);
                                System.out.println("Magenta OMG  !!!!! "+map);
                            }
                            else
                            {
                                //if((255-2*(map-128))>255){System.out.println("OMG  !!!!! "+map);}
                                g.setColor(new Color(255,255-2*(map-128),255-2*(map-128)));
                            }
                          }
                      if(backcolor_ON || IsS){
                        g.fillRect(LeftImageIndent-1+i*2,startmap2+j*2,2,2);
                      }
                    }
                }
            }
            else if(resolution == 3){
                map = 0;
                for(i = 0; i < (new_width/4+1); i++){
                    for(j = 0; j < (height/4+1); j++){
                      
                      if(i>=new_width/4 && j>=height/4){
                          map = (int)MaestroA.mapper(lean[(i-1)*4+1][(j-1)*4+1]/maximoE,255.0,0.0,1.0,0.0);
                      }
                      else if(i>=new_width/4){
                          map = (int)MaestroA.mapper(lean[(i-1)*4+1][j*4]/maximoE,255.0,0.0,1.0,0.0);
                      }
                      else if(j>=height/4){
                          map = (int)MaestroA.mapper(lean[i*4][(j-1)*4+1]/maximoE,255.0,0.0,1.0,0.0);
                      }
                      else if(i<new_width/4 && j<height/4){
                          map = (int)MaestroA.mapper(lean[i*4][j*4]/maximoE,255.0,0.0,1.0,0.0);
                      }
                          if(map < 128){
                            g.setColor(new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255));                        
                          }
                          else{
                            g.setColor(new Color(255,255-2*(map-128),255-2*(map-128)));
                          }
                      g.fillRect(startmap1+i*4,startmap2+j*4,4,4);
                    }
                }
            }
            
            /*else{
                if(resolution == 1){
                    for(i = 0; i < (new_width); i++){
                        for(j = 0; j < (height); j++){

                          map = (int)MaestroA.mapper(lean[i][j]/maximoE,255.0,0.0,1.0,0.0);
                              if(map < 128){
                                g.setColor(new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255));                        
                              }
                              else{
                                g.setColor(new Color(255,255-2*(map-128),255-2*(map-128)));
                              }
                          g.fillRect(startmap1+i+1,startmap2+j+1,1,1);
                        }
                    }
                }
                else if(resolution == 2){
                    map = 0;
                    for(i = 0; i < (new_width/2+1); i++){
                        for(j = 0; j < (height/2+1); j++){

                          if(i>=new_width/2 && j>=height/2){
                              map = (int)MaestroA.mapper(lean[(i-1)*2+1][(j-1)*2+1]/maximoE,255.0,0.0,1.0,0.0);
                          }
                          else if(i>=new_width/2){
                              map = (int)MaestroA.mapper(lean[(i-1)*2+1][j*2]/maximoE,255.0,0.0,1.0,0.0);
                          }
                          else if(j>=height/2){
                              map = (int)MaestroA.mapper(lean[i*2][(j-1)*2+1]/maximoE,255.0,0.0,1.0,0.0);
                          }
                          else if(i<new_width/2 && j<height/2){
                              map = (int)MaestroA.mapper(lean[i*2][j*2]/maximoE,255.0,0.0,1.0,0.0);
                          }
                              if(map < 128){
                                g.setColor(new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255));                        
                              }
                              else{
                                g.setColor(new Color(255,255-2*(map-128),255-2*(map-128)));
                              }
                          g.fillRect(startmap1+i*2,startmap2+j*2,2,2);
                        }
                    }
                }
                else if(resolution == 3){
                    map = 0;
                    for(i = 0; i < (new_width/4+1); i++){
                        for(j = 0; j < (height/4+1); j++){

                          if(i>=new_width/4 && j>=height/4){
                              map = (int)MaestroA.mapper(lean[(i-1)*4+1][(j-1)*4+1]/maximoE,255.0,0.0,1.0,0.0);
                          }
                          else if(i>=new_width/4){
                              map = (int)MaestroA.mapper(lean[(i-1)*4+1][j*4]/maximoE,255.0,0.0,1.0,0.0);
                          }
                          else if(j>=height/4){
                              map = (int)MaestroA.mapper(lean[i*4][(j-1)*4+1]/maximoE,255.0,0.0,1.0,0.0);
                          }
                          else if(i<new_width/4 && j<height/4){
                              map = (int)MaestroA.mapper(lean[i*4][j*4]/maximoE,255.0,0.0,1.0,0.0);
                          }
                              if(map < 128){
                                g.setColor(new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255));                        
                              }
                              else{
                                g.setColor(new Color(255,255-2*(map-128),255-2*(map-128)));
                              }
                          g.fillRect(startmap1+i*4,startmap2+j*4,4,4);
                        }
                    }
                }   
            }*/
            g.setColor(Color.black);
	    //Vertical axis
	    g.drawLine(LeftImageIndent/2,res2+18,LeftImageIndent/2,res1-15);
	    //Draw Arrow
	    MaestroG.drawArrow(LeftImageIndent/2,res1-15,5,g);
	    
	    //horizontal axis
	    //g.drawLine(8,res2+10,new_width+50,res2+10);
	    //Draw Arrow
	    //MaestroG.drawArrow(new_width+50,res2+10,7,g);
            
            
            
            g.drawLine(8,res2+10,435,res2+10);
	    //Draw Arrow
	    MaestroG.drawArrow(435,res2+10,7,g);
	    
            //normal (z) axis --------------------------------------------------
            g.setColor(Color.white);
            MaestroG.fillCircle(LeftImageIndent/2,res2+10, 10, g);
            g.setColor(Color.black);
            MaestroG.drawCircle(LeftImageIndent/2,res2+10, 10, g);
            g.drawLine(LeftImageIndent/2-3,res2-3+10,LeftImageIndent/2+3,res2+3+10);
            g.drawLine(LeftImageIndent/2-3,res2+3+10,LeftImageIndent/2+3,res2-3+10);
            //------------------------------------------------------------------
            
	    g.setFont(new Font("SanSerif",Font.PLAIN,12));
	    FontMetrics fm = g.getFontMetrics();
	    g.drawString("x",5,15);
	    //g.drawString("y",new_width+55,res2+25);
            g.drawString("y",440,res2+25);
            
            g.drawString("z",5,res2+25);
	    
	    //draw plates
	    g.drawRect(LeftImageIndent-5,res1-25,new_width+9,height+9);
	    g.drawRect(LeftImageIndent-2,res1-22,new_width+3,height+3);
	    g.setColor(Color.cyan);
	    g.drawRect(LeftImageIndent-3,res1-23,new_width+5,height+5);
	    g.drawRect(LeftImageIndent-4,res1-24,new_width+7,height+7);
            
            g.setColor(Color.black);
            double leano = 0.0;
            double arrow_angle = 18.0;
            int rad = 6; // scaling factor for x and y component of field to trace stem of vector
            double igo, jgo, igo2, jgo2;
	    int ii = 0; int jj = 0;
            double i1,i2,i3,i4;
            int linethick = 1;
            double Leng = 6.0;// scaling factor for arrow
            double angolo = 0.0; double angolo2 = 0.0;
            double angus, angus2; double plus1, plus2, plus3, plus4; int iplus1, iplus2, iplus3, iplus4;
            
            if(!IsS){
            
            if(state.mode_number2 == 0) // first set of parallel waveguide modes
            {            
                for(i = 0; i < bnum1-1; i++){
                    for(j = 0; j < bnum2-1; j++){
                      ii = (i+1)*istep;
                      jj = (j+1)*istep;

                      leano = lean[ii][jj];
                      if(leano <= 0.05*maximoE){rad = 4; Leng = 3.0; linethick = 1;}
                      else if(leano <= 0.25*maximoE && leano > 0.05*maximoE){rad = 4; Leng = 4.0; linethick = 1;}
                      else if(leano <= 0.5*maximoE && leano > 0.25*maximoE){rad = 5; Leng = 4.0; linethick = 1;}
                      else if(leano <= 0.75*maximoE && leano > 0.5*maximoE){rad = 6; Leng = 5.0; linethick = 2;}
                      else if(leano > 0.75*maximoE){rad = 7; Leng = 6.0; linethick = 2;}
                      
                      igo = (double)(start1 + brest + ii);
                      jgo = (double)(start2 + jj);
                      igo2 = rad*myarray_y[ii][jj]/leano;
                      jgo2 = rad*myarray_x[ii][jj]/leano;
                      
                      map = (int)MaestroA.mapper(leano/maximoE,255.0,0.0,1.0,0.0);
                      
                      if(!backcolor_ON && linecolor_ON && !lineblack_ON && !IsS){
                          if(map < 128){
                            g.setColor(new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255));
                            linecolor = new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255);
                          }
                          else{
                            g.setColor(new Color(255,255-2*(map-128),255-2*(map-128)));
                            linecolor = new Color(255,255-2*(map-128),255-2*(map-128));
                          }
                      }
                      else if(!backcolor_ON && !linecolor_ON && lineblack_ON && !IsS){
                          linecolor = Color.black;
                      }
                      //--------------------------------------------------------
                      i1=igo-igo2; i2 = jgo-jgo2; i3 = igo+igo2; i4 = jgo+jgo2;
                      angus = Math.atan((i4-i2)/(i3-i1));
                      angus2 = arrow_angle*Math.PI/180.0;
                      //--------------------------------------------------------
                      plus1 = i4-Leng*Math.sin(angus + angus2);              
                      iplus1 = (int)plus1; 
                      if(Math.abs(plus1-iplus1)<0.5){
                          plus1 = Math.abs(iplus1)*Math.signum(plus1);
                      }
                      else{
                          plus1 = Math.abs(iplus1+1)*Math.signum(plus1);
                      }
                      //--------------------------------------------------------
                      plus2 = i4+Leng*Math.sin(angus + angus2);
                      iplus2 = (int)plus2; 
                      if(Math.abs(plus2-iplus2)<0.5){
                          plus2 = Math.abs(iplus2)*Math.signum(plus2);
                      }
                      else{
                          plus2 = Math.abs(iplus2+1)*Math.signum(plus2);
                      }
                      //--------------------------------------------------------
                      
                      drawLineThick(g,i1,i2,i3,i4,linethick,linecolor);
                      //--------------------------------------------------------
                      if(i1>i3)
                      {   
                        drawLineThick(g,i1,i2,i1-Leng*Math.cos(angus + angus2),plus1,linethick,linecolor);
                        //drawLineThick(g,i1,i2,i1-Leng*Math.cos(angus - angus2),i2-Leng*Math.sin(angus - angus2),linethick,linecolor);
                        drawLineThick(g,i1,i2,i1-Leng*Math.cos(angus + angus2),plus2,linethick,linecolor);
                      }
                      else{
                        drawLineThick(g,i1,i2,i1+Leng*Math.cos(angus+ angus2),plus2,linethick,linecolor);
                        //drawLineThick(g,i1,i2,i1+Leng*Math.cos(angus- angus2),i2+Leng*Math.sin(angus- angus2),linethick,linecolor);
                        drawLineThick(g,i1,i2,i1+Leng*Math.cos(angus+ angus2),plus1,linethick,linecolor);
                      }
                    }
                }
	    }
            
            else if(state.mode_number1 == 0){  // second set of parallel waveguide modes
                for(i = 0; i < bnum1-1; i++){
                    for(j = 0; j < bnum2-1; j++){
                      ii = (i+1)*istep;
                      jj = (j+1)*istep;

                      leano = lean[ii][jj];
                      
                      if(leano <= 0.05*maximoE){rad = 4; Leng = 3.0; linethick = 1;}
                      else if(leano <= 0.25*maximoE && leano > 0.05*maximoE){rad = 4; Leng = 4.0; linethick = 1;}
                      else if(leano <= 0.5*maximoE && leano > 0.25*maximoE){rad = 5; Leng = 4.0; linethick = 1;}
                      else if(leano <= 0.75*maximoE && leano > 0.5*maximoE){rad = 6; Leng = 5.0; linethick = 2;}
                      else if(leano > 0.75*maximoE){rad = 7; Leng = 6.0; linethick = 2;}
                      
                      igo = (double)(start1 + brest + ii);
                      jgo = (double)(start2 + jj);
                      igo2 = rad*myarray_y[ii][jj]/leano;
                      jgo2 = rad*myarray_x[ii][jj]/leano;

                      map = (int)MaestroA.mapper(leano/maximoE,255.0,0.0,1.0,0.0);
                      
                      if(!backcolor_ON && linecolor_ON && !lineblack_ON && !IsS){
                          if(map < 128){
                            g.setColor(new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255));
                            linecolor = new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255);
                          }
                          else{
                            g.setColor(new Color(255,255-2*(map-128),255-2*(map-128)));
                            linecolor = new Color(255,255-2*(map-128),255-2*(map-128));
                          }
                      }
                      else if(!backcolor_ON && !linecolor_ON && lineblack_ON && !IsS){
                          linecolor = Color.black;
                      }
                      
                      //--------------------------------------------------------
                      i1=igo-igo2; i2 = jgo-jgo2; i3 = igo+igo2; i4 = jgo+jgo2;
                      angus = Math.PI; //Math.atan((i4-i2)/(i3-i1));
                      angus2 = arrow_angle*Math.PI/180.0;
                      //--------------------------------------------------------
                      plus1 = i1-Leng*Math.sin(angus - angus2);              
                      iplus1 = (int)plus1; 
                      if(Math.abs(plus1-iplus1)<0.5){
                          plus1 = Math.abs(iplus1)*Math.signum(plus1);
                      }
                      else{
                          plus1 = Math.abs(iplus1+1)*Math.signum(plus1);
                      }
                      //--------------------------------------------------------
                      plus2 = i1+Leng*Math.sin(angus - angus2);
                      iplus2 = (int)plus2; 
                      if(Math.abs(plus2-iplus2)<0.5){
                          plus2 = Math.abs(iplus2)*Math.signum(plus2);
                      }
                      else{
                          plus2 = Math.abs(iplus2+1)*Math.signum(plus2);
                      }
                      //--------------------------------------------------------
                      
                      drawLineThick(g,i1,i2,i3,i4,linethick,linecolor);
                      //--------------------------------------------------------
                      if(i2>i4)
                      {   
                        drawLineThick(g,i1,i2,plus1,i2+Leng*Math.cos(angus- angus2),linethick,linecolor);
                        drawLineThick(g,i1,i2,plus2,i2+Leng*Math.cos(angus- angus2),linethick,linecolor);
                      }
                      else{
                        drawLineThick(g,i1,i2,plus2,i2-Leng*Math.cos(angus- angus2),linethick,linecolor);
                        drawLineThick(g,i1,i2,plus1,i2-Leng*Math.cos(angus- angus2),linethick,linecolor);
                      }     
                    }
                }
            }
            else // rectangular modes
            
            {    
                if (bnum1/2 == ((bnum1-1)/2) ){// bnum1 == odd ---> even number of field lines
                    for(i = 0; i < (bnum1-1)/2; i++){
                    for(j = 0; j < bnum2-1; j++){
                          ii = new_width/2+1 -istep/2 - (i)*istep;
                          jj = (j+1)*istep;

                          leano = lean[ii][jj];

                          if(leano <= 0.05*maximoE){rad = 4; Leng = 3.0; linethick = 1;}
                          else if(leano <= 0.25*maximoE && leano > 0.05*maximoE){rad = 4; Leng = 4.0; linethick = 1;}
                          else if(leano <= 0.5*maximoE && leano > 0.25*maximoE){rad = 5; Leng = 4.0; linethick = 1;}
                          else if(leano <= 0.75*maximoE && leano > 0.5*maximoE){rad = 6; Leng = 5.0; linethick = 2;}
                          else if(leano > 0.75*maximoE){rad = 7; Leng = 6.0; linethick = 2;}
                            
                          igo = (double)(start1 + ii);
                          jgo = (double)(start2 + jj);
                          igo2 = rad*myarray_y[ii][jj]/leano;
                          jgo2 = rad*myarray_x[ii][jj]/leano;

                          map = (int)MaestroA.mapper(leano/maximoE,255.0,0.0,1.0,0.0);
                          
                          if(!backcolor_ON && linecolor_ON && !lineblack_ON && !IsS){
                              if(map < 128){
                                g.setColor(new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255));
                                linecolor = new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255);
                              }
                              else{
                                g.setColor(new Color(255,255-2*(map-128),255-2*(map-128)));
                                linecolor = new Color(255,255-2*(map-128),255-2*(map-128));
                              }
                          }
                          else if(!backcolor_ON && !linecolor_ON && lineblack_ON && !IsS){
                            linecolor = Color.black;
                          }
                          //--------------------------------------------------------
                          i1=igo-igo2; i2 = jgo-jgo2; i3 = igo+igo2; i4 = jgo+jgo2;
                          angus = Math.atan((i4-i2)/(i3-i1));
                          angus2 = arrow_angle*Math.PI/180.0;
                          drawLineThick(g,i1,i2,i3,i4,linethick,linecolor);

                          if(i1>i3)
                          {   
                            drawLineThick(g,i1,i2,i1-Leng*Math.cos(angus+ angus2),i2-Leng*Math.sin(angus+ angus2),linethick,linecolor);
                            drawLineThick(g,i1,i2,i1-Leng*Math.cos(angus- angus2),i2-Leng*Math.sin(angus- angus2),linethick,linecolor);
                          }
                          else{
                            drawLineThick(g,i1,i2,i1+Leng*Math.cos(angus+ angus2),i2+Leng*Math.sin(angus+ angus2),linethick,linecolor);
                            drawLineThick(g,i1,i2,i1+Leng*Math.cos(angus- angus2),i2+Leng*Math.sin(angus- angus2),linethick,linecolor);
                          }     
                        }
                    }
                    
                    for(i = 0; i < (bnum1-1)/2; i++){
                    for(j = 0; j < bnum2-1; j++){
                        
                          ii = new_width/2+1 + istep/2 + (i)*istep;
                          jj = (j+1)*istep;

                          leano = lean[ii][jj];

                            if(leano <= 0.05*maximoE){rad = 4; Leng = 3.0; linethick = 1;}
                            else if(leano <= 0.25*maximoE && leano > 0.05*maximoE){rad = 4; Leng = 4.0; linethick = 1;}
                            else if(leano <= 0.5*maximoE && leano > 0.25*maximoE){rad = 5; Leng = 4.0; linethick = 1;}
                            else if(leano <= 0.75*maximoE && leano > 0.5*maximoE){rad = 6; Leng = 5.0; linethick = 2;}
                            else if(leano > 0.75*maximoE){rad = 7; Leng = 6.0; linethick = 2;}

                      //if(leano <= 0.05*maximoE){rad = 3; Leng = 1.0; linethick = 1;}
                      //else if(leano <= 0.25*maximoE && leano > 0.05*maximoE){rad = 3; Leng = 2.0; linethick = 1;}
                      //else if(leano <= 0.5*maximoE && leano > 0.25*maximoE){rad = 4; Leng = 3.0; linethick = 1;}
                      //else if(leano <= 0.75*maximoE && leano > 0.5*maximoE){rad = 5; Leng = 3.0; linethick = 2;}
                      //else if(leano > 0.75*maximoE){rad = 5; Leng = 4.0; linethick = 2;}
                      
                          igo = (double)(start1 + ii);
                          jgo = (double)(start2 + jj);
                          igo2 = rad*myarray_y[ii][jj]/leano;
                          jgo2 = rad*myarray_x[ii][jj]/leano;

                          map = (int)MaestroA.mapper(leano/maximoE,255.0,0.0,1.0,0.0);
                          
                          if(!backcolor_ON && linecolor_ON && !lineblack_ON && !IsS){
                              if(map < 128){
                                g.setColor(new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255));
                                linecolor = new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255);
                              }
                              else{
                                g.setColor(new Color(255,255-2*(map-128),255-2*(map-128)));
                                linecolor = new Color(255,255-2*(map-128),255-2*(map-128));
                              }
                          }
                          else if(!backcolor_ON && !linecolor_ON && lineblack_ON && !IsS){
                                linecolor = Color.black;
                          }
                          //--------------------------------------------------------
                          i1=igo-igo2; i2 = jgo-jgo2; i3 = igo+igo2; i4 = jgo+jgo2;
                          angus = Math.atan((i4-i2)/(i3-i1));
                          angus2 = arrow_angle*Math.PI/180.0;
                          drawLineThick(g,i1,i2,i3,i4,linethick,linecolor);

                          if(i1>i3)
                          {   
                            drawLineThick(g,i1,i2,i1-Leng*Math.cos(angus+ angus2),i2-Leng*Math.sin(angus+ angus2),linethick,linecolor);
                            drawLineThick(g,i1,i2,i1-Leng*Math.cos(angus- angus2),i2-Leng*Math.sin(angus- angus2),linethick,linecolor);
                          }
                          else{
                            drawLineThick(g,i1,i2,i1+Leng*Math.cos(angus+ angus2),i2+Leng*Math.sin(angus+ angus2),linethick,linecolor);
                            drawLineThick(g,i1,i2,i1+Leng*Math.cos(angus- angus2),i2+Leng*Math.sin(angus- angus2),linethick,linecolor);
                          }     
                        }
                    }
                    
            }
            else{ // bnum1 == even ---> odd number of field lines
                  // The code below tries to symmetrize field lines as much as possible
                    for(i = 0; i < (bnum1-1)/2; i++){
                    for(j = 0; j < bnum2-1; j++){
                          ii = new_width/2+1 - (i+1)*istep;
                          jj = (j+1)*istep;

                          leano = lean[ii][jj];

                          if(leano <= 0.05*maximoE){rad = 4; Leng = 2.0; linethick = 1;}
                          else if(leano <= 0.25*maximoE && leano > 0.05*maximoE){rad = 5; Leng = 3.0; linethick = 1;}
                          else if(leano <= 0.5*maximoE && leano > 0.25*maximoE){rad = 6; Leng = 4.0; linethick = 1;}
                          else if(leano <= 0.75*maximoE && leano > 0.5*maximoE){rad = 7; Leng = 5.0; linethick = 2;}
                          else if(leano > 0.75*maximoE){rad = 8; Leng = 6.0; linethick = 2;}

                      //if(leano <= 0.05*maximoE){rad = 3; Leng = 1.0; linethick = 1;}
                      //else if(leano <= 0.25*maximoE && leano > 0.05*maximoE){rad = 3; Leng = 2.0; linethick = 1;}
                      //else if(leano <= 0.5*maximoE && leano > 0.25*maximoE){rad = 4; Leng = 3.0; linethick = 1;}
                      //else if(leano <= 0.75*maximoE && leano > 0.5*maximoE){rad = 5; Leng = 3.0; linethick = 2;}
                      //else if(leano > 0.75*maximoE){rad = 5; Leng = 4.0; linethick = 2;}
                      
                          igo = (double)(start1 + ii);
                          jgo = (double)(start2 + jj);
                          igo2 = rad*myarray_y[ii][jj]/leano;
                          jgo2 = rad*myarray_x[ii][jj]/leano;

                          map = (int)MaestroA.mapper(leano/maximoE,255.0,0.0,1.0,0.0);

                            if(!backcolor_ON && linecolor_ON && !lineblack_ON && !IsS){
                                if(map < 128){
                                    g.setColor(new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255));
                                    linecolor = new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255);
                                }
                                else{
                                    g.setColor(new Color(255,255-2*(map-128),255-2*(map-128)));
                                    linecolor = new Color(255,255-2*(map-128),255-2*(map-128));
                                }
                            }
                            else if(!backcolor_ON && !linecolor_ON && lineblack_ON && !IsS){
                                    linecolor = Color.black;
                            }
                      
                          //--------------------------------------------------------
                          i1=igo-igo2; i2 = jgo-jgo2; i3 = igo+igo2; i4 = jgo+jgo2;
                          angus = Math.atan((i4-i2)/(i3-i1));
                          angus2 = arrow_angle*Math.PI/180.0;
                          drawLineThick(g,i1,i2,i3,i4,linethick,linecolor);

                          if(i1>i3)
                          {   
                            drawLineThick(g,i1,i2,i1-Leng*Math.cos(angus+ angus2),i2-Leng*Math.sin(angus+ angus2),linethick,linecolor);
                            drawLineThick(g,i1,i2,i1-Leng*Math.cos(angus- angus2),i2-Leng*Math.sin(angus- angus2),linethick,linecolor);
                          }
                          else{
                            drawLineThick(g,i1,i2,i1+Leng*Math.cos(angus+ angus2),i2+Leng*Math.sin(angus+ angus2),linethick,linecolor);
                            drawLineThick(g,i1,i2,i1+Leng*Math.cos(angus- angus2),i2+Leng*Math.sin(angus- angus2),linethick,linecolor);
                          }     
                        }
                    }
                    
                    for(i = 0; i < (bnum1-1)/2; i++){
                    for(j = 0; j < bnum2-1; j++){
                          
                          ii = new_width/2+1 + (i+1)*istep;
                          jj = (j+1)*istep;

                          leano = lean[ii][jj];

                            if(leano <= 0.05*maximoE){rad = 4; Leng = 3.0; linethick = 1;}
                            else if(leano <= 0.25*maximoE && leano > 0.05*maximoE){rad = 4; Leng = 4.0; linethick = 1;}
                            else if(leano <= 0.5*maximoE && leano > 0.25*maximoE){rad = 5; Leng = 4.0; linethick = 1;}
                            else if(leano <= 0.75*maximoE && leano > 0.5*maximoE){rad = 6; Leng = 5.0; linethick = 2;}
                            else if(leano > 0.75*maximoE){rad = 7; Leng = 6.0; linethick = 2;}
                            
                      //if(leano <= 0.05*maximoE){rad = 3; Leng = 1.0; linethick = 1;}
                      //else if(leano <= 0.25*maximoE && leano > 0.05*maximoE){rad = 3; Leng = 2.0; linethick = 1;}
                      //else if(leano <= 0.5*maximoE && leano > 0.25*maximoE){rad = 4; Leng = 3.0; linethick = 1;}
                      //else if(leano <= 0.75*maximoE && leano > 0.5*maximoE){rad = 5; Leng = 3.0; linethick = 2;}
                      //else if(leano > 0.75*maximoE){rad = 5; Leng = 4.0; linethick = 2;}
                      
                          igo = (double)(start1 + ii);
                          jgo = (double)(start2 + jj);
                          igo2 = rad*myarray_y[ii][jj]/leano;
                          jgo2 = rad*myarray_x[ii][jj]/leano;

                          map = (int)MaestroA.mapper(leano/maximoE,255.0,0.0,1.0,0.0);

                            if(!backcolor_ON && linecolor_ON && !lineblack_ON && !IsS){
                                if(map < 128){
                                    g.setColor(new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255));
                                    linecolor = new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255);
                                }
                                else{
                                    g.setColor(new Color(255,255-2*(map-128),255-2*(map-128)));
                                    linecolor = new Color(255,255-2*(map-128),255-2*(map-128));
                                }
                            }
                            else if(!backcolor_ON && !linecolor_ON && lineblack_ON && !IsS){
                                    linecolor = Color.black;
                            }
                      
                          //--------------------------------------------------------
                          i1=igo-igo2; i2 = jgo-jgo2; i3 = igo+igo2; i4 = jgo+jgo2;
                          angus = Math.atan((i4-i2)/(i3-i1));
                          angus2 = arrow_angle*Math.PI/180.0;
                          drawLineThick(g,i1,i2,i3,i4,linethick,linecolor);

                          if(i1>i3)
                          {   
                            drawLineThick(g,i1,i2,i1-Leng*Math.cos(angus+ angus2),i2-Leng*Math.sin(angus+ angus2),linethick,linecolor);
                            drawLineThick(g,i1,i2,i1-Leng*Math.cos(angus- angus2),i2-Leng*Math.sin(angus- angus2),linethick,linecolor);
                          }
                          else{
                            drawLineThick(g,i1,i2,i1+Leng*Math.cos(angus+ angus2),i2+Leng*Math.sin(angus+ angus2),linethick,linecolor);
                            drawLineThick(g,i1,i2,i1+Leng*Math.cos(angus- angus2),i2+Leng*Math.sin(angus- angus2),linethick,linecolor);
                          }     
                        }
                    } 
                    
                    for(j = 0; j < bnum2-1; j++){
                          //ii = (i+1)*istep;
                          ii = new_width/2+1;
                          jj = (j+1)*istep;

                          leano = lean[ii][jj];

                            if(leano <= 0.05*maximoE){rad = 4; Leng = 3.0; linethick = 1;}
                            else if(leano <= 0.25*maximoE && leano > 0.05*maximoE){rad = 4; Leng = 4.0; linethick = 1;}
                            else if(leano <= 0.5*maximoE && leano > 0.25*maximoE){rad = 5; Leng = 4.0; linethick = 1;}
                            else if(leano <= 0.75*maximoE && leano > 0.5*maximoE){rad = 6; Leng = 5.0; linethick = 2;}
                            else if(leano > 0.75*maximoE){rad = 7; Leng = 6.0; linethick = 2;}

                      //if(leano <= 0.05*maximoE){rad = 3; Leng = 1.0; linethick = 1;}
                      //else if(leano <= 0.25*maximoE && leano > 0.05*maximoE){rad = 3; Leng = 2.0; linethick = 1;}
                      //else if(leano <= 0.5*maximoE && leano > 0.25*maximoE){rad = 4; Leng = 3.0; linethick = 1;}
                      //else if(leano <= 0.75*maximoE && leano > 0.5*maximoE){rad = 5; Leng = 3.0; linethick = 2;}
                      //else if(leano > 0.75*maximoE){rad = 5; Leng = 4.0; linethick = 2;}
                      
                          igo = (double)(start1 + ii);
                          jgo = (double)(start2 + jj);
                          igo2 = rad*myarray_y[ii][jj]/leano;
                          jgo2 = rad*myarray_x[ii][jj]/leano;

                          map = (int)MaestroA.mapper(leano/maximoE,255.0,0.0,1.0,0.0);

                            if(!backcolor_ON && linecolor_ON && !lineblack_ON && !IsS){
                                if(map < 128){
                                    g.setColor(new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255));
                                    linecolor = new Color((2550000/128*map)/10000,(2550000/128*map)/10000,255);
                                }
                                else{
                                    g.setColor(new Color(255,255-2*(map-128),255-2*(map-128)));
                                    linecolor = new Color(255,255-2*(map-128),255-2*(map-128));
                                }
                            }
                            else if(!backcolor_ON && !linecolor_ON && lineblack_ON && !IsS){
                                    linecolor = Color.black;
                            }
                      
                          //--------------------------------------------------------
                          i1=igo-igo2; i2 = jgo-jgo2; i3 = igo+igo2; i4 = jgo+jgo2;
                          angus = Math.atan((i4-i2)/(i3-i1));
                          angus2 = arrow_angle*Math.PI/180.0;
                          drawLineThick(g,i1,i2,i3,i4,linethick,linecolor);

                          if(i1>i3)
                          {   
                            drawLineThick(g,i1,i2,i1-Leng*Math.cos(angus+ angus2),i2-Leng*Math.sin(angus+ angus2),linethick,linecolor);
                            drawLineThick(g,i1,i2,i1-Leng*Math.cos(angus- angus2),i2-Leng*Math.sin(angus- angus2),linethick,linecolor);
                          }
                          else{
                            drawLineThick(g,i1,i2,i1+Leng*Math.cos(angus+ angus2),i2+Leng*Math.sin(angus+ angus2),linethick,linecolor);
                            drawLineThick(g,i1,i2,i1+Leng*Math.cos(angus- angus2),i2+Leng*Math.sin(angus- angus2),linethick,linecolor);
                          }     
                        }
                }  
              }
            }
	}
        
        private void drawLineThick(Graphics g, double x1, double y1, double x2, double y2, int thick, Color color){
	
            Graphics2D g2d = (Graphics2D)g;
            g2d.setPaint(color);
            g2d.setStroke(new BasicStroke(thick,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));

            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

            Line2D.Double line = new Line2D.Double(x1,y1,x2,y2);
            g2d.draw(line);

            g2d.setStroke(new BasicStroke(1));
            //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
        }
	
	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)];
	    
	}
        
}	

