import java.awt.*;
import java.text.*;

// Chapter 3 Module 1
public class Mod1Info extends Panel {
    private static final Color bgcolor = new Color(236,236,236);
    //private static final Color bgcolor = Color.white;

    private Label titlelabel;
    Mod1State state;
    String s1 = null;
    String s2 = null;
    int endingX;
    DecimalFormat dec = new DecimalFormat("#.###");
    DecimalFormat decTheta = new DecimalFormat("###.###");

    static double piOver180 = Math.PI/180.0;

    public NewOutputCanvas1 outInfo;

    public Mod1Info(Mod1State state) {
        super();
        setBackground(bgcolor);
        setLayout(null);
        this.state = state;
	titlelabel = new Label("Output",Label.CENTER);
	//titlelabel.setFont(TheFonts.bold14);
	//titlelabel.setFont(TheFonts.bold16);
        titlelabel.setFont(new Font("SanSerif",Font.BOLD,state.font16));
        
	add(titlelabel);
	titlelabel.setBounds(state.s15,state.s5,state.s70,state.s18);
        titlelabel.setBackground(bgcolor);
        
        outInfo = new NewOutputCanvas1(state);
        add(outInfo);
        // total width = 285, height = 257

        ////infoPanel.setBounds(517,469,450,128);
        //infoPanel.setBounds(517,527,450,70); // 469+58 = 527;  128 - 58 = 70

        //outInfo.setBounds(0,24,285,233);
        //outInfo.setBounds(4,24,285,100);
        outInfo.setBounds(state.s4,state.s24,state.s285,state.s35);
    }
    
    public void displayOutput() {
        double x=0,y=0,z=0,theta=0;
        boolean xySet = false;
        boolean zThetaSet = false;
        if (state.outputAplusB) {
            if (state.outputC_XandY) {
                x = state.xValA + state.xValB;
                y = state.yValA + state.yValB;
                xySet = true;
            } else { // output z and theta
                x = state.xValA + state.xValB;
                y = state.yValA + state.yValB;
                z = Math.sqrt((1.0*x*x)+(y*y));
                theta = Math.atan2(y,x)/piOver180;
                zThetaSet = true;
            }
        } else if (state.outputAminusB) {
            if (state.outputC_XandY) {
                x = state.xValA - state.xValB;
                y = state.yValA - state.yValB;
                xySet = true;
            } else { // output z and theta
                x = state.xValA - state.xValB;
                y = state.yValA - state.yValB;
                z = Math.sqrt((1.0*x*x)+(y*y));
                theta = Math.atan2(y,x)/piOver180;
                zThetaSet = true;
            }
        }

        if (xySet) {
            state.sOutput1 = "Vector "+STR.GREEN+"C    "+STR.ENDGREEN;
            state.sOutput2 = STR.GREEN+"x"+STR.ENDGREEN+
                " = "+dec.format(x)+STR.GREEN+"    y"+STR.ENDGREEN+
                " = "+dec.format(y);
        } else if (zThetaSet) {
            state.sOutput1 = "Vector "+STR.GREEN+"C    "+STR.ENDGREEN;
            state.sOutput2 = STR.GREEN+"|z|"+STR.ENDGREEN+
                    " = "+dec.format(z)+"    "+STR.GREEN+
                    Character.toString((char)0x03b8)+STR.ENDGREEN+" = "+
                    decTheta.format(theta)+"\u00b0";
        } else {
            state.sOutput1 = "            ";
            state.sOutput2 = "                      ";
        }
            
        //if (state.outputXandY) {
        //    state.s1 = STR.GREEN+"x = "+ dec.format(state.xVal)+STR.ENDGREEN;
        //    state.s2 = STR.GREEN+"y = "+ dec.format(state.yVal)+STR.ENDGREEN;
        //} else if (state.outputZandTheta) {
        //    state.s1 = STR.RED+"|z| = "+dec.format(state.zVal)+STR.ENDRED;
        //    state.s2 = STR.BLUE+Character.toString((char)0x03b8)+" = "+
        //        decTheta.format(state.thetaVal)+"\u00b0"+STR.ENDBLUE;
        //}
        outInfo.repaint();
    }
    
    	
    public void paint(Graphics g){
	
	g.setColor(bgcolor.darker());
	//g.fillRect(0,getSize().height-2,getSize().width,2);
	//g.fillRect(getSize().width-2,0,2,getSize().height);
	//g.setColor(bgcolor.brighter());
	//g.fillRect(0,0,2,getSize().height-1);
	//g.fillRect(0,0,getSize().width-2,2);
    }
}

class NewOutputCanvas1 extends Canvas{
    private static  Font normalfont, boldfont;
    private static  Font subfont;
    Mod1State state;
    private int endingX;
    private Image im;
    private Graphics buf;
    public NewOutputCanvas1(Mod1State state){
        super();
        this.state = state;
        //boldfont = state.sanSerifFont.deriveFont(Font.BOLD,12f);
        //normalfont = state.sanSerifFont.deriveFont(12f);
        //subfont = state.sanSerifFont.deriveFont(10f);
        
        boldfont = new Font("SanSerif",Font.BOLD,state.font14);
        normalfont = new Font("SanSerif",Font.PLAIN,state.font14);
        subfont = new Font("SanSerif",Font.PLAIN,state.font12);
    }
    
    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){
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        //g.setFont(state.ttfFont.deriveFont(14f));

        Color bgcolor = new Color(236,236,236);
       
        g.setColor(bgcolor);
        //g.fillRect(0,0,getSize().width,getSize().height);
        g.fillRect(0,0,getSize().width-6,getSize().height-6);


        /*
	g.setColor(bgcolor.darker());
	g.fillRect(0,getSize().height-2,getSize().width,2);
	g.fillRect(getSize().width-2,0,2,getSize().height);
	g.setColor(bgcolor.brighter());
	g.fillRect(0,0,2,getSize().height-1);
	g.fillRect(0,0,getSize().width-2,2);
        */


        g.setColor(Color.black);
        //if (state.s1 != null) endingX = STR.displayString(state.s1,g,40,80);
        //if (state.s2 != null) endingX = STR.displayString(state.s2,g,40,110);
        //if (state.s1 != null) endingX = STR.displayString(state.s1,g,40,56);
        //if (state.s2 != null) endingX = STR.displayString(state.s2,g,40,86);
        if (state.sOutput1 != null) {
            //g.setFont(TheFonts.bold14);
            g.setFont(boldfont);
            //endingX = STR.displayString(state.sOutput1,g,20,40);
            //endingX = STR.displayString(state.sOutput1,g,16,40);
            endingX = STR.displayString(state.sOutput1,g,state.s16,state.s20);
            if (state.sOutput2 != null) {
                //g.setFont(TheFonts.sanSerif14);
                g.setFont(boldfont);
                
                //int junk = STR.displayString(state.sOutput2,g,endingX,40);
                //int junk = STR.displayString(state.sOutput2,g,endingX,40);
                int junk = STR.displayString(state.sOutput2,g,endingX,state.s20);
            }
        }
    }
}

