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;
    private RectWaveGuide_State state;
    
    private BufferedImage bullet;

    public Instructions(RectWaveGuide_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.s400;
        int buttony = state.s400;
        
        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);

        //boolean saveOldFlag = MaestroG.useBiggerFont;
        //MaestroG.useBiggerFont = false;
        
        g.setColor(Color.black);
        g.setFont(new Font("SanSerif",Font.BOLD,state.font16));
        int vpos = state.s25;
        int hpos = state.s15;
        int deltaY = state.s25;
        int deltaY2 = state.s20;
        int deltaY3 = state.s40;
        
        g.drawString("Instructions", hpos,vpos);
        
        vpos+=deltaY;
        
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        g.drawString("This module provides information about TE and "+
                    "TM modes in a metal",hpos,vpos);
                    
        vpos+=deltaY2;
        g.drawString("rectangular waveguide.",hpos,vpos);
        
        vpos+=deltaY;
        
        g.setColor(Color.red);
        g.setFont(new Font("SanSerif",Font.BOLD,state.font15));

        g.drawString("Input", hpos,vpos);
        g.setColor(Color.black);
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        vpos+=deltaY;
        //INPUT
        //String s = "\u2022 Waveguide dimensions a and b";
        //int nextX = STR.displayString(s,g,hpos,vpos);
	g.drawImage(bullet,hpos,vpos-bullet.getHeight(),this);
        String s = " Waveguide dimensions a and b";
        int nextX = STR.displayString(s,g,hpos+bullet.getWidth(),vpos);

        vpos+=deltaY;
        //s = "\u2022 TE or TM mode";
	//next = STR.displayString(s,g,hpos,vpos);
	g.drawImage(bullet,hpos,vpos-bullet.getHeight(),this);
        s = " TE or TM mode";
        nextX = STR.displayString(s,g,hpos+bullet.getWidth(),vpos);
	
        vpos+=deltaY;
        //s = "\u2022 frequency  f";
        //nextX = STR.displayString(s,g,state.s15,vpos);
	g.drawImage(bullet,hpos,vpos-bullet.getHeight(),this);
        s = " frequency  f";
        nextX = STR.displayString(s,g,hpos+bullet.getWidth(),vpos);
	
        vpos+=deltaY;
        //s = "\u2022 \u03b5"+STR.SUB+"r"+STR.ENDSUB+" of dielectric material";
        //nextX = STR.displayString(s,g,state.s15,vpos);
	g.drawImage(bullet,hpos,vpos-bullet.getHeight(),this);
        s = " \u03b5"+STR.SUB+"r"+STR.ENDSUB+" of dielectric material";
        nextX = STR.displayString(s,g,hpos+bullet.getWidth(),vpos);
	
       //OUTPUT
        g.setColor(Color.red);
        g.setFont(new Font("SanSerif",Font.BOLD,state.font15));
        
        vpos+=deltaY;
        g.drawString("Displayed Information", hpos,vpos);
        g.setColor(Color.black);
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        vpos+=deltaY;
        //s = "\u2022 Cutoff frequencies of TE and TM modes";
        //nextX = STR.displayString(s,g,hpos,vpos);
	g.drawImage(bullet,hpos,vpos-bullet.getHeight(),this);
        s = " Cutoff frequencies of TE and TM modes";
        nextX = STR.displayString(s,g,hpos+bullet.getWidth(),vpos);
	
        vpos+=deltaY;
        //s = "\u2022 Spatial distribution of "+STR.BOLD+"E"+STR.ENDBOLD+" for "+
        //    "TE modes and "+STR.BOLD+"H"+STR.ENDBOLD+" for TM modes";
        //nextX = STR.displayString(s,g,hpos,vpos);
	g.drawImage(bullet,hpos,vpos-bullet.getHeight(),this);
        s = " Spatial distribution of "+STR.BOLD+"E"+STR.ENDBOLD+" for "+
            "TE modes and "+STR.BOLD+"H"+STR.ENDBOLD+" for TM modes";
        nextX = STR.displayString(s,g,hpos+bullet.getWidth(),vpos);
	
        vpos+=deltaY;
        //s = "\u2022 Phase and group velocities";
        //nextX = STR.displayString(s,g,hpos,vpos);
	g.drawImage(bullet,hpos,vpos-bullet.getHeight(),this);
        s = " Phase and group velocities";
        nextX = STR.displayString(s,g,hpos+bullet.getWidth(),vpos);
        
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
        vpos+=deltaY+10;
        s = "The Mode Spectrum panel only displays cutoff frequencies of lower order modes.";
        nextX = STR.displayString(s,g,hpos,vpos);
        
        vpos+=deltaY2;
        s = "Use Mode Selector to scan higher order modes in the Mode Properties panel.";
        nextX = STR.displayString(s,g,hpos,vpos);
        vpos+=deltaY2;
        s = "";
        nextX = STR.displayString(s,g,hpos,vpos);
        
        drawCopyRight(g);
        
        //MaestroG.subscripter("Higher order modes are listed in the Mode Properties panel ( use Mode Selector to scan modes )","","",g,10,20, getSize().height-10);
        //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.s12));
        fm = g.getFontMetrics();
        
        g.drawString("  Application Design: Umberto Ravaioli",
                     startingX,startingY+deltaY);
        
        String s = "  Interactive Java"+STR.SUP+"TM"+STR.ENDSUP+
            " platform:  www.amanogawa.com";
        int nextX = STR.displayString(s,g,startingX,startingY+2*deltaY);
        int lineLength = fm.stringWidth("Interactive JavaTM platform:  www.amanogawa.com");
        lineLength+= state.s10;

        g.drawString("  All Rights Reserved",startingX,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
