// 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);
    //private static final Color bgcolor = Color.white;
    
    public TextField epsilon;
    //private Paint paint;
    
    private Image im;
    private Graphics buf;

    Trans_State state;
    public Button bupdate;
    
    private BufferedImage bullet;

    public Instructions(Trans_State state){
	super();
        this.state = state;
	setLayout(null);
	setBackground(bgcolor);

	getImages();
	
	// update button
	bupdate = new Button("CLOSE");
        bupdate.setBackground(new Color(240,240,255));
        bupdate.setFont(state.ttfFont.deriveFont((float)(state.s12)));
	add(bupdate);
        int buttonx = state.s430;
        int buttony = state.s550;
        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() {
        // read in the bullet image, because CHEERPJ does not render this
	//  symbol in any font!  (java to javascript converter)
        try {
            //bullet = ImageIO.read(getClass().getResource("bullet.png"));
	    BufferedImage img =
		ImageIO.read(getClass().getResource("bullet.png"));
	    int targetWidth = (int) (state.sfactor * img.getWidth());
	    int targetHeight = (int) (state.sfactor * img.getHeight());
	    bullet =
		new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
	    Graphics2D graphics2D = bullet.createGraphics();
	    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
					RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	    graphics2D.drawImage(img, 0, 0, targetWidth, targetHeight, 0, 0,
				 img.getWidth(), img.getHeight(), null);
	    graphics2D.dispose();

        } 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);

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

        g.drawString("Instructions", state.s15,state.s15);
        g.setColor(Color.red);
        g.setFont(state.ttfFont.deriveFont(Font.BOLD,(float)(state.s15)));
        g.drawString("Input", state.s25,state.s90);
        g.setColor(Color.black);
        g.setFont(state.ttfFont.deriveFont((float)(state.s14)));
        int deltaY = state.s22;
        //INPUT
        //g.drawString("\u2022 Characteristic impedance", state.s25,state.s120);
	g.drawImage(bullet,state.s25,state.s120-bullet.getHeight(), this);
	g.drawString(" Characteristic impedance",state.s25+bullet.getWidth(),state.s120);
	
        FontMetrics fmNormal, fmSmall;
        g.setFont(state.ttfFont.deriveFont((float)(state.s12)));
        fmSmall = g.getFontMetrics();
        g.setFont(state.ttfFont.deriveFont((float)(state.s14)));
        fmNormal = g.getFontMetrics();
        int lineLength = fmNormal.stringWidth("\u2022 Specify Z, ");
        lineLength += fmSmall.stringWidth("L");
        //MaestroG.subscripter("\u2022 Specify Z","L",", ",
        //                     g,state.font14,state.s25,state.s120+deltaY);
	g.drawImage(bullet,state.s25,state.s120+deltaY-bullet.getHeight(), this);
        MaestroG.subscripter(" Specify Z","L",", ",
                             g,state.font14,state.s25+bullet.getWidth(),state.s120+deltaY);
	

	
        MaestroG.subscripter("Y","L",", \u0393, or use mouse to position load on Smith Chart",
                             g,state.font14,state.s25+lineLength,state.s120+deltaY);
        //g.drawString("   use mouse to position load on Smith Chart",25,125+3*deltaY);
        //g.drawString("\u2022 Use cursor to move towards the source", state.s25,state.s120+2*deltaY);
	g.drawImage(bullet,state.s25,state.s120+2*deltaY-bullet.getHeight(), this);
	g.drawString(" Use cursor to move towards the source", state.s25+bullet.getWidth(),state.s120+2*deltaY);
	
        //OUTPUT
        int newY = state.s140+4*deltaY;
        g.setColor(Color.red);
        g.setFont(state.ttfFont.deriveFont(Font.BOLD,(float)(state.s15)));
        g.drawString("Displayed Information", state.s25,newY);
        g.setColor(Color.black);
        g.setFont(state.ttfFont.deriveFont((float)(state.s14)));

        newY += state.s40; 
        deltaY = state.s25;
        //g.drawString("\u2022 SWR circle", state.s25,newY);
	g.drawImage(bullet,state.s25,newY-bullet.getHeight(), this);
	g.drawString(" SWR circle", state.s25+bullet.getWidth(),newY);
	
        lineLength = fmNormal.stringWidth("   r and ");
        lineLength += fmSmall.stringWidth("L");
        //dblsubscriptor("r","L"," and x","L"," circles",g,450,80+deltaY);
        //MaestroG.subscripter("\u2022 r","L"," and",g,state.font14,state.s25,newY+deltaY);
	g.drawImage(bullet,state.s25,newY+deltaY-bullet.getHeight(), this);
	MaestroG.subscripter(" r","L"," and",g,state.font14,state.s25+bullet.getWidth(),newY+deltaY);
	
        MaestroG.subscripter(" x","L"," circles",g,state.font14,state.s25+lineLength,newY+deltaY);
        //g.drawString("\u2022 \u0393 = Load Reflection Coefficient",state.s25,newY+2*deltaY);
	g.drawImage(bullet,state.s25,newY+2*deltaY-bullet.getHeight(), this);
	g.drawString(" \u0393 = Load Reflection Coefficient",state.s25+bullet.getWidth(),newY+2*deltaY);
	
        lineLength = fmNormal.stringWidth("\u2022 \u0393 = \u0393");
        lineLength += fmSmall.stringWidth("d   ");
        //MaestroG.subscripter("\u2022 \u0393","d"," = \u0393",g,state.font14,state.s25,newY+3*deltaY);
	g.drawImage(bullet,state.s25,newY+3*deltaY-bullet.getHeight(), this);
	MaestroG.subscripter(" \u0393","d"," = \u0393",g,state.font14,state.s25+bullet.getWidth(),newY+3*deltaY);
	
        MaestroG.superscripter("e","-j2\u03b2d",", phase-shifted Reflection "+
                               "Coefficient",g,state.font14,state.s25+lineLength,newY+3*deltaY);
        //g.drawString("\u2022 Z(d) = Line Impedance",state.s25,newY+4*deltaY);
	g.drawImage(bullet,state.s25,newY+4*deltaY-bullet.getHeight(), this);
	g.drawString(" Z(d) = Line Impedance",state.s25+bullet.getWidth(),newY+4*deltaY);
	
        //g.drawString("\u2022 y(d) = Line Admittance",state.s25,newY+5*deltaY);
	g.drawImage(bullet,state.s25,newY+5*deltaY-bullet.getHeight(), this);
	g.drawString(" y(d) = Line Admittance",state.s25+bullet.getWidth(),newY+5*deltaY);
	
        //g.drawString("\u2022 S = Voltage-Standing-Wave-Ratio",state.s25,newY+6*deltaY);
	g.drawImage(bullet,state.s25,newY+6*deltaY-bullet.getHeight(), this);
	g.drawString(" S = Voltage-Standing-Wave-Ratio",state.s25+bullet.getWidth(),newY+6*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.s14)));
        g.drawString("All Rights Reserved",startingX+state.s15,startingY+3*deltaY);
        g.drawLine(startingX,startingY,
                   startingX+lineLength+25,startingY);
        g.drawLine(startingX+lineLength+25,startingY,
                   startingX+lineLength+25,startingY+4*deltaY);
    }


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

