//RectWaveGuideOutputPanel.java
import java.awt.*;
import java.awt.event.*;
//import java.applet.*;
//import java.lang.*;  
//import maestro.lib.math.*;
//import maestro.lib.graphics.*;

public class RectWaveGuideOutputPanel extends Panel implements ItemListener{
	RectWaveGuide_State state;
	//private static final Color bgcolor = Color.lightGray;
        private static final Color bgcolor = new Color(236,236,236);
	private static final Font labfont=new Font("SanSerif",Font.PLAIN,12);
	private static final Font titlefont=new Font("SanSerif",Font.BOLD,14);
	Label titlelabel;	
	public Choice c1;
        public Button about;
        
	RectWaveOutputCanvasA rwocA; 
	//public ScrollPane pane1, pane2;
	
	public RectWaveGuideOutputPanel(RectWaveGuide_State state){
	    super();
	    setLayout(null);
	    this.state = state;
	    setBackground(bgcolor);
	    titlelabel = new Label("Mode Properties");
	    add(titlelabel);
	    titlelabel.setBounds(5,5,150,20);    
	    titlelabel.setFont(titlefont);
            
            about = new Button("Instructions");
            // CHANGE ==========================================================
            //about.setBackground(new Color(255,255,255));
            about.setBackground(bgcolor);
            //==================================================================
            add(about);
	    about.setBounds(170,5,120,25);
            
	    //pane1 = new ScrollPane();
	    //pane1.setBounds(2,30,297,285);
	    //add(pane1);
	    
	    //pane2  = new ScrollPane();
	    //pane2.setBounds(2,30,297,205);
	    //add(pane2);

	    
	    
	    rwocA = new RectWaveOutputCanvasA(state);
	    //pane1.add(rwocA);
	    add(rwocA);
	    rwocA.setBounds(1,35,295,300);
	    
	    
	    //Listeners
	   
	}


public void itemStateChanged(ItemEvent evt){
	ItemSelectable ie = evt.getItemSelectable();
	    if(evt.getSource()==c1){
		if(ie.getSelectedObjects()[0]=="Output1"){
		    //pane1.setVisible(true);
		    //pane2.setVisible(false);
		}
		else if(ie.getSelectedObjects()[0]=="Output2"){
		    //pane1.setVisible(false);
		    //pane2.setVisible(true);
		}
	    }
    }	
}//RectWaveGuideOutputPanel.java
class RectWaveOutputCanvasA extends Canvas{
	
        private Image im;
        private Graphics buf;
    
        private static final Font normalfont = new Font("SanSerif",Font.PLAIN,12);
	private static final Font subfont    = new Font("SanSerif",Font.PLAIN,10);
	private static final Font symbolfont=new Font("Serif",Font.PLAIN,14);
	RectWaveGuide_State state;
	public RectWaveOutputCanvasA(RectWaveGuide_State state){
	    super();
	    this.state = state;
	}
	
        @Override
	 public void paint(Graphics g){
	    if(im == null){
		im = createImage(getSize().width,getSize().height);
		buf = im.getGraphics();
		drawCanvas(buf);
	    }
	    else{
		drawCanvas(buf);
	    }
	    g.drawImage(im,0,0,null);
    }
    
    
    public void update(Graphics g){
	paint(g);
    }
        
