// 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.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.font.*;
import java.net.URL;
import java.text.*;
import java.util.*;
import java.util.Map;
import java.util.Hashtable;
import java.util.jar.Attributes;
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,245);
    //private static final Color bgcolor = Color.white;
    
    public TextField epsilon;
    private Paint paint;
    
    private Image im;
    private Graphics buf;
    private BufferedImage figure;
  
    public Button bupdate;

    StateVars state;
  
    public Instructions (StateVars state){
	super();
        this.state = state;
	setLayout(null);
	setBackground(bgcolor);
	// update button
	bupdate = new Button("CLOSE");
        bupdate.setBackground(new Color(240,240,255));
	add(bupdate);
        //int buttonx = 400;
        //int buttonx = 160;
        //int buttony = 300;
        int buttonx = 490;
        int buttony = 390;
        int buttonwide = 70;
        int buttonheight = 27;
        
        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);
		
    }
    
    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();

        boolean saveOldFlag = MaestroG.useBiggerFont;
        MaestroG.useBiggerFont = false;
        
        g.setColor(Color.black);
        g.setFont(state.ttfFont.deriveFont(Font.BOLD,16f));


        try {
            figure = ImageIO.read(getClass().getResource("figureb.gif"));
        } catch (IOException e) {
            e.printStackTrace();
        }



        g.drawString("Instructions", 20,25);
        g.setColor(Color.red);
        g.setFont(state.ttfFont.deriveFont(Font.BOLD,15f));
        g.drawString("Input", 25,60);
        //g.drawString("Design Procedure", 325,50);
        g.setColor(Color.black);
        g.setFont(state.ttfFont.deriveFont(14f));
        int deltaY = 20;
        int starty = 30;
        //INPUT
        FontMetrics fmNormal, fmSmall;
        g.setFont(state.ttfFont.deriveFont(12f));
        fmSmall = g.getFontMetrics();
        g.setFont(state.ttfFont.deriveFont(14f));
        fmNormal = g.getFontMetrics();
        int lineLength = fmNormal.stringWidth("Main Line Parameters (Z and ");
        lineLength += fmSmall.stringWidth("01");
        MaestroG.subscripter("Main Line Parameters (Z","01","and",g,14,25,90);
        MaestroG.subscripter("\u03b5","r1",")",g,14,25+lineLength,90);

        //dblsubscriptor("Line Parameters (Z","01","and \u03b5","r1",")",
        //               g,25,80);
        MaestroG.subscripter("Load Impedance Z","L","",g,14,25,90+deltaY);
        //DESIGN PROCEDURE
        g.drawString("The goal is to select:", 391,starty);
        g.drawString("    (a) the distance d from the load at which to insert",
                     391,starty+2*deltaY);
        g.drawString("        the \u03bb/4 transformer.  This is done such that",
                     391,starty+3*deltaY);
        g.drawString("        at d, Z(d) is purely real, which corresponds to ",
                     391,starty+4*deltaY);
        g.drawString("        the locations of voltage maxima and minima: ",
                     391,starty+5*deltaY);
        lineLength = fmNormal.stringWidth("        d = dmax and ");
        lineLength += fmSmall.stringWidth("1");
        MaestroG.subscripter("        d","1"," = dmax and ",
                             g,14,391,starty+6*deltaY);
        MaestroG.subscripter("d","2"," = dmin (2 solutions)",
                             g,14,391+lineLength,starty+6*deltaY);

        lineLength = fmNormal.stringWidth("    (b) If d = d");
        lineLength += fmSmall.stringWidth("1");
        MaestroG.subscripter("    (b) If d = d","1","",
                             g,14,391,starty+8*deltaY);
        //g.setFont(new Font("Symbol",Font.PLAIN,16));
        //FontMetrics fmSymbol = g.getFontMetrics();
        //g.drawString(" \u21d2 ",325+lineLength,80+8*deltaY);
        //lineLength += fmSymbol.stringWidth(" \u21d2 ");

        g.drawString(" then ",391+lineLength,starty+8*deltaY);
        lineLength += fmNormal.stringWidth(" then ");

        g.setFont(state.ttfFont.deriveFont(14f));
        MaestroG.subscripter("Z","02"," = ",
                             g,14,391+lineLength,starty+8*deltaY);
        lineLength += fmNormal.stringWidth("Z = ");
        lineLength += fmSmall.stringWidth("02");
        MaestroG.subscripter("\u221AZ","01","Z(",
                             g,14,391+lineLength,starty+8*deltaY);
        int xstart = 391+lineLength+fmNormal.stringWidth("\u221A");

        lineLength += fmNormal.stringWidth("\u221AZZ(");
        lineLength += fmSmall.stringWidth("01");
        MaestroG.subscripter("d","1",")",
                             g,14,391+lineLength,starty+8*deltaY);

        int xend = xstart + fmNormal.stringWidth("ZZ(d)")+
            fmSmall.stringWidth("011");
        //dblsubscriptor("    (b) If d = dmax, Z","02"," = \u221AZ","01",
        //               "Z(dmax)",g,325,80+6*deltaY);
        g.drawLine(xstart-2,starty+8*deltaY - 14,xend,starty+8*deltaY - 14);

        lineLength = fmNormal.stringWidth("    (c) If d = d");
        lineLength += fmSmall.stringWidth("2");
        MaestroG.subscripter("    (c) If d = d","2","",
                             g,14,391,starty+10*deltaY);
        //g.setFont(new Font("Symbol",Font.PLAIN,16));
        //g.drawString(" \u21d2 ",325+lineLength,80+10*deltaY);
        //lineLength += fmSymbol.stringWidth(" \u21d2 ");

        g.drawString(" then ",391+lineLength,starty+10*deltaY);
        lineLength += fmNormal.stringWidth(" then ");

        g.setFont(state.ttfFont.deriveFont(14f));
        MaestroG.subscripter("Z","02"," = ",
                             g,14,391+lineLength,starty+10*deltaY);
        lineLength += fmNormal.stringWidth("Z = ");
        lineLength += fmSmall.stringWidth("02");
        MaestroG.subscripter("\u221AZ","01","Z(",
                             g,14,391+lineLength,starty+10*deltaY);
        xstart = 391+lineLength+fmNormal.stringWidth("\u221A");

        lineLength += fmNormal.stringWidth("\u221AZZ(");
        lineLength += fmSmall.stringWidth("01");
        MaestroG.subscripter("d","2",")",
                             g,14,391+lineLength,starty+10*deltaY);

        xend = xstart + fmNormal.stringWidth("ZZ(d)")+
            fmSmall.stringWidth("012");
        //dblsubscriptor("    (c) If d = dmin, Z","02"," = \u221AZ","01",
        //               "Z(dmin)",g,325,80+10*deltaY);
        g.drawLine(xstart-2,starty+10*deltaY - 14,xend,starty+10*deltaY - 14);

        /*
        lineLength = fmNormal.stringWidth("    (c) If d = dmin, Z = ");
        lineLength += fmSmall.stringWidth("02");

        MaestroG.subscripter("    (c) If d = dmin, Z","02"," =",
                             g,14,325,80+10*deltaY);
        MaestroG.subscripter("\u221AZ","01","Z(dmin)",
                             g,14,325+lineLength,80+8*deltaY);

        //dblsubscriptor("    (c) If d = dmin, Z","02"," = \u221AZ","01",
        //               "Z(dmin)",g,325,80+7*deltaY);
        xstart = 325+lineLength+fmNormal.stringWidth("\u221A");
        xend = xstart;
        xend += fmNormal.stringWidth("ZZ(dmin)");
        xend += fmSmall.stringWidth("01");
        //int sqrtHeight = fmNormal.stringHeight("\u221A");
        //System.out.println("sqrtHeight = "+String.valueOf(sqrtHeight));
        g.drawLine(xstart-2,80+8*deltaY - 14,xend,80+8*deltaY - 14);
        */

        g.drawImage(figure,20,200,this);
        drawCopyRight(g);

        MaestroG.useBiggerFont = saveOldFlag;

    }


    public void drawCopyRight(Graphics g) {
        int deltaY = 20;
        int startingY = getSize().height - 70;
        int startingX = 0;
        FontMetrics fm;
        g.setColor(Color.black);
        g.setColor(Color.black);
        //g.setFont(normalfont14);
        g.setFont(state.ttfFont.deriveFont(14f));
        fm = g.getFontMetrics();
        g.drawString("Applet Design: Umberto Ravaioli",
                     startingX+15,startingY+deltaY);
        
        MaestroG.superscripter("Interactive Java","TM"," platform:  www.amanogawa.com",
                               g,14,startingX+15,startingY+2*deltaY);
        int lineLength = fm.stringWidth("Interactive JavaTM platform:  www.amanogawa.com");
        lineLength += 10;

        g.setFont(state.ttfFont.deriveFont(14f));
        g.drawString("All Rights Reserved",startingX+15,startingY+3*deltaY);
        g.drawLine(startingX,startingY,
                   startingX+lineLength+25,startingY);
        g.drawLine(startingX+lineLength+25,startingY,
                   startingX+lineLength+25,startingY+4*deltaY);
    }


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

