// 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("figuree.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", 391,20);
        g.setColor(Color.black);
        g.setFont(state.ttfFont.deriveFont(14f));
        int deltaY = 20;
        //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("Line Parameters (Z and ");
        lineLength += fmSmall.stringWidth("01");
        MaestroG.subscripter("Line Parameters (Z","0"," and",
                             g,14,25,90);
        MaestroG.subscripter("\u03b5","r",")",
                             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,100+deltaY);
        //DESIGN PROCEDURE
        int ystart = 20;
        deltaY = 22;
        g.drawString("The goal is to match the feedline to the load by", 391,ystart);
        g.drawString("connecting a separate, short-circuited line to the",
                     391,ystart+deltaY);
        g.drawString("original line at a distance d from the load.",
                     391,ystart+2*deltaY);
        g.drawString("Two solutions exist:", 391,ystart+4*deltaY);
        lineLength = fmNormal.stringWidth("    (a) Select d such that y (");
        lineLength += fmSmall.stringWidth("1");
        MaestroG.subscripter("    (a) Select d","1"," such that y (",
                             g,14,391,ystart+6*deltaY);
        MaestroG.subscripter(" d","1",") = 1+ jx,",
                             g,14,391+lineLength,ystart+6*deltaY);

        //dblsubscriptor("    (a) Select d","1"," such that y(d",
        //               "1",") = 1+ jx,", g,391,ystart+5*deltaY);
        lineLength = fmNormal.stringWidth("        and then select ");
        g.drawString("        and then select ",391,ystart+7*deltaY);
        MaestroG.subscripterIT("l","1"," such that ",
                               g,14,391+lineLength,ystart+7*deltaY);
        g.setFont(state.italicFont.deriveFont(14f));
        FontMetrics fmItalic = g.getFontMetrics();
        lineLength += fmItalic.stringWidth("l");
        lineLength += fmSmall.stringWidth("1");
        lineLength += fmNormal.stringWidth(" such that ");
        g.setFont(state.ttfFont.deriveFont(14f));
        MaestroG.subscripter("y","s","(",
                             g,14,396+lineLength,ystart+7*deltaY);
        lineLength += fmNormal.stringWidth("y(");
        lineLength += fmSmall.stringWidth("s");
        MaestroG.subscripterIT(" l","1",") = -jx.",
                               g,14,396+lineLength,ystart+7*deltaY);

        //tplsubscriptor("        and then select Z","1"," such that y","s",
        //                "(Z","1",") = -jx.", g,351,ystart+7*deltaY);

        lineLength = fmNormal.stringWidth("    (b) Select d such that y (");
        lineLength += fmSmall.stringWidth("2");
        MaestroG.subscripter("    (b) Select d","2"," such that y (",
                             g,14,391,ystart+9*deltaY);
        MaestroG.subscripter(" d","2",") = 1 - jx,",
                             g,14,391+lineLength,ystart+9*deltaY);

        //dblsubscriptor("    (b) Select d","2"," such that y(d","2",
        //               ") = 1 - jx,",g,391,ystart+8*deltaY);


        lineLength = fmNormal.stringWidth("        and then select ");
        g.drawString("        and then select ",391,ystart+10*deltaY);
        MaestroG.subscripterIT("l","2"," such that ",
                               g,14,391+lineLength,ystart+10*deltaY);
        lineLength += fmItalic.stringWidth("l");
        lineLength += fmSmall.stringWidth("2");
        lineLength += fmNormal.stringWidth(" such that ");
        MaestroG.subscripter("y","s","(",
                             g,14,396+lineLength,ystart+10*deltaY);
        lineLength += fmNormal.stringWidth("y(");
        lineLength += fmSmall.stringWidth("s");
        MaestroG.subscripterIT(" l","2",") = jx.",
                               g,14,396+lineLength,ystart+10*deltaY);


        /*
        lineLength = fmNormal.stringWidth("        and then select Z such that");
        lineLength += fmSmall.stringWidth("2");
        MaestroG.subscripter("        and then select Z","2"," such that",
                             g,14,391,80+9*deltaY);
        MaestroG.subscripter(" y","s","(", 
                             g,14,391+lineLength,80+9*deltaY);
        lineLength += fmNormal.stringWidth(" y(");
        lineLength += fmSmall.stringWidth("s");
        MaestroG.subscripter("Z","2",") = jx.", 
                             g,14,391+lineLength,80+9*deltaY);
        */
        //tplsubscriptor("         and then select Z","2"," such that y","s",
        //               "(Z","2",") = jx.",g,351,80+9*deltaY);


        g.drawString("The final output is a plot of | \u0393 |, as seen by",
                     391,ystart+12*deltaY);
        g.drawString("the feedline, as a function of frequency.",
                     391,ystart+13*deltaY);

        //g.drawImage(figure,10,250,this);
        g.drawImage(figure,20,175,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
    