        public void drawCanvas(Graphics g){
            g.clearRect(0,0,getSize().width,getSize().height);
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

	    int x, y, dx, dxx, dy, dyy;
	    double v_normalized, f_normalized;
	    String stmp;
	    FontMetrics fm;
	    x = 10;
	    y = 15;
	    dx = 80;
	    dxx = 15;
	    g.setFont(normalfont);
	    fm = g.getFontMetrics();
	    dy = fm.getHeight();
	    dyy = dy+2;
	
	    	g.setColor(Color.red);	
	    if(state.mode_number1==1 && state.mode_number2==0){
		stmp = "";
		MaestroG.subscripter("The fundamental mode is the TE","10",stmp,g,12,x,y);
		
	    }
	    else if(state.mode_number1==0 || state.mode_number2==0 && state.mode_number1>1){
		stmp = "";
		MaestroG.subscripter("TE",""+state.mode_number1+","+state.mode_number2,stmp,g,12,x,y);
	    }
	    
		
	    else{
		stmp ="";
		MaestroG.subscripter("TE",""+state.mode_number1+","+state.mode_number2,stmp,g,12,x,y);
		MaestroG.subscripter("TM",""+state.mode_number1+","+state.mode_number2,stmp,g,12,x+dx,y);
	    }
		g.setColor(Color.black);
	    
		y += dyy;
		
		if(state.a == 0.0){
		
		    g.drawString("cutoff frequency         =         [ Hz ]",x,y);	
		    g.setFont(symbolfont);
		    g.drawString("\u221e",fm.stringWidth("cutoff frequency         = ")+x,y); 
		    g.setFont(normalfont);
		}
		else{
		if(state.cut_off_frequency_for_mode < 1.0E3 ){
		    f_normalized = state.cut_off_frequency_for_mode;
		    //g.drawString("    cutoff frequency         =  "+MaestroA.rounder(f_normalized,6)+" [ Hz ]",x,y);	
                    MaestroG.subscripterSer5("cutoff frequency        ","f","c"," =  "+MaestroA.rounder(f_normalized,6)+" [ Hz ]",g,12,x,y);	
		}
		else if(state.cut_off_frequency_for_mode < 1.0E6 && state.cut_off_frequency_for_mode > 1.0E3  ){
		    f_normalized = state.cut_off_frequency_for_mode/1.0E3;
		    //g.drawString("    cutoff frequency         =  "+MaestroA.rounder(f_normalized,6)+" [ kHz ]",x,y);	
                    MaestroG.subscripterSer5("cutoff frequency        ","f","c"," =  "+MaestroA.rounder(f_normalized,6)+" [ kHz ]",g,12,x,y);	
		}
		else if(state.cut_off_frequency_for_mode < 1.0E9 && state.cut_off_frequency_for_mode > 1.0E6 ){
		    f_normalized = state.cut_off_frequency_for_mode/1.0E6;
		    //g.drawString("    cutoff frequency         =  "+MaestroA.rounder(f_normalized,6)+" [ MHz ]",x,y);	
                    MaestroG.subscripterSer5("cutoff frequency        ","f","c"," =  "+MaestroA.rounder(f_normalized,6)+" [ MHz ]",g,12,x,y);	
		}
		else if(state.cut_off_frequency_for_mode < 1.0E12 && state.cut_off_frequency_for_mode > 1.0E9 ){
		    f_normalized = state.cut_off_frequency_for_mode/1.0E9;
		    //g.drawString("    cutoff frequency         =  "+MaestroA.rounder(f_normalized,6)+" [ GHz ]",x,y);	
                    MaestroG.subscripterSer5("cutoff frequency        ","f","c"," =  "+MaestroA.rounder(f_normalized,6)+" [ GHz ]",g,12,x,y);	
		}
		else if(state.cut_off_frequency_for_mode < 1.0E15 && state.cut_off_frequency_for_mode >1.0E12 ){
		    f_normalized = state.cut_off_frequency_for_mode/1.0E12;
		    //g.drawString("    cutoff frequency         =  "+MaestroA.rounder(f_normalized,6)+" [ THz ]",x,y);	
                    MaestroG.subscripterSer5("cutoff frequency        ","f","c"," =  "+MaestroA.rounder(f_normalized,6)+" [ THz ]",g,12,x,y);	
		}
		else{
		    f_normalized = state.cut_off_frequency_for_mode/1.0E12;
		    //g.drawString("    cutoff frequency         =  "+MaestroA.rounder(f_normalized,6)+" [ THz ]",x,y);
                    MaestroG.subscripterSer5("cutoff frequency        ","f","c"," =  "+MaestroA.rounder(f_normalized,6)+" [ THz ]",g,12,x,y);	
		}
		
		y += dyy;
		
		stmp = " = "+MaestroA.rounder(state.cut_off_wavelength,7)+" [ m ]";
		MaestroG.subscripterSer4("cutoff wavelength      ","\u03bb","c",stmp,g,12,x,y);
		//g.setFont(symbolfont);
		//g.drawString("\u03bb",fm.stringWidth("    cut-off wavelength     ")+x,y); 
		//g.setFont(normalfont);

		y += dyy+2;
		g.setColor(Color.blue);
		g.drawString("At the frequency of operation :",x,y);
		g.setColor(Color.black);
		y += dyy;
		
	    if(state.frequency>state.cut_off_frequency_for_mode){
		
                v_normalized = state.phase_velocity/1.0E8;
		
	        if(v_normalized < 1.0E3){
                    stmp="  = "+MaestroA.rounder(v_normalized,5);
                    MaestroG.subscripterSer6("phase velocity  ","u","pz",stmp,g,12,x,y);
                    MaestroG.superscripter("      [ 10","8"," m/s ]",g,12,x+2*dx+20,y);
                }
                else if(v_normalized >= 1.0E3 && v_normalized < 1.0E6 ){
                    stmp="  = "+MaestroA.rounder(v_normalized/1.0E3,3);
                    MaestroG.subscripterSer6("phase velocity  ","u","pz",stmp,g,12,x,y);
                    MaestroG.superscripter("      [ 10","11"," m/s ]",g,12,x+2*dx+20,y);
                }
                else if(v_normalized >= 1.0E6 && v_normalized < 1.0E9 ){
                    stmp="  = "+MaestroA.rounder(v_normalized/1.0E6,3);
                    MaestroG.subscripterSer6("phase velocity  ","u","pz",stmp,g,12,x,y);
                    MaestroG.superscripter("      [ 10","14"," m/s ]",g,12,x+2*dx+20,y);
                }
                else if(v_normalized >= 1.0E9){
                    stmp="  = "+MaestroA.rounder(v_normalized/1.0E9,3);
                    MaestroG.subscripterSer6("phase velocity  ","u","pz",stmp,g,12,x,y);
                    MaestroG.superscripter("      [ 10","17"," m/s ]",g,12,x+2*dx+20,y);
                }
                
                y += dyy;
                
	        v_normalized = state.group_velocity/1.0E8;
	        
                if(v_normalized >= 1.0){
                    stmp="  = "+MaestroA.rounder(v_normalized,5);
                    MaestroG.subscripterSer6("group velocity  ","u","gz",stmp,g,12,x,y);
                    MaestroG.superscripter("      [ 10","8"," m/s ]",g,12,x+2*dx+20,y);
                }
                else if(v_normalized < 1.0 && v_normalized >= 1.0E-3){
                    stmp="  = "+MaestroA.rounder(v_normalized*1.0E3,3);
                    MaestroG.subscripterSer6("group velocity  ","u","gz",stmp,g,12,x,y);
                    MaestroG.superscripter("      [ 10","5"," m/s ]",g,12,x+2*dx+20,y);
                }
                else if(v_normalized < 1.0E-3 && v_normalized >= 1.0E-6 ){
                    stmp="  = "+MaestroA.rounder(v_normalized*1.0E6,3);
                    MaestroG.subscripterSer6("group velocity  ","u","gz",stmp,g,12,x,y);
                    MaestroG.superscripter("      [ 10","2"," m/s ]",g,12,x+2*dx+20,y);
                }
                else if(v_normalized < 1.0E-6 && v_normalized >= 1.0E-9 ){
                    stmp="  = "+MaestroA.rounder(v_normalized*1.0E9,3);
                    MaestroG.subscripterSer6("group velocity  ","u","gz",stmp,g,12,x,y);
                    MaestroG.superscripter("      [ 10","-1"," m/s ]",g,12,x+2*dx+20,y);
                }
                else if(v_normalized < 1.0E-9){
                    stmp="  = "+MaestroA.rounder(v_normalized*1.0E12,3);
                    MaestroG.subscripterSer6("group velocity  ","u","gz",stmp,g,12,x,y);
                    MaestroG.superscripter("      [ 10","-4"," m/s ]",g,12,x+2*dx+20,y);
                }
                
	        y += dyy;
                double test;
                test = state.phase_velocity/state.frequency;
	        
                if(test < 1.0E3){
                    stmp = " = "+MaestroA.rounder(test,6)+" [ m ]";
                    MaestroG.subscripterSer4B("guide wavelength   ","\u03bb","g",stmp,g,12,x,y);
                }
                else if(test >= 1.0E3 && test < 1.0E6 ){
                    stmp = " = "+MaestroA.rounder(test/1.0E3,3);
                    MaestroG.subscripterSer4B("guide wavelength   ","\u03bb","g",stmp,g,12,x,y);
                    MaestroG.superscripter("      [ 10","3"," m ]",g,12,x+2*dx+20,y);
                }
                else if(test >= 1.0E6 && test < 1.0E9 ){
                    stmp = " = "+MaestroA.rounder(test/1.0E6,3);
                    MaestroG.subscripterSer4B("guide wavelength   ","\u03bb","g",stmp,g,12,x,y);
                    MaestroG.superscripter("      [ 10","6"," m ]",g,12,x+2*dx+20,y);
                }
                else if(test >= 1.0E9 ){
                    stmp = " = "+MaestroA.rounder(test/1.0E9,3);
                    MaestroG.subscripterSer4B("guide wavelength   ","\u03bb","g",stmp,g,12,x,y);
                    MaestroG.superscripter("      [ 10","9"," m ]",g,12,x+2*dx+20,y);
                }
                
                //stmp = " = "+MaestroA.rounder(state.phase_velocity/state.frequency,7)+" [ m ]";
	        //MaestroG.subscripterSer4B("  guide wavelength    ","\u03bb","g",stmp,g,12,x,y);
		
		y += dyy;
                test = state.impedance_TE;
                if(test < 1.0E3){
                    stmp = " = "+MaestroA.rounder(state.impedance_TE,4)+" [ \u03a9 ]";
                    MaestroG.subscripter("guide impedance   \u03b7","TE",stmp,g,12,x,y);
                }
                else if(test >= 1.0E3 && test < 1.0E6 ){
                    stmp = " = "+MaestroA.rounder(state.impedance_TE/1.0E3,3);
                    MaestroG.subscripter("guide impedance   \u03b7","TE",stmp,g,12,x,y);
                    MaestroG.superscripter("      [ 10","3"," \u03a9 ]",g,12,x+2*dx+20,y);
                }
                else if(test >= 1.0E6 && test < 1.0E9 ){
                    stmp = " = "+MaestroA.rounder(state.impedance_TE/1.0E6,3);
                    MaestroG.subscripter("guide impedance   \u03b7","TE",stmp,g,12,x,y);
                    MaestroG.superscripter("      [ 10","6"," \u03a9 ]",g,12,x+2*dx+20,y);
                }
                else if(test >= 1.0E9 ){
                    stmp = " = "+MaestroA.rounder(state.impedance_TE/1.0E9,3);
                    MaestroG.subscripter("guide impedance   \u03b7","TE",stmp,g,12,x,y);
                    MaestroG.superscripter("      [ 10","9"," \u03a9 ]",g,12,x+2*dx+20,y);
                }
                
		//g.setFont(symbolfont);
		//g.drawString("\u03b7",fm.stringWidth("  guide impedance   ")+x,y); 
		//g.setFont(normalfont);
	    
	      if(state.mode_number1>0 && state.mode_number2>0){
		y += dyy;
	        stmp = " = "+MaestroA.rounder(state.impedance_TM,4)+" [ \u03a9 ]";
	        MaestroG.subscripter("guide impedance   \u03b7","TM",stmp,g,12,x,y);

                //g.setFont(symbolfont);
		//g.drawString("\u03b7",fm.stringWidth("    guide impedance   ")+x,y); 
		//g.setFont(normalfont);
	      }
	            

	    }
	    else if (state.frequency < state.cut_off_frequency_for_mode){
		
		stmp=" = undefined";
	        MaestroG.subscripterSer6("phase velocity  ","u","pz",stmp,g,12,x,y);
	        
	        y += dyy;
	        
	        stmp="  = undefined";
	        MaestroG.subscripterSer6("group velocity  ","u","gz",stmp,g,12,x,y);
	       
	        y += dyy;
	        stmp = " = undefined";
	        MaestroG.subscripter("guide wavelength   \u03bb","g",stmp,g,12,x,y);
		
                //g.setFont(symbolfont);
		//g.drawString("\u03bb",fm.stringWidth("    guide wavelength   ")+x,y);
		//g.setFont(normalfont);
		
	    }
	    else if (state.frequency == state.cut_off_frequency_for_mode){
		
		stmp=" = \u221e";
	        MaestroG.subscripterSer6("phase velocity  ","u","pz",stmp,g,12,x,y);
		//g.setFont(symbolfont);
		//g.drawString("\u221e",fm.stringWidth("  phase velocity  vpz  =  ")+x,y);
		//g.setFont(normalfont);
	        
	        y += dyy;
	        
	        stmp="  = 0.0";
	        MaestroG.subscripterSer6("group velocity  ","u","gz",stmp,g,12,x,y);
	       
	        y += dyy;
	        stmp = " = \u221e";
	        MaestroG.subscripter("guide wavelength   \u03bb","g",stmp,g,12,x,y);
		//g.setFont(symbolfont);
		////g.drawString("\u03b2",fm.stringWidth("    guide wavelength  ")+x,y); 
		//g.drawString("\u221e",fm.stringWidth("  guide wavelength      = ")+x,y);
		//g.setFont(normalfont);
		
		
	    }
		y += dyy+2;
		g.setColor(Color.blue);
		g.drawString("Wave vector components :",x,y);
		g.setColor(Color.black);
		y += dyy;
		if(state.cut_off_frequency_for_mode < state.frequency){
		    stmp=" = "+MaestroA.rounder(2.0*Math.PI*state.frequency/state.phase_velocity,5);
		    MaestroG.subscripterSer("   k","z",stmp,g,12,x,y);
		    
                    stmp = " ]";
		    MaestroG.superscripter(" [ m","-1",stmp,g,12,x+2*dx+20,y);
		    //g.setFont(symbolfont);
		    //g.drawString("k",fm.stringWidth("      ")+x,y); 
		    //g.setFont(normalfont);

		    
	        }
		else if(state.cut_off_frequency_for_mode==state.frequency){
		    stmp=" = 0.0  -  Cut-off Condition";
		    MaestroG.subscripterSer("   k","z",stmp,g,12,x,y);
		    //g.setFont(symbolfont);
		    //g.drawString("k",fm.stringWidth("      ")+x,y); 
		    //g.setFont(normalfont);
	        }
		else if(state.cut_off_frequency_for_mode > state.frequency){
		    
		    g.setColor(Color.red);
		    stmp="EVANESCENT MODE";
		    MaestroG.subscripter("","",stmp,g,12,x,y);
		    y+=dyy;
		    g.setColor(Color.black);
		    double betacompl=MaestroA.rounder(Math.abs(Complex.Imaginary(state.BetaC)),5);
		    stmp=" = - j"+betacompl;
		    MaestroG.subscripterSer("   k","z",stmp,g,12,x,y);
		    //g.setFont(symbolfont);
		    //g.drawString("k",fm.stringWidth("      ")+x,y); 
		    //g.setFont(normalfont);
		    stmp = " ]";
		    MaestroG.superscripter(" [ m","-1",stmp,g,12,x+2*dx+20,y);
	        }
							
		y += dyy;
	        
	        stmp="  = "+MaestroA.rounder(state.mode_number1*Math.PI/state.a,5);
	        MaestroG.subscripterSer("   k","x",stmp,g,12,x,y);
                    //g.setFont(symbolfont);
		    //g.drawString("k",fm.stringWidth("      ")+x,y); 
		    //g.setFont(normalfont);
		stmp = " ]";
	        MaestroG.superscripter(" [ m","-1",stmp,g,12,x+2*dx+20,y);
		y += dyy;
	        
	        stmp="  = "+MaestroA.rounder(state.mode_number2*Math.PI/state.b,5);
	        MaestroG.subscripterSer("   k","y",stmp,g,12,x,y);
                    //g.setFont(symbolfont);
		    //g.drawString("k",fm.stringWidth("      ")+x,y); 
		    //g.setFont(normalfont);
		stmp = " ]";
	        MaestroG.superscripter(" [ m","-1",stmp,g,12,x+2*dx+20,y);
		
	    }
            
            y+=dyy;
            g.setColor(Color.black);
	    g.drawLine(5,y+dyy/4,getSize().width-15,y+dyy/4);
	   
	    y= y+dyy+dy;
	    
            g.setColor(Color.red);
	    g.drawString("Total number of propagating modes  =  "+state.TotalModes,x,y);
	    if(state.TotalModes == 1){
                y+=dyy;
                g.drawString("Monomode Condition",x,y);
            }
	        
	    }
	
}
