// 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 javax.imageio.ImageIO;
import java.io.IOException;

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 closeIt;
    PlaneWave_State state;

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

	getImages();
	setBackground(bgcolor);
	closeIt = new Button("CLOSE");
        closeIt.setBackground(new Color(240,240,255));
        closeIt.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
	add(closeIt);
        
        int buttonx = state.s400+state.s30;
        int buttony = state.s500+state.s50;
        int buttonwide = state.s70;
        int buttonheight = state.s27;
        
        closeIt.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);

        g.setColor(Color.black);
        //g.setFont(state.ttfFont.deriveFont(Font.BOLD,16f));
        g.setFont(new Font("SanSerif",Font.BOLD,state.font16));

        g.drawString("Instructions", state.s15,state.s25);
        g.setColor(Color.red);
        //g.setFont(state.ttfFont.deriveFont(Font.BOLD,15f));
        g.setFont(new Font("SanSerif",Font.BOLD,state.font15));

        g.drawString("Input", state.s15,state.s60);
        g.setColor(Color.black);
        //g.setFont(state.ttfFont.deriveFont(14f));
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        
        int deltaY = state.s20;
        
        //INPUT
        //String s = "\u2022 Select the initial location of the electron "+
        //    "(x"+STR.SUB+"0"+STR.ENDSUB+",y"+STR.SUB+"0"+STR.ENDSUB+")";
        //int nextX = STR.displayString(s,g, state.s15,state.s85);
	g.drawImage(bullet,state.s15,state.s85-bullet.getHeight(),this);
        String s = " Select the initial location of the electron "+
            "(x"+STR.SUB+"0"+STR.ENDSUB+",y"+STR.SUB+"0"+STR.ENDSUB+")";
        int nextX = STR.displayString(s,g, state.s15+bullet.getWidth(),state.s85);
	
        //s = "\u2022 Select the initial velocity of the electron "+STR.VBAR+
        //    "u"+STR.ENDVBAR+" = "+STR.HAT+"x"+STR.ENDHAT+"u"+STR.SUB+"x"+
        //    STR.ENDSUB+STR.SUBSUB+"0"+STR.ENDSUBSUB+"+"+STR.HAT+"y"+
        //    STR.ENDHAT+"u"+STR.SUB+"y"+STR.ENDSUB+STR.SUBSUB+"0"+STR.ENDSUBSUB;
        //nextX = STR.displayString(s,g, state.s15,state.s85+deltaY);
	g.drawImage(bullet,state.s15,state.s85+deltaY-bullet.getHeight(),this);
        s = " Select the initial velocity of the electron "+STR.VBAR+
            "u"+STR.ENDVBAR+" = "+STR.HAT+"x"+STR.ENDHAT+"u"+STR.SUB+"x"+
            STR.ENDSUB+STR.SUBSUB+"0"+STR.ENDSUBSUB+"+"+STR.HAT+"y"+
	    STR.ENDHAT+"u"+STR.SUB+"y"+STR.ENDSUB+STR.SUBSUB+"0"+STR.ENDSUBSUB;
        nextX = STR.displayString(s,g, state.s15+bullet.getWidth(),state.s85+deltaY);
	
        //s = "\u2022 Select the electric field "+STR.VBAR+"E"+STR.ENDVBAR+
        //    " = "+STR.HAT+"x"+STR.ENDHAT+"E"+STR.SUB+"x"+STR.ENDSUB+"+"+
        //    STR.HAT+"y"+STR.ENDHAT+"E"+STR.SUB+"y"+STR.ENDSUB;
        //nextX = STR.displayString(s,g, state.s15,state.s85+2*deltaY);
	g.drawImage(bullet,state.s15,state.s85+2*deltaY-bullet.getHeight(),this);
        s = " Select the electric field "+STR.VBAR+"E"+STR.ENDVBAR+
            " = "+STR.HAT+"x"+STR.ENDHAT+"E"+STR.SUB+"x"+STR.ENDSUB+"+"+
            STR.HAT+"y"+STR.ENDHAT+"E"+STR.SUB+"y"+STR.ENDSUB;
        nextX = STR.displayString(s,g, state.s15+bullet.getWidth(),state.s85+2*deltaY);
	
        //s = "\u2022 Select the magnetic field "+STR.VBAR+"H"+STR.ENDVBAR+
        //    " = "+STR.HAT+"z"+STR.ENDHAT+"H"+STR.SUB+"z"+STR.ENDSUB;
        //nextX = STR.displayString(s,g, state.s15,state.s85+3*deltaY);
	g.drawImage(bullet,state.s15,state.s85+3*deltaY-bullet.getHeight(),this);
        s = " Select the magnetic field "+STR.VBAR+"H"+STR.ENDVBAR+
            " = "+STR.HAT+"z"+STR.ENDHAT+"H"+STR.SUB+"z"+STR.ENDSUB;
        nextX = STR.displayString(s,g, state.s15+bullet.getWidth(),state.s85+3*deltaY);

        //g.drawString("\u2022 Select the time step between updates",
        //             state.s15,state.s85+4*deltaY);
	g.drawImage(bullet,state.s15,state.s85+4*deltaY-bullet.getHeight(),this);
        g.drawString(" Select the time step between updates",
                     state.s15+bullet.getWidth(),state.s85+4*deltaY);
	
        //OUTPUT
        int newY = state.s120+2*deltaY;
        g.setColor(Color.red);
        g.setFont(new Font("SanSerif",Font.BOLD,state.font15));
        newY = newY+2*deltaY;
        g.drawString("Displayed Information", state.s15,newY);
        g.setColor(Color.black);
        //g.setFont(state.ttfFont.deriveFont(14f));
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        newY += state.s25;
        //s = "\u2022 Select to observe either:";
        //nextX = STR.displayString(s,g, state.s15,newY);
	g.drawImage(bullet,state.s15,newY-bullet.getHeight(),this);
        s = " Select to observe either:";
        nextX = STR.displayString(s,g, state.s15+bullet.getWidth(),newY);
        
        newY += deltaY;
        s = "   (a) the electron motion in the x-y plane with the electric "+"and magnetic";
        nextX = STR.displayString(s,g,state.s15,newY);
        
        newY += deltaY;
        s = "       forces acting on it displayed as red and blue arrows, "+
            "respectively, or";
        nextX = STR.displayString(s,g,state.s15,newY);
        
        s = "   (b) the trace generated by the moving electron";
        newY += deltaY;
        nextX = STR.displayString(s,g,state.s15,newY);
        
        newY += 15*deltaY/10;
        //s = "\u2022 Select to place the electron inside a closed boundary "+
        //    "or in an open";
        //nextX = STR.displayString(s,g,state.s15,newY);
	g.drawImage(bullet,state.s15,newY-bullet.getHeight(),this);
        s = " Select to place the electron inside a closed boundary "+
            "or in an open";
        nextX = STR.displayString(s,g,state.s15+bullet.getWidth(),newY);
        
        s = "   boundary";
        newY += deltaY;
        nextX = STR.displayString(s,g,state.s15,newY);
        
        g.setColor(Color.red);
        //g.setFont(state.ttfFont.deriveFont(Font.BOLD,15f));
        g.setFont(new Font("SanSerif",Font.BOLD,state.font15));
        
        newY = newY+2*deltaY;
        g.drawString("Time Step Selection", state.s15,newY);
        g.setColor(Color.black);
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        
        newY += state.s25;
        s = "Accuracy of the time-dependent simulation depends on the choice of time step.";
        nextX = STR.displayString(s,g, state.s15,newY);
        
        newY += + deltaY;
        s = "A maximum recommended time step is given in order to reproduce faithfully a";
        nextX = STR.displayString(s,g, state.s15,newY);
        
        newY += + deltaY;
        s = "long series of boundary reflections with the ''closed boundary'' option selected.";
        nextX = STR.displayString(s,g, state.s15,newY);
        
        newY += + deltaY;
        s = "In order to speed up the animation, in general it is fine to increase the time step";
        nextX = STR.displayString(s,g, state.s15,newY);
        
        newY += + deltaY;
        s = "particularly when ''open boundary'' is selected, without loss of accuracy.";
        nextX = STR.displayString(s,g, state.s15,newY);
        
        g.setColor(Color.blue);
        newY += + deltaY + state.s5;
        s = "If the particle trace does not appear continuous, reduce the time step instead.";
        nextX = STR.displayString(s,g, state.s15,newY);

        drawCopyRight(g);
        //MaestroG.useBiggerFont = saveOldFlag;
    }

    public void drawCopyRight(Graphics g) {
        int deltaY = state.s20;
        int startingY = getSize().height - state.s70;
        int startingX = 0;
        int DeltaX = state.s10;
        FontMetrics fm;
        
        g.setColor(Color.black);
        g.setColor(Color.black);
        //g.setFont(normalfont14);
        //g.setFont(state.ttfFont.deriveFont(14f));
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        fm = g.getFontMetrics();
        g.drawString("Applet Design: Umberto Ravaioli",
                     startingX+DeltaX,startingY+deltaY);
        
        //MaestroG.superscripter("Interactive Java","TM"," platform:  www.amanogawa.com",
        //                       g,14,startingX,startingY+2*deltaY);
        String s = "Interactive Java"+STR.SUP+"TM"+STR.ENDSUP+
            " platform:  www.amanogawa.com";
        int nextX = STR.displayString(s,g,startingX+DeltaX,startingY+2*deltaY);
        int lineLength = fm.stringWidth("Interactive JavaTM platform:  www.amanogawa.com");
        lineLength+= state.s10;

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

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

