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

    //Mod1State state;
    public Button bupdate;
    NewGuide_State state;
    
    private BufferedImage bullet;
  
    public Instructions(NewGuide_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.s600;
        int buttony = state.s200+state.s55;
        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);

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

        //boolean saveOldFlag = MaestroG.useBiggerFont;
        //MaestroG.useBiggerFont = false;
        
        g.setColor(Color.black);
        g.setFont(new Font("SanSerif",Font.BOLD,state.font16));
        
        //g.drawString("Instructions", 20,25);
        
        String s;
        int nextX;
        int newY;
        int xleft = state.s30;
        int deltaY = state.s20;
        int ystart = state.s25;
        
        g.setColor(Color.red);
        g.setFont(new Font("SanSerif",Font.BOLD,state.font15));

        g.drawString("Input", xleft,ystart);
        g.setColor(Color.black);
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        
        //INPUT
        newY = ystart+deltaY;
        //s = "\u2022 Ground distance between antennas "+STR.ITAL+"d"+STR.ENDITAL;
        //nextX = STR.displayString(s,g,xleft,newY);
	g.drawImage(bullet,xleft,newY-bullet.getHeight(),this);
        s = " Ground distance between antennas "+STR.ITAL+"d"+STR.ENDITAL;
        nextX = STR.displayString(s,g,xleft+bullet.getWidth(),newY);
	
        newY += deltaY;
        //s = "\u2022 Elevation of antennas "+STR.ITAL+"h"+STR.ENDITAL+STR.SUB+"1"
        //    +STR.ENDSUB+STR.ITAL+" and  h"+STR.ENDITAL+STR.SUB+"2"+STR.ENDSUB;
        //nextX = STR.displayString(s,g,xleft,newY);
	g.drawImage(bullet,xleft,newY-bullet.getHeight(),this);
        s = " Elevation of antennas "+STR.ITAL+"h"+STR.ENDITAL+STR.SUB+"1"
            +STR.ENDSUB+STR.ITAL+" and  h"+STR.ENDITAL+STR.SUB+"2"+STR.ENDSUB;
        nextX = STR.displayString(s,g,xleft+bullet.getWidth(),newY);
	
        newY += deltaY;
        //s = "\u2022 Frequency of operation "+STR.ITAL+"f"+STR.ENDITAL;
        //nextX = STR.displayString(s,g,xleft,newY);
	g.drawImage(bullet,xleft,newY-bullet.getHeight(),this);
        s = " Frequency of operation "+STR.ITAL+"f"+STR.ENDITAL;
        nextX = STR.displayString(s,g,xleft+bullet.getWidth(),newY);
	
        newY += deltaY;
        //s = "\u2022 Conductivity and permittivity of the terrain ";
        //nextX = STR.displayString(s,g,xleft,newY);
	g.drawImage(bullet,xleft,newY-bullet.getHeight(),this);
        s = " Conductivity and permittivity of the terrain ";
        nextX = STR.displayString(s,g,xleft+bullet.getWidth(),newY);
        
        newY += deltaY + state.s10;
        g.setColor(Color.red);
        g.setFont(new Font("SanSerif",Font.BOLD,state.font15));
        g.drawString("Assumptions", xleft,newY);
        g.setColor(Color.black);
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        newY += deltaY;
        //s = "\u2022 Earth curvature is neglected";
        //nextX = STR.displayString(s,g,xleft,newY);
	g.drawImage(bullet,xleft,newY-bullet.getHeight(),this);
        s = " Earth curvature is neglected";
        nextX = STR.displayString(s,g,xleft+bullet.getWidth(),newY);
	
        newY += deltaY;
        //s = "\u2022 No obstruction on the reflected path";
        //nextX = STR.displayString(s,g,xleft,newY);
	g.drawImage(bullet,xleft,newY-bullet.getHeight(),this);
        s = " No obstruction on the reflected path";
        nextX = STR.displayString(s,g,xleft+bullet.getWidth(),newY);
	
        newY += deltaY;
        //s = "\u2022 Uniform atmosphere ("+STR.ITAL+"\u03b5"+STR.ENDITAL+STR.SUB+"r"
        //    +STR.ENDSUB+" = 1.0 )";
        //nextX = STR.displayString(s,g,xleft,newY);
	g.drawImage(bullet,xleft,newY-bullet.getHeight(),this);
        s = " Uniform atmosphere ("+STR.ITAL+"\u03b5"+STR.ENDITAL+STR.SUB+"r"
            +STR.ENDSUB+" = 1.0 )";
        nextX = STR.displayString(s,g,xleft+bullet.getWidth(),newY);

	newY += deltaY;
        //s = "\u2022 Uniform radiation pattern in the plane of incidence";
        //nextX = STR.displayString(s,g,xleft,newY);
	g.drawImage(bullet,xleft,newY-bullet.getHeight(),this);
        s = " Uniform radiation pattern in the plane of incidence";
        nextX = STR.displayString(s,g,xleft+bullet.getWidth(),newY);
                
        // SECOND COLUMN
        
        //OUTPUT
        int xright = state.s400+state.s20;
        ystart = 0;
        newY = ystart + state.s20;
        
        g.setColor(Color.red);
        g.setFont(new Font("SanSerif",Font.BOLD,state.font15));
        g.drawString("Displayed Information", xright,newY);
        g.setColor(Color.black);
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        newY += state.s20;
        //s = "\u2022 Vector interference diagram for the resultant signal at the receiver.";
        //nextX = STR.displayString(s,g,xright,newY);
	g.drawImage(bullet,xright,newY-bullet.getHeight(),this);
        s = " Vector interference diagram for the resultant signal at the receiver.";
	nextX = STR.displayString(s,g,xright+bullet.getWidth(),newY);
	
	
        //s = "\u2022 Complex reflection coefficient for perpendicular (horizontal) polarization";
        //nextX = STR.displayString(s,g,xright,newY+deltaY);
	g.drawImage(bullet,xright,newY+deltaY-bullet.getHeight(),this);
        s = " Complex reflection coefficient for perpendicular (horizontal) polarization";
        nextX = STR.displayString(s,g,xright+bullet.getWidth(),newY+deltaY);
		
        s = "or parallel (vertical) polarization, and phase delay information.";
        nextX = STR.displayString(s,g,xright,newY+2*deltaY);
	
        
        g.setColor(Color.blue.darker());
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        newY += state.s75;
        s = "At sufficiently high frequencies, communication between two antennas takes";
        nextX = STR.displayString(s,g,xright,newY);
        newY += state.s20;
        
        s = "place mainly through superposition of a direct and a ground-reflected signal.";
        nextX = STR.displayString(s,g,xright,newY);
        newY += state.s20;
        s = "The different travel paths generate a phase delay for the reflected signal.";
        nextX = STR.displayString(s,g,xright,newY);
        newY += state.s20;
        
        s = "In addition, the complex reflection coefficient related to the conductivity and";
        nextX = STR.displayString(s,g,xright,newY);
        newY += state.s20;
        
        s = "the permittivity of the terrain causes additional phase shift and attenuation.";
        nextX = STR.displayString(s,g,xright,newY);
        newY += state.s20;
        
        s = "For a fixed transmitter, the elevation of the receiving antenna determines ";
        nextX = STR.displayString(s,g,xright,newY);
        newY += state.s20;
        
        s = "whether there is constructive or destructive interference of the two signals.";
        nextX = STR.displayString(s,g,xright,newY);
               
        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("Application 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 + state.s5,
                   startingX+15+lineLength + state.s10,startingY + state.s5);
        g.drawLine(startingX+15+lineLength + state.s10,startingY + state.s5,
                   startingX+15+lineLength + state.s10,startingY+4*deltaY);
    }

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

