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

    //Mod1State state;
    public Button bupdate;
    PlaneWave_State state;
    
    private BufferedImage bullet;
    
    public Instructions(PlaneWave_State state){
	super();
        this.state = state;
        
        setLayout(null);

	getImages();
	setBackground(bgcolor);
	bupdate = new Button("CLOSE");
        bupdate.setBackground(new Color(240,240,255));
        bupdate.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
	add(bupdate);
        int buttonx = state.s550;
        int buttony = state.s400+state.s40;
        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 nabla images, 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.drawImage(img, 0, 0, targetWidth, targetHeight, 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);

        //g.setFont(font_one);
        //fm = g.getFontMetrics();

        //boolean saveOldFlag = MaestroG.useBiggerFont;
        //MaestroG.useBiggerFont = false;
        
        g.setColor(Color.black);
        g.drawRect(0,0,getSize().width-1,getSize().height-1);
        g.setFont(new Font("SanSerif",Font.BOLD,state.font16));
        
        g.drawString("Instructions", state.s15,state.s25);
        
        //g.setFont(state.ttfFont.deriveFont(14f));
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        g.drawString("This module can accommodate linearly polarized plane "+
                    "waves in both lossy and lossless media.",state.s15,state.s55);
        
        g.setColor(Color.red);
        g.setFont(new Font("SanSerif",Font.BOLD,state.font15));

        g.drawString("Input Parameters", state.s15,state.s95);
        g.setColor(Color.black);
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        
        int deltaY = state.s20;
        
        //INPUT
        //String s = "\u2022 Constitutive parameters of the medium ("+
        //    "\u03b5"+STR.SUB+"r"+STR.ENDSUB+", \u03bc"+STR.SUB+"r"+STR.ENDSUB+
        //    ", \u03c3)";
        //int nextX = STR.displayString(s,g,state.s15,state.s115);
	g.drawImage(bullet,state.s15,state.s115-bullet.getHeight(),this);
        String s = " Constitutive parameters of the medium ("+
            "\u03b5"+STR.SUB+"r"+STR.ENDSUB+", \u03bc"+STR.SUB+"r"+STR.ENDSUB+
            ", \u03c3)";
        int nextX = STR.displayString(s,g,state.s15+bullet.getWidth(),state.s115);
	
        //s = "\u2022 "+STR.BOLD+"E"+STR.ENDBOLD+"-field amplitude, phase, "+
        //    "and frequency";
        //nextX = STR.displayString(s,g,state.s15,state.s115+deltaY);
	g.drawImage(bullet,state.s15,state.s115+deltaY-bullet.getHeight(),this);
        s = " "+STR.BOLD+"E"+STR.ENDBOLD+"-field amplitude, phase, "+
            "and frequency";
        nextX = STR.displayString(s,g,state.s15+bullet.getWidth(),state.s115+deltaY);
	
        //s = "\u2022 Spacing between two selectable windows A and B (at "+
        //    "z"+STR.SUB+"A"+STR.ENDSUB+" and z"+STR.SUB+"B"+STR.ENDSUB+")";
        //nextX = STR.displayString(s,g,state.s15,state.s115+2*deltaY);
	g.drawImage(bullet,state.s15,state.s115+2*deltaY-bullet.getHeight(),this);
        s = " Spacing between two selectable windows A and B (at "+
            "z"+STR.SUB+"A"+STR.ENDSUB+" and z"+STR.SUB+"B"+STR.ENDSUB+")";
        nextX = STR.displayString(s,g,state.s15+bullet.getWidth(),state.s115+2*deltaY);
	
	//OUTPUT
        g.setColor(Color.red);
        g.setFont(new Font("SanSerif",Font.BOLD,state.font15));
        int newY = state.s115+4*deltaY+state.s40;
        g.drawString("Displayed Information", state.s15,newY);
        g.setColor(Color.black);
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        newY += state.s20;
        //s = "\u2022 Spatial and temporal variations of "+STR.BOLD+"E"+
        //    STR.ENDBOLD+" and "+STR.BOLD+"H"+STR.ENDBOLD;
        //nextX = STR.displayString(s,g,state.s15,newY);
	g.drawImage(bullet,state.s15,newY-bullet.getHeight(),this);
        s = " Spatial and temporal variations of "+STR.BOLD+"E"+
            STR.ENDBOLD+" and "+STR.BOLD+"H"+STR.ENDBOLD;
        nextX = STR.displayString(s,g,state.s15+bullet.getWidth(),newY);
	
        //s = "\u2022 Phasor fields "+STR.BOLD+"E"+STR.ENDBOLD+" and "+
        //    STR.BOLD+"H"+STR.ENDBOLD+" across selected windows";
        //nextX = STR.displayString(s,g,state.s15,newY+deltaY);
	g.drawImage(bullet,state.s15,newY+deltaY-bullet.getHeight(),this);
        s = " Phasor fields "+STR.BOLD+"E"+STR.ENDBOLD+" and "+
            STR.BOLD+"H"+STR.ENDBOLD+" across selected windows";
        nextX = STR.displayString(s,g,state.s15+bullet.getWidth(),newY+deltaY);
	
        //g.drawString("\u2022 Wave properties",state.s15,newY+2*deltaY);
	g.drawImage(bullet,state.s15,newY+2*deltaY-bullet.getHeight(),this);
	g.drawString(" Wave properties",state.s15+bullet.getWidth(),newY+2*deltaY);
	
        //g.drawString("\u2022 Power flow, including skin depth",
        //             state.s15,newY+3*deltaY);
	g.drawImage(bullet,state.s15,newY+3*deltaY-bullet.getHeight(),this);
        g.drawString(" Power flow, including skin depth",
                     state.s15+bullet.getWidth(),newY+3*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(new Font("SanSerif",Font.PLAIN,state.font14));
        fm = g.getFontMetrics();
        g.drawString("Applet Design: Umberto Ravaioli",
                     startingX+state.s15,startingY+deltaY);
        
        String s = "Interactive Java"+STR.SUP+"TM"+STR.ENDSUP+
            " platform:  www.amanogawa.com";
        int nextX = STR.displayString(s,g,startingX+state.s15,startingY+2*deltaY);
        int lineLength = fm.stringWidth("Interactive JavaTM platform:  www.amanogawa.com");
        lineLength+= state.s10;

        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
    

