// Instructions.java

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);
    
    public TextField epsilon;
    private Paint paint;
    
    private Image im;
    private Graphics buf;
    
    public Button bupdate;
    Antenna ant;
    
    private BufferedImage bullet;
    
    public Instructions(Antenna ant){
	super();
        this.ant = ant; 
	setLayout(null);

	getImages();
	setBackground(bgcolor);
	bupdate = new Button("CLOSE");
        bupdate.setBackground(new Color(240,240,255));
        bupdate.setFont(new Font("SanSerif",Font.PLAIN,ant.font12));
	add(bupdate);
        
        int buttonx = ant.s500+ant.s50;
        int buttony = ant.s300+ant.s40;
        int buttonwide = ant.s70;
        int buttonheight = ant.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) (ant.sfactor * img.getWidth());
	    int targetHeight = (int) (ant.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;
        int nextX, newY;
        int xstart = ant.s50;
        int ystart = ant.s40;
        int xstart2 = ant.s300;
        int ystart2 = ant.s200;
        int deltaY = ant.s20;
        
        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(new Font("SanSerif",Font.BOLD,ant.font16));
        g.drawString("Instructions", xstart,ystart);
        
        g.setFont(new Font("SanSerif",Font.PLAIN,ant.font14));
        String s = "This module displays the far-field radiation patterns of "+
            
        STR.BOLD+"E"+STR.ENDBOLD+" and "+
            STR.BOLD+"S"+STR.ENDBOLD+STR.SUB+"av"+STR.ENDSUB;
            nextX = STR.displayString(s,g,xstart2,ystart);
        
        s = " in elevation  (\u03b8-plane)  and azimuth  ("+"\u03c6"+"-plane)  for two";
        nextX = STR.displayString(s,g,xstart2,ystart + deltaY);
        
        s = " linear dipoles oriented along the z-direction.";
        nextX = STR.displayString(s,g,xstart2,ystart + 2*deltaY);
        
        newY = ystart + 4*deltaY;
        
        g.setColor(Color.red);
        g.setFont(new Font("SanSerif",Font.BOLD,ant.font15));
        g.drawString("Input", xstart, newY);
        
        g.setColor(Color.black);
        g.setFont(new Font("SanSerif",Font.PLAIN,ant.font14));
        
        newY += 15*deltaY/10;
        
        //INPUT
        //s = "\u2022 Current amplitudes and lengths of dipoles A and B";
        //nextX = STR.displayString(s,g,xstart,newY);
	g.drawImage(bullet,xstart,newY-bullet.getHeight(),this);
        s = " Current amplitudes and lengths of dipoles A and B";
        nextX = STR.displayString(s,g,xstart+bullet.getWidth(),newY);	
        
        newY += deltaY;
        //s = "\u2022 Distance between dipoles A and B";
        //nextX = STR.displayString(s,g,xstart,newY);
	g.drawImage(bullet,xstart,newY-bullet.getHeight(),this);
        s = " Distance between dipoles A and B";
        nextX = STR.displayString(s,g,xstart+bullet.getWidth(),newY);
        
        newY += deltaY;
        //s = "\u2022 Phase of dipole B relative to that of A";
        //nextX = STR.displayString(s,g,xstart,newY);
	g.drawImage(bullet,xstart,newY-bullet.getHeight(),this);
        s = " Phase of dipole B relative to that of A";
        nextX = STR.displayString(s,g,xstart+bullet.getWidth(),newY);
        
        //OUTPUT
        g.setColor(Color.red);
        g.setFont(new Font("SanSerif",Font.BOLD,ant.font15));
        
        newY += 2*deltaY;
        g.drawString("Displayed Information", xstart,newY);
        
        g.setColor(Color.black);
        g.setFont(new Font("SanSerif",Font.PLAIN,ant.font14));
        newY += 15*deltaY/10;
        //s = "\u2022 Radiation patterns";
        //nextX = STR.displayString(s,g,xstart,newY);
	g.drawImage(bullet,xstart,newY-bullet.getHeight(),this);
        s = " Radiation patterns";
        nextX = STR.displayString(s,g,xstart+bullet.getWidth(),newY);
        
        //s = "\u2022 Total radiated power";
        //nextX = STR.displayString(s,g,xstart,newY+deltaY);
	g.drawImage(bullet,xstart,newY+deltaY-bullet.getHeight(),this);
        s = " Total radiated power";
        nextX = STR.displayString(s,g,xstart+bullet.getWidth(),newY+deltaY);
        
        //s = "\u2022 Directivity";
        //nextX = STR.displayString(s,g,xstart,newY+2*deltaY);
        g.drawImage(bullet,xstart,newY+2*deltaY-bullet.getHeight(),this);
        s = " Directivity";
        nextX = STR.displayString(s,g,xstart+bullet.getWidth(),newY+2*deltaY);
	
        drawCopyRight(g);
    }


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

        g.drawString("All Rights Reserved",startingX+ant.s15,startingY+3*deltaY);
        g.drawLine(startingX,startingY+ant.s5,
                   startingX+15+lineLength+ant.s10,startingY+ant.s5);
        g.drawLine(startingX+15+lineLength+ant.s10,startingY+ant.s5,
                   startingX+15+lineLength+ant.s10,startingY+4*deltaY);
    }
//----------------------------------------------------------------------------------------    
}//End
    

