// 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(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 bupdate;
    
    NewGuide_State state;
    
    private BufferedImage bullet, less_or_equal;
  
    public Instructions(NewGuide_State state){
	super();
        this.state = state;

	getImages();
        setLayout(null);
	setBackground(bgcolor);
	bupdate = new Button("CLOSE");
        bupdate.setFont(new Font("SanSerif",Font.BOLD,state.font12));
        bupdate.setBackground(new Color(240,240,255));
	add(bupdate);
        int buttonx = 68*state.s900/100;
        int buttony = 72*state.s300/100;
        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();

	    img = ImageIO.read(getClass().getResource("less_or_equal.png"));
	    targetWidth = (int) (state.sfactor * img.getWidth());
	    targetHeight = (int) (state.sfactor * img.getHeight());

	    less_or_equal =
		new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
	    graphics2D = less_or_equal.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 flickering
    public void update(Graphics g){	
            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(new Font("SanSerif",Font.BOLD,state.font16));
        
        String s;
        int nextX;
        int xpos = state.s25;
        
        g.drawString("Instructions", xpos, state.s25);
        
        g.setColor(Color.red);
        g.setFont(new Font("SanSerif",Font.BOLD,state.font14));
        g.drawString("Input", xpos,state.s50);
        g.setColor(Color.black);
        int deltaY = state.s20;
        int starty = state.s70;
        //INPUT
        //MaestroG.special3("\u2022 Antenna diameter  ","d = ","2","a",g,state.font14,xpos,starty);
	MaestroG.ImgSpecial3(bullet," Antenna diameter  ","d = ","2","a",g,state.font14,xpos,starty,this);
	    
        //MaestroG.specialgreek("\u2022 Taper factor "," \u03b1","","",g,state.font14,xpos,starty+deltaY);
	MaestroG.ImgSpecialgreek(bullet," Taper factor "," \u03b1","","",g,state.font14,xpos,starty+deltaY,this);
	    
        //OUTPUT
        g.setColor(Color.red);
        g.setFont(new Font("SanSerif",Font.BOLD,state.font14));
        int newY = starty+2*deltaY+state.s10;
        g.drawString("Displayed Information", xpos,newY);
        g.setColor(Color.black);
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        
        newY += state.s20;
        //s = "\u2022 Radiation pattern";
        //nextX = STR.displayString(s,g,xpos,newY);
	g.drawImage(bullet,xpos,newY-bullet.getHeight(),this);
        s = " Radiation pattern";
        nextX = STR.displayString(s,g,xpos+bullet.getWidth(),newY);
        
        //MaestroG.special3("\u2022 Antenna Directivity  ","D","","",g,state.font14,xpos,newY+deltaY);
	MaestroG.ImgSpecial3(bullet," Antenna Directivity  ","D","","",g,state.font14,xpos,newY+deltaY,this);
	
        //MaestroG.specialgreek("\u2022 Antenna beamwidth  ","\u03b2","","",g,state.font14,xpos,newY+2*deltaY);
	MaestroG.ImgSpecialgreek(bullet," Antenna beamwidth  "," \u03b2","","",g,state.font14,xpos,newY+2*deltaY,this);
	
        //======================================================================
        xpos = state.s300+state.s20;
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        s = "The small horn illuminates the dish antenna with:";
        nextX = STR.displayString(s,g,xpos,2*deltaY);
        //======================================================================
        //MaestroG.Formula1("E","~","a"," ( ","r","a ",")","  =  ","E","0 ","["," 1 \u2212 ","\u03b1 "," r","a","___","a ","]", g, state.font16, xpos + state.s30,4*deltaY);

	// NEW:
	MaestroG.Formula1("E","~","a"," ( ","r","a ",")","  =  ","E","0 ",
			  "["," 1 - ","\u03b1 "," r","a","___","a ","]",
			  g, state.font16, xpos + state.s30,4*deltaY);

	
        //MaestroG.special3("0 \u2264 "," \u03b1"," \u2264 1.0","",g,state.font14,xpos + state.s90,55*deltaY/10);
	MaestroG.Img2Special3("0",less_or_equal,"\u03b1",less_or_equal,"1.0",g,state.font14,xpos + state.s90,55*deltaY/10,this);
        
        // END OF EQUATION
        //======================================================================
        
        MaestroG.special3("Uniform illumination (no taper): "," \u03b1"," = 0","",g,state.font14,xpos,75*deltaY/10);
        MaestroG.special3("Steepest taper: "," \u03b1"," = 1","",g,state.font14,xpos,87*deltaY/10); 
        //g.drawImage(figure,state.s400+state.s35,8*deltaY,this);
       
        // SCALABLE FIGURE OF ANTENNA
        int xforw = state.s30;
        int xcenter = state.s40;
        int ycenter = state.s48;
        int myh = 20*getSize().height/100;
        int myw = 79*getSize().width/100;
        Color bluette = new Color(10,150,255);
        
        // LABELS AND AXIS
        MaestroG.subscripterSymbol3B("r","a","",g,state.font16,myw+xforw-state.s40, myh-state.s25);
        MaestroG.subscripter("0","","",g,state.font15,myw+xforw-state.s36, myh+ycenter+state.s4);
        MaestroG.subscripter("a","","",g,state.font15,myw+xforw-state.s36, myh+state.s4);
        
        MaestroG.drawLineThick(g, myw+xforw-state.s5, (double)(myh)+ycenter, 
                (double)(myw)+xforw-state.s25, (double)(myh)+ycenter, 1, Color.gray.brighter());
        MaestroG.drawLineThick(g, myw+xforw-state.s15, (double)(myh), 
                (double)(myw)+xforw-state.s25, (double)(myh), 1, Color.gray.brighter());
        MaestroG.drawLineThick(g, myw+xforw-state.s20, (double)(myh)+ycenter+state.s5 , 
                (double)(myw)+xforw-state.s20, (double)(myh)-state.s20, 1, new Color(0,200,0));
        MaestroG.drawArrowScaled(myw+xforw-state.s20,myh-state.s20, 1, state.sfactor, g);
        
        //======================================================================
        // PARABOLOID
        MaestroG.drawArcThick(g, (double)(myw)+xforw, (double)(myh), 
                (double)state.s70, (double)state.s100, 110.0, 140.0, state.s2, Color.black);
        
        g.setColor(Color.white);
        g.fillRect(myw+state.s9+xforw, myh-state.s13, state.s100, state.s150);
        
        MaestroG.drawLineThick(g, (double)(myw)+state.s7+xforw, (double)(myh+state.s80), 
                (double)(myw)+state.s25+xforw, (double)(myh)+state.s110, state.s2, Color.black);
        MaestroG.drawLineThick(g, (double)(myw)+state.s7+xforw, (double)(myh)+state.s17, 
                (double)(myw)+state.s25+xforw, (double)(myh)-state.s13, state.s2, Color.black);
        
        g.setColor(Color.white);
        g.fillRect(myw+state.s18+xforw, myh-state.s20, state.s100, state.s180);
        //======================================================================
        // FEEDER
        MaestroG.drawBendThickA(g, (double)(myw)+xforw+xcenter+state.s4, (double)(myh)+ycenter, 
                                   (double)(myw)+xforw+xcenter+state.s18, (double)(myh)+ycenter, 
                                   (double)(myw)+xforw+xcenter-state.s23, (double)(myh)+2*ycenter+state.s20, 
                                   state.s4+2, Color.black);
        
        MaestroG.drawLineThick(g, (double)(myw)+xforw+xcenter+state.s3, (double)(myh)+ycenter-state.s2-1, 
                (double)(myw)+xforw+xcenter, (double)(myh)+ycenter-state.s5-1, 1, Color.black);
        MaestroG.drawLineThick(g, (double)(myw)+xforw+xcenter+state.s3, (double)(myh)+ycenter+state.s2+1, 
                (double)(myw)+xforw+xcenter, (double)(myh)+ycenter+state.s5+1, 1, Color.black);
        
        MaestroG.drawBendThickA(g, (double)(myw)+xforw+xcenter+state.s1, (double)(myh)+ycenter, 
                                   (double)(myw)+xforw+xcenter+state.s18, (double)(myh)+ycenter, 
                                   (double)(myw)+xforw+xcenter-state.s23, (double)(myh)+2*ycenter+state.s20, 
                                   state.s4, Color.white);
        
        //shorten feeder
        g.setColor(Color.white);
        g.fillRect(myw+xforw+xcenter-state.s35, myh+2*ycenter+state.s15, state.s30, state.s30);
        
        //======================================================================
        // RAYS
        MaestroG.drawLineThick(g, myw+state.s9+xforw, (double)(myh)+state.s17, 
                (double)(myw)+xforw+xcenter, (double)(myh)+ycenter, 1, bluette);
        MaestroG.drawLineThick(g, myw+state.s9+xforw, (double)(myh)+state.s80, 
                (double)(myw)+xforw+xcenter, (double)(myh)+ycenter, 1, bluette);
        
        MaestroG.drawLineThick(g, myw+state.s5+xforw, (double)(myh)+state.s27, 
                (double)(myw)+xforw+xcenter, (double)(myh)+ycenter, 1, bluette);
        MaestroG.drawLineThick(g, myw+state.s5+xforw, (double)(myh)+state.s70, 
                (double)(myw)+xforw+xcenter, (double)(myh)+ycenter, 1, bluette);
        
        MaestroG.drawLineThick(g, myw+state.s3+xforw, (double)(myh)+state.s37, 
                (double)(myw)+xforw+xcenter, (double)(myh)+ycenter, 1, bluette);
        MaestroG.drawLineThick(g, myw+state.s3+xforw, (double)(myh)+state.s60, 
                (double)(myw)+xforw+xcenter, (double)(myh)+ycenter, 1, bluette);
        
        MaestroG.drawLineThick(g, myw+state.s9+xforw, (double)(myh)+state.s17, 
                (double)(myw)+state.s120+xforw, (double)(myh)+state.s17, 1, bluette);
        MaestroG.drawLineThick(g, myw+state.s9+xforw, (double)(myh)+state.s80, 
                (double)(myw)+state.s120+xforw, (double)(myh)+state.s80, 1, bluette);
        
        MaestroG.drawLineThick(g, myw+state.s5+xforw, (double)(myh)+state.s27, 
                (double)(myw)+state.s120+xforw, (double)(myh)+state.s27, 1, bluette);
        MaestroG.drawLineThick(g, myw+state.s5+xforw, (double)(myh)+state.s70, 
                (double)(myw)+state.s120+xforw, (double)(myh)+state.s70, 1, bluette);
        MaestroG.drawLineThick(g, myw+state.s3+xforw, (double)(myh)+state.s37, 
                (double)(myw)+state.s120+xforw, (double)(myh)+state.s37, 1, bluette);
        MaestroG.drawLineThick(g, myw+state.s3+xforw, (double)(myh)+state.s60, 
                (double)(myw)+state.s120+xforw, (double)(myh)+state.s60, 1, bluette);
        
        g.setColor(bluette);
        MaestroG.drawArrowScaled((myw)+state.s120+xforw, (myh)+state.s17, 3, state.sfactor, g);
        MaestroG.drawArrowScaled((myw)+state.s120+xforw, (myh)+state.s27, 3, state.sfactor, g);
        MaestroG.drawArrowScaled((myw)+state.s120+xforw, (myh)+state.s37, 3, state.sfactor, g);
        MaestroG.drawArrowScaled((myw)+state.s120+xforw, (myh)+state.s60, 3, state.sfactor, g);
        MaestroG.drawArrowScaled((myw)+state.s120+xforw, (myh)+state.s70, 3, state.sfactor, g);
        MaestroG.drawArrowScaled((myw)+state.s120+xforw, (myh)+state.s80, 3, state.sfactor, g);
        
        //======================================================================
                
        drawCopyRight(g);
    }

    public void drawCopyRight(Graphics g) {
        int deltaY = state.s25;
        int startingY = getSize().height - state.s90;
        int startingX = 0;
        
        deltaY = state.s20;
        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 + deltaY/2);
        
        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+ deltaY/2);
        int lineLength = fm.stringWidth("Interactive JavaTM platform:  www.amanogawa.com");
        lineLength += state.s10;

        g.drawString("All Rights Reserved",startingX + state.s15,startingY + 3*deltaY+ deltaY/2);
        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+ deltaY/2);
    }
//----------------------------------------------------------------------------------------    
}//End
