//RectWaveGuideOutputPanel.java
import java.awt.*;
import java.awt.event.*;

public class RectWaveGuideOutputPanel extends Panel implements ItemListener{
	RectWaveGuide_State state;
	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);
            setBackground(Color.white);
            
	    titlelabel = new Label("Mode Properties");
	    add(titlelabel);
	    titlelabel.setBounds(state.s5,state.s5,state.s150,state.s20);    
	    titlelabel.setFont(titlefont);
            
            int buttonx = state.s180;
            int buttony = state.s5;
            int buttonwide = state.s110;
            int buttonheight = state.s25;
        
            about = new Button("Instructions");
            // CHANGE ==========================================================
            about.setBackground(bgcolor);
            about.setFont(new Font("SanSerif",Font.PLAIN,state.font13));
            //==================================================================
            add(about);
	    about.setBounds(buttonx,buttony,buttonwide,buttonheight);
            Panel ps7 = new Panel();
	    ps7.setBackground(Color.black);
	    add(ps7);
	    ps7.setBounds(buttonx-1,buttony-1,buttonwide+2,buttonheight+2);
            
	    //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(state.s1,state.s35,state.s200+state.s95,state.s300);
	    
            //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 Font normalfont;
    private Font subfont;
    private Font symbolfont;
    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){
            normalfont = new Font("SanSerif",Font.PLAIN,state.font12);
            subfont    = new Font("SanSerif",Font.PLAIN,state.font10);
            symbolfont=new Font("Serif",Font.PLAIN,state.font14);
	
            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 = state.s10;
	    y = state.s15;
	    dx = state.s80;
	    dxx = state.s15;
	    g.setFont(normalfont);
	    fm = g.getFontMetrics();
	    dy = fm.getHeight();
	    dyy = dy + state.s2;
            
            int fonto = state.font12;
	
            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,fonto,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,fonto,x,y);
	    }
	    else{
		stmp ="";
		MaestroG.subscripter("TE",""+state.mode_number1+","+state.mode_number2,stmp,g,fonto,x,y);
		MaestroG.subscripter("TM",""+state.mode_number1+","+state.mode_number2,stmp,g,fonto,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;
                MaestroG.subscripterSer5("cutoff frequency        ","f","c"," =  "+MaestroA.rounder(f_normalized,6)+" [ Hz ]",g,fonto,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;
                MaestroG.subscripterSer5("cutoff frequency        ","f","c"," =  "+MaestroA.rounder(f_normalized,6)+" [ kHz ]",g,fonto,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;
                MaestroG.subscripterSer5("cutoff frequency        ","f","c"," =  "+MaestroA.rounder(f_normalized,6)+" [ MHz ]",g,fonto,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;
                MaestroG.subscripterSer5("cutoff frequency        ","f","c"," =  "+MaestroA.rounder(f_normalized,6)+" [ GHz ]",g,fonto,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;
                MaestroG.subscripterSer5("cutoff frequency        ","f","c"," =  "+MaestroA.rounder(f_normalized,6)+" [ THz ]",g,fonto,x,y);	
            }
            else{
                f_normalized = state.cut_off_frequency_for_mode/1.0E12;
                MaestroG.subscripterSer5("cutoff frequency        ","f","c"," =  "+MaestroA.rounder(f_normalized,6)+" [ THz ]",g,fonto,x,y);	
            }

            y += dyy;

            stmp = " = "+MaestroA.rounder(state.cut_off_wavelength,7)+" [ m ]";
            MaestroG.subscripterSer4("cutoff wavelength      ","\u03bb","c",stmp,g,fonto,x,y);
            
            y += dyy + state.s2;
            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,fonto,x,y);
                    MaestroG.superscripter("      [ 10","8"," m/s ]",g,fonto,x+2*dx+state.s20,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,fonto,x,y);
                    MaestroG.superscripter("      [ 10","11"," m/s ]",g,fonto,x+2*dx+state.s20,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,fonto,x,y);
                    MaestroG.superscripter("      [ 10","14"," m/s ]",g,fonto,x+2*dx+state.s20,y);
                }
                else if(v_normalized >= 1.0E9){
                    stmp="  = "+MaestroA.rounder(v_normalized/1.0E9,3);
                    MaestroG.subscripterSer6("phase velocity  ","u","pz",stmp,g,fonto,x,y);
                    MaestroG.superscripter("      [ 10","17"," m/s ]",g,fonto,x+2*dx+state.s20,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,fonto,x,y);
                    MaestroG.superscripter("      [ 10","8"," m/s ]",g,fonto,x+2*dx+state.s20,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,fonto,x,y);
                    MaestroG.superscripter("      [ 10","5"," m/s ]",g,fonto,x+2*dx+state.s20,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,fonto,x,y);
                    MaestroG.superscripter("      [ 10","2"," m/s ]",g,fonto,x+2*dx+state.s20,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,fonto,x,y);
                    MaestroG.superscripter("      [ 10","-1"," m/s ]",g,fonto,x+2*dx+state.s20,y);
                }
                else if(v_normalized < 1.0E-9){
                    stmp="  = "+MaestroA.rounder(v_normalized*1.0E12,3);
                    MaestroG.subscripterSer6("group velocity  ","u","gz",stmp,g,fonto,x,y);
                    MaestroG.superscripter("      [ 10","-4"," m/s ]",g,fonto,x+2*dx+state.s20,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,fonto,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,fonto,x,y);
                    MaestroG.superscripter("      [ 10","3"," m ]",g,fonto,x+2*dx+state.s20,y);
                }
                else if(test >= 1.0E6 && test < 1.0E9 ){
                    stmp = " = "+MaestroA.rounder(test/1.0E6,3);
                    MaestroG.subscripterSer4B("guide wavelength   ","\u03bb","g",stmp,g,fonto,x,y);
                    MaestroG.superscripter("      [ 10","6"," m ]",g,fonto,x+2*dx+state.s20,y);
                }
                else if(test >= 1.0E9 ){
                    stmp = " = "+MaestroA.rounder(test/1.0E9,3);
                    MaestroG.subscripterSer4B("guide wavelength   ","\u03bb","g",stmp,g,fonto,x,y);
                    MaestroG.superscripter("      [ 10","9"," m ]",g,fonto,x+2*dx+state.s20,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,fonto,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,fonto,x,y);
                    MaestroG.superscripter("      [ 10","3"," \u03a9 ]",g,fonto,x+2*dx+state.s20,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,fonto,x,y);
                    MaestroG.superscripter("      [ 10","6"," \u03a9 ]",g,fonto,x+2*dx+state.s20,y);
                }
                else if(test >= 1.0E9 ){
                    stmp = " = "+MaestroA.rounder(state.impedance_TE/1.0E9,3);
                    MaestroG.subscripter("guide impedance   \u03b7","TE",stmp,g,fonto,x,y);
                    MaestroG.superscripter("      [ 10","9"," \u03a9 ]",g,fonto,x+2*dx+state.s20,y);
                }
                
	      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,fonto,x,y);
	      }
	            

	    }
	    else if (state.frequency < state.cut_off_frequency_for_mode){
		
		stmp=" = undefined";
	        MaestroG.subscripterSer6("phase velocity  ","u","pz",stmp,g,fonto,x,y);
	        
	        y += dyy;
	        
	        stmp="  = undefined";
	        MaestroG.subscripterSer6("group velocity  ","u","gz",stmp,g,fonto,x,y);
	       
	        y += dyy;
	        stmp = " = undefined";
	        MaestroG.subscripter("guide wavelength   \u03bb","g",stmp,g,fonto,x,y);
	    }
	    else if (state.frequency == state.cut_off_frequency_for_mode){
		
		stmp=" = \u221e";
	        MaestroG.subscripterSer6("phase velocity  ","u","pz",stmp,g,fonto,x,y);
		
	        y += dyy;
	        
	        stmp="  = 0.0";
	        MaestroG.subscripterSer6("group velocity  ","u","gz",stmp,g,fonto,x,y);
	       
	        y += dyy;
	        stmp = " = \u221e";
	        MaestroG.subscripter("guide wavelength   \u03bb","g",stmp,g,fonto,x,y);
	    }
		y += dyy+state.s2;
		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,fonto,x,y);
		    
                    stmp = " ]";
		    MaestroG.superscripter(" [ m","-1",stmp,g,fonto,x+2*dx+state.s20,y);
		    
	        }
		else if(state.cut_off_frequency_for_mode==state.frequency){
		    stmp=" = 0.0  -  Cut-off Condition";
		    MaestroG.subscripterSer("   k","z",stmp,g,fonto,x,y);
		    
	        }
		else if(state.cut_off_frequency_for_mode > state.frequency){
		    
		    g.setColor(Color.red);
		    stmp="EVANESCENT MODE";
		    MaestroG.subscripterB("","",stmp,g,fonto,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,fonto,x,y);
		    
		    stmp = " ]";
		    MaestroG.superscripter(" [ m","-1",stmp,g,fonto,x+2*dx+state.s20,y);
	        }
							
		y += dyy;
	        
	        stmp="  = "+MaestroA.rounder(state.mode_number1*Math.PI/state.a,5);
	        MaestroG.subscripterSer("   k","x",stmp,g,fonto,x,y);
		stmp = " ]";
	        MaestroG.superscripter(" [ m","-1",stmp,g,fonto,x+2*dx+state.s20,y);
		y += dyy;
	        
	        stmp="  = "+MaestroA.rounder(state.mode_number2*Math.PI/state.b,5);
	        MaestroG.subscripterSer("   k","y",stmp,g,fonto,x,y);
                stmp = " ]";
	        MaestroG.superscripter(" [ m","-1",stmp,g,fonto,x+2*dx+state.s20,y);
		
	    }
            
            y+=dyy;
            g.setColor(Color.black);
	    g.drawLine(state.s5,y+dyy/4,getSize().width-state.s15,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);
            }
	        
	    }
	
}
