// About.java
/* A Java class for
 * LineImpedance.java
 * Electromagnetic Transmission Line Applet
 * Applet without Smith Chart - Prepared by Umberto Ravaioli 
 * for 6th edition of Fundamentals of Applied Electromagnetics Book
 * May 2009 - All Rights Reserved
 */   

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Instructions extends Panel {
    
    private static final Color bgcolor = new Color(255,255,250);
    
    public TextField epsilon;
    private Paint paint;
    
    private Image im;
    private Graphics buf;
    
    public Button bupdate;

    NewGuide_State state;
    
    private BufferedImage square_root;
  
    public Instructions(NewGuide_State state){
	super();
        this.state = state;
	
	getImages();
	setLayout(null);
	setBackground(bgcolor);
        
        // update button
	bupdate = new Button("CLOSE");
        bupdate.setBackground(new Color(240,240,255));
        bupdate.setFont(state.ttfFont.deriveFont(Font.PLAIN,(float)state.font12));
	add(bupdate);
        
        int buttonx = state.s450;
        int buttony = state.s230;
        int buttonwide = state.s70;
        int buttonheight = state.s27;
	 
        bupdate.setBounds(buttonx,buttony,buttonwide,buttonheight);	
        Panel ps7 = new Panel();
	    ps7.setBackground(Color.lightGray);
	    add(ps7);
	    ps7.setBounds(buttonx-1,buttony-1,buttonwide+2,buttonheight+2);
	    
	Panel ps8 = new Panel();
	    ps8.setBackground(Color.black);
	    add(ps8);
	    ps8.setBounds(buttonx-2,buttony-2,buttonwide+4,buttonheight+4);
    }


    private void getImages() {

	System.out.printf("state.sfactor is %f!%n", state.sfactor);
	
        // read in character images that CHEERPJ will not render
	//   (java to javascript converter)
	double font14_factor = 14.0 * state.sfactor;
	//System.out.printf("font factor: %f%n",font14_factor);
	long fontsize = Math.round(font14_factor);
	if (fontsize > 30) fontsize = 30;
	else if (fontsize < 14) fontsize = 14;
	//System.out.printf("effective fontsize: %d%n",fontsize);
	String filename = "square_root_"+String.valueOf(fontsize)+".png";
	//System.out.printf("filnam: %s%n",filename);
        try {
		square_root = ImageIO.read(getClass().getResource(filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
	
    }
    
    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);
    }
	
	//Addition to reduce flicker new routine
    public void update(Graphics g){		// added to avoid clearing
            paint(g);
    }
    
    public void clear(){
	    this.getGraphics().clearRect(0,0,getSize().width,getSize().height);
	    repaint();
    }
    
    public void drawCanvas(Graphics g){
        FontMetrics fm;
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);        
        g.clearRect(0,0,getSize().width-1,getSize().height-1);

        //g.setFont(font_one);
        //fm = g.getFontMetrics();
        
        g.setColor(Color.black);
        g.drawRect(0,0,getSize().width-1,getSize().height-1);
        
        boolean saveOldFlag = MaestroG.useBiggerFont;
        MaestroG.useBiggerFont = false;
        
        g.setColor(Color.black);
        g.setFont(state.ttfFont.deriveFont(Font.BOLD,(float)state.font16));

        g.drawString("Instructions", state.s20,state.s25);
        g.setColor(Color.red);
        g.setFont(state.ttfFont.deriveFont(Font.BOLD,(float)state.font15));
        g.drawString("Input", state.s25,state.s50);
        g.drawString("Displayed Information", state.s325,state.s50);
        g.setColor(Color.black);
        g.setFont(state.ttfFont.deriveFont((float)state.font14));
        int deltaY = state.s20;
        //INPUT
        g.drawString("d = wire diameter", state.s25,state.s80);
        g.drawString("D = distance between wires' centers", state.s25,state.s80+deltaY);
        MaestroG.subscripter("\u03b5","r"," = relative permittivity of substrate",
                             g,state.font14,state.s25,state.s80+2*deltaY);
        g.drawString("\u03c3 = conductivity of substrate",state.s25,state.s80+3*deltaY);
        MaestroG.subscripter("\u03c3","c"," = conductivity of wire material",
                             g,state.font14,state.s25,state.s80+4*deltaY);
        MaestroG.subscripter("\u03bc = \u03bc","0"," for both substrate and wires",
                             g,state.font14,state.s25,state.s80+5*deltaY);
        //OUTPUT
        g.drawString("Line Parameters C\u00b4, L\u00b4, R\u00b4, G\u00b4", state.s325,state.s80);
        MaestroG.subscripter("Z","0"," = characteristic impedance",
                             g,state.font14,state.s325,state.s80+deltaY);
        g.drawString("\u03b1 = attenuation constant",state.s325,state.s80+2*deltaY);
        g.drawString("\u03b2 = wave number",state.s325,state.s80+3*deltaY);
        // \u221A is the square root symbol

        FontMetrics fmNormal,fmSub;
        g.setFont(state.ttfFont.deriveFont((float)state.font14));
        fmNormal = g.getFontMetrics();
        g.setFont(state.ttfFont.deriveFont((float)state.font12));
        fmSub = g.getFontMetrics();
        int len1,len2;
        //len1 = fmNormal.stringWidth("\u03bb = \u03bb" + "/\u221A");
	// CHEERPJ doesn't print \u221A square root symbol, 10 is width
	//  of new square_root image:
	len1 = fmNormal.stringWidth("\u03bb = \u03bb" + " / ") + square_root.getWidth();
        len1 += fmSub.stringWidth("0");
        len2 = fmNormal.stringWidth("\u03b5");
        len2 += fmSub.stringWidth("r");

        //dblsubscriptor("\u03bb = \u03bb","0","/\u221A\u03b5","r","",
        //               g,325,80+4*deltaY);
        //MaestroG.subscripter("\u03bb = \u03bb","0","/\u221A",
        //                     g,state.font14,state.s325,state.s80+4*deltaY);
	MaestroG.SubTxtTxtImg("\u03bb = \u03bb","0"," / ",square_root,
			      g,state.font14,state.s325,state.s80+4*deltaY,this);
			      
        MaestroG.subscripter("\u03b5","r","",
                             g,state.font14,state.s327+len1,state.s80+4*deltaY);
        g.drawLine(state.s325+len1,state.s66+4*deltaY, state.s325+len1+len2,state.s66+4*deltaY);
             
        drawCopyRight(g);

        MaestroG.useBiggerFont = saveOldFlag;

    }


    public void drawCopyRight(Graphics g) {
        int deltaY = state.s20;
        int startingY = getSize().height - state.s70;
        int startingX = 0;
        FontMetrics fm;
        g.setColor(Color.black);
        g.setColor(Color.black);
        //g.setFont(normalfont14);
        g.setFont(state.ttfFont.deriveFont((float)state.font14));
        fm = g.getFontMetrics();
        g.drawString("Application Design: Umberto Ravaioli",
                     startingX+state.s15,startingY+deltaY);
        
        MaestroG.superscripter("Interactive Java","TM"," platform:  www.amanogawa.com",
                               g,state.font14,startingX+state.s15,startingY+2*deltaY);
        int lineLength = fm.stringWidth("Interactive JavaTM platform:  www.amanogawa.com");
        lineLength += state.s10;

        g.setFont(state.ttfFont.deriveFont((float)state.font14));
        g.drawString("All Rights Reserved",startingX+state.s15,startingY+3*deltaY);
        g.drawLine(startingX,startingY,
                   startingX+lineLength+state.s25,startingY);
        g.drawLine(startingX+lineLength+state.s25,startingY,
                   startingX+lineLength+state.s25,startingY+4*deltaY);
    }

//----------------------------------------------------------------------------------------    
}//End
    

