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


public class Instructions extends Panel {
    
    //private static final Color bgcolor = new Color(236,236,236);
    private static final Color bgcolor = Color.white;
    
    public TextField epsilon;
    private Paint paint;
    
    private Image im;
    private Graphics buf;
    private BufferedImage figure;

    Mod1State state;
    public Button closeIt;

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

	getImages();
	setBackground(bgcolor);
	closeIt = new Button("CLOSE");
        closeIt.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        closeIt.setBackground(new Color(240,240,255));
	add(closeIt);
        
        int buttonx = state.s440;
        int buttony = state.s440;
        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.setFont(font_one);
        //fm = g.getFontMetrics();

        //boolean saveOldFlag = MaestroG.useBiggerFont;
        //MaestroG.useBiggerFont = false;
        
        try {
            figure = ImageIO.read(getClass().getResource("mod5.1302.gif"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        int xstart, ystart, ystart2, newY;
        
        xstart = state.s40;
        ystart = state.s30;
        ystart2 = state.s60;
        newY = ystart2;
        
        g.setColor(Color.black);
        //g.setFont(state.ttfFont.deriveFont(Font.BOLD,16f));
        g.setFont(new Font("SanSerif",Font.BOLD,state.font16));
        
        g.drawString("Instructions", xstart,ystart);
        
        g.setColor(Color.red);
        //g.setFont(state.ttfFont.deriveFont(Font.BOLD,15f));
        g.setFont(new Font("SanSerif",Font.BOLD,state.font15));
        g.drawString("Input", xstart,newY);
        
        //g.drawString("Output", 230,50);
        g.setColor(Color.black);
        g.setFont(state.ttfFont.deriveFont(14f));
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        
        newY += state.s20;
        int deltaY = state.s20;
        
        //INPUT
        //String s = "\u2022 Place line sources carrying currents flowing"+
        //    " in the positive ";
        //int nextX = STR.displayString(s,g, xstart,newY);
	g.drawImage(bullet,xstart,newY-bullet.getHeight(),this);
        String s = " Place line sources carrying currents flowing"+
            " in the positive ";
        int nextX = STR.displayString(s,g, xstart+bullet.getWidth(),newY);
	
        s = "  or negative "+STR.BOLD+"z"+STR.ENDBOLD+"-direction in the"+
            " display plane (x-y plane)";
        nextX = STR.displayString(s,g, xstart,newY+deltaY);
        //g.drawString("\u2022 You can select line sources"+
        //             " on the canvas, move them to ", xstart,newY+2*deltaY);
	g.drawImage(bullet,xstart,newY+2*deltaY-bullet.getHeight(),this);
        g.drawString(" You can select line sources"+
                     " on the canvas, move them to ", xstart+bullet.getWidth(),newY+2*deltaY);
		
        g.drawString("  different locations, and change their values",
                     xstart,newY+3*deltaY);
       //OUTPUT
        g.setColor(Color.red);
        //g.setFont(state.ttfFont.deriveFont(Font.BOLD,15f));
        g.setFont(new Font("SanSerif",Font.BOLD,state.font15));
        
        newY = state.s70+4*deltaY+state.s30;
        g.drawString("Displayed Information", xstart,newY);
        
        g.setColor(Color.black);
        g.setFont(state.ttfFont.deriveFont(14f));
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        newY += state.s20;
        
        //s = "\u2022 Spatial pattern of the magnetic flux "+
        //    STR.BOLD+"B"+STR.ENDBOLD+"(x,y) ";
        //nextX = STR.displayString(s,g, xstart,newY);
	g.drawImage(bullet,xstart,newY-bullet.getHeight(),this);
        s = " Spatial pattern of the magnetic flux "+
            STR.BOLD+"B"+STR.ENDBOLD+"(x,y) ";
	nextX = STR.displayString(s,g, xstart+bullet.getWidth(),newY);
	
        //s = "\u2022 Magnitude and direction of "+STR.BOLD+"B"+STR.ENDBOLD+
        //    "(x,y) at cursor location (x,y)";
        //nextX = STR.displayString(s,g,xstart,newY+deltaY);
	g.drawImage(bullet,xstart,newY+deltaY-bullet.getHeight(),this);
        s = " Magnitude and direction of "+STR.BOLD+"B"+STR.ENDBOLD+
            "(x,y) at cursor location (x,y)";
        nextX = STR.displayString(s,g,xstart+bullet.getWidth(),newY+deltaY);
	
        //g.drawImage(figure,state.s10,state.s290,this);
        drawCopyRight(g);

        //MaestroG.useBiggerFont = saveOldFlag;
        
        
        //Draw scalable picture ------------------------------------------------
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        int x1, x2, x3, x4, y1, y2, y3, y4, th1;
        
        // Just move these two points to shift the picture
        x1 = state.s60;             y1 = state.s435;
        //----------------------------------------------------------------------
        x2 = x1 + state.s80;        y2 = y1 - state.s120;
        x3 = x1 + state.s270;       y3 = y1 - state.s120;
        x4 = x1 + state.s230;       y4 = y1;
        //----------------------------------------------------------------------
        th1 = state.s8;
        
        // Bottom part of wires
        MaestroG.drawLineThickA(g,(double)((x2 - state.s10)),(double)((y4+y3)/2+state.s120),
                    (double)((x2 - state.s10)),(double)((y3+3*(y4-y3)/4)),th1,Color.red);
        
        // highlights
        MaestroG.drawLineThick(g,(double)((x2 - state.s12)),(double)((y4+y3)/2+state.s123),
                    (double)((x2 - state.s12)),(double)((y3+3*(y4-y3)/4)+state.s3),state.s2,Color.white);
        MaestroG.drawLineThick(g,(double)((x2 - state.s11+1)),(double)((y4+y3)/2+state.s123),
                    (double)((x2 - state.s11+1)),(double)((y3+3*(y4-y3)/4)+state.s3),state.s1,Color.pink);
        

        // current vector pointing down
        g.setColor(Color.red);
        MaestroG.drawArrowScaled( (x2 - state.s10), (y4+y3)/2+state.s140, 2, 2*state.sfactor, g);
        MaestroG.drawLineThickA(g,(double)(x2 - state.s10), (double)((y4+y3)/2+state.s145),
                    (double)(x2 - state.s10), (double)((y4+y3)/2+state.s130),state.s2,Color.red); 
        
        MaestroG.drawLineThickA(g,(double)((x4 - state.s20)),(double)((y4+y3)/2+state.s80),
                    (double)((x4 - state.s20)),(double)((y3+(y4-y3)/4)),th1,Color.red);
        
        // highlights
        MaestroG.drawLineThick(g,(double)((x4 - state.s22)),(double)((y4+y3)/2+state.s83),
                    (double)((x4 - state.s22)),(double)((y3+(y4-y3)/4)+state.s3),state.s2,Color.white);
        MaestroG.drawLineThick(g,(double)((x4 - state.s21)),(double)((y4+y3)/2+state.s83),
                    (double)((x4 - state.s21)),(double)((y3+(y4-y3)/4)+state.s3),state.s1,Color.pink);
        
        // current vector pointing up
        g.setColor(Color.red);
        MaestroG.drawArrowScaled((x4 - state.s20), (y4+y3)/2+state.s103, 1, 2*state.sfactor, g);
        MaestroG.drawLineThickA(g,(double)(x4 - state.s20), (double)((y4+y3)/2+state.s112),
                    (double)(x4 - state.s20), (double)((y4+y3)/2+state.s100),state.s2,Color.red); 
        
        // Semi-transparent 2D surface
        int rule; float alpha;
        rule = AlphaComposite.SRC_OVER;
        alpha = 0.9f;
        g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
        
        g2d.setStroke(new BasicStroke(state.s1));       
        
            Polygon xyplane = new Polygon();
            xyplane.addPoint(x1, y1);
            xyplane.addPoint(x2, y2);
            xyplane.addPoint(x3,y3);
            xyplane.addPoint(x4,y4);
            
        //g.setColor(new Color(79,188,240));
        g.setColor(new Color(199,208,250));
        g.fillPolygon(xyplane);
        g.setColor(Color.gray);
        g.drawPolygon(xyplane);
        
        alpha = 1.0f;
        g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
        
        // y axis
        MaestroG.drawLineThickA(g,(double)((x1+x2)/2),(double)((y1+y2)/2),
                    (double)((x4+x3)/2 + state.s20),(double)((y1+y2)/2),state.s0,Color.black);        
        MaestroG.drawArrowScaled( (x4+x3)/2 + state.s18, (y1+y2)/2, 3, 1.5*state.sfactor, g);
        // x axis
        MaestroG.drawLineThickA(g,(double)((x2+x3)/2),(double)((y2+y3)/2),
                    (double)((x4+x1)/2 - state.s8),(double)((y1+y4)/2+ state.s16),state.s0,Color.black);
        //draw oblique arrow head
		Polygon pM = new Polygon();
                int x, y; double sf = state.sfactor;
                x = (x4+x1)/2 - state.s12;
                y = (y1+y4)/2+ state.s24;
                pM.addPoint(x,y);
		pM.addPoint(x+(int)(sf*3),y-(int)(sf*11));
		pM.addPoint(x+(int)(sf*8),y-(int)(sf*9));
		
                //g.drawPolygon(pM);
		g.fillPolygon(pM);
              
        // z axis
        MaestroG.drawLineThickA(g,(double)(((x4+x1)/2+(x2+x3)/2)/2),(double)((y4+y3)/2),
                    (double)(((x4+x1)/2+(x2+x3)/2)/2),(double)((y3+y4)/2- state.s100),state.s0,Color.black); 
        MaestroG.drawArrowScaled( ((x4+x1)/2+(x2+x3)/2)/2, (y3+y4)/2- state.s98, 1, 1.5*state.sfactor, g);
        
        //TOP PARTS OF WIRES
        
        //First wire
        MaestroG.drawLineThickA(g,(double)((x2 - state.s10)),(double)((y4+y3)/2-state.s80),
                    (double)((x2 - state.s10)),(double)((y3+3*(y4-y3)/4)),th1,Color.red);
        // highlights
        MaestroG.drawLineThick(g,(double)((x2 - state.s12)),(double)((y4+y3)/2-state.s80),
                    (double)((x2 - state.s12)),(double)((y3+3*(y4-y3)/4)+state.s3),state.s2,Color.white);
        MaestroG.drawLineThick(g,(double)((x2 - state.s11+1)),(double)((y4+y3)/2-state.s80),
                    (double)((x2 - state.s11+1)),(double)((y3+3*(y4-y3)/4)+state.s3),state.s1,Color.pink);
        
        // current vector pointing down
        g.setColor(Color.red);
        MaestroG.drawArrowScaled( (x2 - state.s10), (y4+y3)/2-state.s103, 2, 2*state.sfactor, g);
        MaestroG.drawLineThickA(g,(double)(x2 - state.s10), (double)((y4+y3)/2-state.s98),
                    (double)(x2 - state.s10), (double)((y4+y3)/2-state.s113),state.s2,Color.red); 
        
        //Second wire
        MaestroG.drawLineThickA(g,(double)((x4 - state.s20)),(double)((y4+y3)/2-state.s100),
                    (double)((x4 - state.s20)),(double)((y3+(y4-y3)/4)),th1,Color.red);
        
        // highlights
        MaestroG.drawLineThick(g,(double)((x4 - state.s22)),(double)((y4+y3)/2-state.s100),
                    (double)((x4 - state.s22)),(double)((y3+(y4-y3)/4)+state.s3),state.s2,Color.white);
        MaestroG.drawLineThick(g,(double)((x4 - state.s21)),(double)((y4+y3)/2-state.s100),
                    (double)((x4 - state.s21)),(double)((y3+(y4-y3)/4)+state.s3),state.s1,Color.pink);
        
        // current vector pointing up
        g.setColor(Color.red);
        MaestroG.drawArrowScaled((x4 - state.s20), (y4+y3)/2-state.s118, 1, 2*state.sfactor, g);
        MaestroG.drawLineThickA(g,(double)(x4 - state.s20), (double)((y4+y3)/2-state.s125),
                    (double)(x4 - state.s20), (double)((y4+y3)/2-state.s110),state.s2,Color.red); 
        
        g.setColor(Color.black);
        g.setFont(new Font("Serif",Font.PLAIN,state.font16));
        
        g.drawString("Line", x1 - state.s2, (y4+y3)/2-state.s70);
        g.drawString("source 2", x1 - state.s2, (y4+y3)/2-state.s55);
        
        g.drawString("Line", x4 - state.s7, (y4+y3)/2-state.s90);
        g.drawString("source 1", x4 - state.s7, (y4+y3)/2-state.s75);
        
        g.drawString("x", (x4+x1)/2 - state.s19,(y1+y4)/2+ state.s35);
        g.drawString("y", (x4+x3)/2 + state.s34,(y1+y2)/2 +state.s2);
        g.drawString("z", ((x4+x1)/2+(x2+x3)/2)/2 +state.s6,(y3+y4)/2 - state.s105);
        
        
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
    }


    public void drawCopyRight(Graphics g) {
        int deltaY = state.s20;
        int startingY = getSize().height - state.s60;
        int startingX = 0;
        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();

        startingY += deltaY;
        g.drawString("  Applet Design: Fawwaz Ulaby and Janice Richards",
                     startingX,startingY+deltaY+state.s3);
                
        int lineLength = fm.stringWidth("  Applet Design: Fawwaz Ulaby and Janice Richards");
        lineLength+= state.s10;
        
        //g.setFont(state.ttfFont.deriveFont(14f));
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        
        g.drawLine(startingX,startingY+state.s5,
                   startingX+lineLength+10,startingY+state.s5);
        g.drawLine(startingX+lineLength+state.s10,startingY+state.s5,
                   startingX+lineLength+state.s10,startingY+2*deltaY);
    }

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

