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 = Color.white;
    public TextField epsilon;
    private Paint paint;
    private Image im;
    private Graphics buf;
    public Button bupdate;
    Trans_State state;
    
    private BufferedImage bullet;
    
    public Instructions(Trans_State state){
	super();
        this.state = state;
	setLayout(null);
	
	getImages();
        setBackground(bgcolor);
	bupdate = new Button("CLOSE");
        bupdate.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
        bupdate.setBackground(new Color(255,255,255));
	add(bupdate);
        int buttonx = state.s500;
        int buttony = state.s400+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);

        int startx = state.s20; 
        int starty = state.s90;
        
        g.setColor(Color.black);
        //g.drawRect(0,0,getSize().width-1,getSize().height-1);
        g.setFont(new Font("SanSerif",Font.BOLD,state.font18));
        g.drawString("Instructions", startx,state.s40);
        int nextX;
        
        g.setColor(Color.red);
        g.setFont(new Font("SanSerif",Font.BOLD,state.font15));

        g.drawString("Input", startx,starty);
        g.setColor(Color.black);
        
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        fm = g.getFontMetrics();
        int deltaY = state.s25;
        
        //INPUT
        //String s = "\u2022 Plot Range: The spatial dimension (in units of \u03bb)"+
        //    " of the display plane";
        //nextX = STR.displayString(s,g,startx,starty+2*deltaY);
	g.drawImage(bullet,startx,starty+2*deltaY-bullet.getHeight(),this);
        String s = " Plot Range: The spatial dimension (in units of \u03bb)"+
            " of the display plane";
        nextX = STR.displayString(s,g,startx+bullet.getWidth(),starty+2*deltaY);
	
        //s = "\u2022 Choice of Horizontal (x-y) or Vertical Plane";
        //nextX = STR.displayString(s,g,startx,starty+3*deltaY);
	g.drawImage(bullet,startx,starty+3*deltaY-bullet.getHeight(),this);
	s = " Choice of Horizontal (x-y) or Vertical Plane";
	nextX = STR.displayString(s,g,startx+bullet.getWidth(),starty+3*deltaY);
	
        //s = "\u2022 Choice of E"+STR.SUB+"\u03b8"+STR.ENDSUB+" (exact "+
        //    "solution) or E"+STR.SUB+"\u03b8FF"+STR.ENDSUB+
        //    " (far-field approximation)";
        //nextX = STR.displayString(s,g,startx,starty+4*deltaY);
	g.drawImage(bullet,startx,starty+4*deltaY-bullet.getHeight(),this);
	s = " Choice of E"+STR.SUB+"\u03b8"+STR.ENDSUB+" (exact "+
            "solution) or E"+STR.SUB+"\u03b8FF"+STR.ENDSUB+
            " (far-field approximation)";
        nextX = STR.displayString(s,g,startx+bullet.getWidth(),starty+4*deltaY);
        
        //OUTPUT
        g.setColor(Color.red);
        g.setFont(new Font("SanSerif",Font.BOLD,state.font15));
        int newY = starty+7*deltaY;
        g.drawString("Displayed Information", startx,newY);
        
        g.setColor(Color.black);
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        newY += state.s40;
        //s = "\u2022 2-D patterns of E\u03b8"+STR.SUB+"\u03b8"+STR.ENDSUB+
        //    " and H"+STR.SUB+"\u03c6"+STR.ENDSUB+", in the Horizontal"+
        //    " and Vertical planes";
        //nextX = STR.displayString(s,g,startx,newY);
	g.drawImage(bullet,startx,newY-bullet.getHeight(),this);
        s = " 2-D patterns of E"+STR.SUB+"\u03b8"+STR.ENDSUB+
            " and H"+STR.SUB+"\u03c6"+STR.ENDSUB+", in the Horizontal"+
            " and Vertical planes";
        nextX = STR.displayString(s,g,startx+bullet.getWidth(),newY);
	
        //s = "\u2022 Dynamic simulation of how E"+STR.SUB+"\u03b8"+STR.ENDSUB+
        //    ", H"+STR.SUB+"\u03c6"+STR.ENDSUB+", and the current I(t), "+
        //    "vary with time";
        //nextX = STR.displayString(s,g,startx,newY+deltaY);
	g.drawImage(bullet,startx,newY+deltaY-bullet.getHeight(),this);
        s = " Dynamic simulation of how E"+STR.SUB+"\u03b8"+STR.ENDSUB+
            ", H"+STR.SUB+"\u03c6"+STR.ENDSUB+", and the current I(t), "+
            "vary with time";
        nextX = STR.displayString(s,g,startx+bullet.getWidth(),newY+deltaY);
        
        drawCopyRight(g);
    }


    public void drawCopyRight(Graphics g) {
        int deltaY = state.s20;
        int startingY = getSize().height - state.s75;
        int startingX = 0;
        FontMetrics fm;
        g.setColor(Color.black);
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font13));
        fm = g.getFontMetrics();
        g.drawString("Applet 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+state.s15+lineLength+state.s10,startingY+state.s5);
        g.drawLine(startingX+state.s15+lineLength+state.s10,startingY+state.s5,
                   startingX+state.s15+lineLength+state.s10,startingY+4*deltaY);
    }
//----------------------------------------------------------------------------------------    
}//End
    

