//Mod1Input.java
/*
 * Panel with all input operations for Module1.1
 * Using null layout - positions of elements hard-coded
 * authors: Umberto Ravaioli, Janice Richards
 * version 1.0 - Copyright: Amanogawa.com - All Rights Reserved
 */

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Mod3Input extends Panel implements ActionListener {
    private Mod3State state;
    //private static final Color bgcolor = new Color(236,236,236);
    private static final Color bgcolor = Color.white;
    private Label titlelabel;
    private String sq1, sq2, s1a, s1b, s1c, s1d, s2a, s2b, s2c, s2d;
    private int endingX;
    private Button button1Green, button1Blue, button1Purple;
    private Button button2Green, button2Blue, button2Purple;
    private BufferedImage x, check, blank;
    private boolean anyGreen1Answer = false;
    private boolean anyBlue1Answer = false;
    private boolean anyPurple1Answer = false;
    private boolean anyGreen2Answer = false;
    private boolean anyBlue2Answer = false;
    private boolean anyPurple2Answer = false;
    private int y1, y2, y3, y4, y5, y6;
  
    public Mod3Input(Mod3State state){
	super();
	setLayout(null);
	setBackground(bgcolor);
	this.state = state;
	titlelabel = new Label("Questions",Label.CENTER);
	//titlelabel.setFont(state.subtitleFont);
        //titlelabel.setFont(state.ttfFont.deriveFont(Font.BOLD,15f));
        //titlelabel.setFont(state.ttfFont.deriveFont(Font.BOLD,16f));
        titlelabel.setFont(new Font("SanSerif",Font.BOLD,state.font16));

        getImages();
        getTextStuff();
        getButtons();
		
	add(titlelabel);
	add(button1Green);
	add(button1Blue);
	add(button1Purple);
	add(button2Green);
	add(button2Blue);
	add(button2Purple);
        
	//titlelabel.setBounds(20,5,80,18);
        titlelabel.setBounds(state.s20,state.s5,state.s100,state.s18);
	//int vPosInit = 25, vPos, dvPos = 2, vHeight = 18; 
	int vPosInit = state.s47, vPos, dvPos = state.s4, vHeight = state.s20; 
	//vPos = vPosInit + 20;
	vPos = vPosInit + state.s40;
        y1 = vPos;
	
	button1Green.setBounds(state.s20,vPos,state.s100,vHeight);	
	    vPos += vHeight + dvPos + state.s2; 
            y2 = vPos;
	button1Blue.setBounds(state.s20,y2,state.s100,vHeight);
	    vPos += vHeight + dvPos + state.s2; 
            y3 = vPos;
	button1Purple.setBounds(state.s20,y3,state.s100,vHeight);
            y4 = y1;
            vPos = y1;
        
	button2Green.setBounds(state.s420,y1,state.s100,vHeight);
	    vPos += vHeight + dvPos + state.s2; 
            y5 = vPos;
	button2Blue.setBounds(state.s420,y5,state.s100,vHeight);
	    vPos += vHeight + dvPos + state.s2; 
            y6 = vPos;
	button2Purple.setBounds(state.s420,y6,state.s100,vHeight);
        
        Panel pbutton1 = new Panel();
        pbutton1.setBackground(Color.black);
        pbutton1.setBounds(state.s20-1,y1-1,state.s100+2,vHeight+2);
        add(pbutton1);
        
        Panel pbutton2 = new Panel();
        pbutton2.setBackground(Color.black);
        pbutton2.setBounds(state.s20-1,y2-1,state.s100+2,vHeight+2);
        add(pbutton2);
        
        Panel pbutton3 = new Panel();
        pbutton3.setBackground(Color.black);
        pbutton3.setBounds(state.s20-1,y3-1,state.s100+2,vHeight+2);
        add(pbutton3);
        
        Panel pbutton4 = new Panel();
        pbutton4.setBackground(Color.black);
        pbutton4.setBounds(state.s420-1,y4-1,state.s100+2,vHeight+2);
        add(pbutton4);
        
        Panel pbutton5 = new Panel();
        pbutton5.setBackground(Color.black);
        pbutton5.setBounds(state.s420-1,y5-1,state.s100+2,vHeight+2);
        add(pbutton5);
        
        Panel pbutton6 = new Panel();
        pbutton6.setBackground(Color.black);
        pbutton6.setBounds(state.s420-1,y6-1,state.s100+2,vHeight+2);
        add(pbutton6);
        
		    
        // Surround for panel
	Panel ps9 = new Panel();
            //ps9.setBackground(Color.white);
            //add(ps9);
            //ps9.setBounds(21,vPos+1,198,vHeight+2);
	    
	Panel ps10 = new Panel();
            //ps10.setBackground(Color.black);
            //add(ps10);
            //ps10.setBounds(20,vPos,200,vHeight+4);
	
	//Listeners
        
            button1Green.addActionListener(this);
            button1Blue.addActionListener(this);
            button1Purple.addActionListener(this);
            button2Green.addActionListener(this);
            button2Blue.addActionListener(this);
            button2Purple.addActionListener(this);
    }

    private void getImages() {
        // read in the x, check, and blank images.
        try {
            blank = ImageIO.read(getClass().getResource("blank.gif"));
            check = ImageIO.read(getClass().getResource("check.gif"));
            x = ImageIO.read(getClass().getResource("x.gif"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    private void getTextStuff() {
        sq1 = "Q1.  Which color wave "+STR.REDBOLD+"leads"+STR.ENDREDBOLD+
            " the "+
            STR.REDBOLD+"red"+STR.ENDREDBOLD+" wave by a *break*phase angle "+
            "of "+Character.toString((char)0x03c0)+"/4 ?";

        sq2 = "Q2.  Which color wave "+STR.REDBOLD+"lags"+STR.ENDREDBOLD+
            " the "+
            STR.REDBOLD+"red"+STR.ENDREDBOLD+" wave by a *break*phase angle "+
            "of "+Character.toString((char)0x03c0)+"/2 ?";

        s1a = "because green wave reaches its";

        s1b = "peak 1s before red wave does;";

        s1c = "wave period = 8s, so 1s";

        s1d = "is equivalent to 2"+
            Character.toString((char)0x03c0)+"/8 = "+
            Character.toString((char)0x03c0)+"/4 rad.";

        s2a = "because blue wave reaches its";

        s2b = "peak 2s after red wave does;";

        s2c = "wave period = 8s, so 2s is";

        s2d = "equivalent to 2"+
            Character.toString((char)0x00d7)+"2"+
            Character.toString((char)0x03c0)+"/8 = "+
            Character.toString((char)0x03c0)+"/2 rad.";
    }


    private void getButtons() {
        button1Green = new Button("Green Wave");
        //button1Green.setFont(TheFonts.sanSerif12);
        //button1Green.setFont(TheFonts.bold12);
        button1Green.setFont(new Font("SanSerif",Font.BOLD,state.font12));
        button1Green.setForeground(state.yList2Color);
        
        button1Blue = new Button("Blue Wave");
        //button1Blue.setFont(TheFonts.sanSerif12);
        //button1Blue.setFont(TheFonts.bold12);
        button1Blue.setFont(new Font("SanSerif",Font.BOLD,state.font12));
        button1Blue.setForeground(state.yList3Color);
        
        button1Purple = new Button("Purple Wave");
        //button1Purple.setFont(TheFonts.sanSerif12);
        //button1Purple.setFont(TheFonts.bold12);
        button1Purple.setFont(new Font("SanSerif",Font.BOLD,state.font12));
        button1Purple.setForeground(state.yList4Color);

        button2Green = new Button("Green Wave");
        //button2Green.setFont(TheFonts.sanSerif12);
        //button2Green.setFont(TheFonts.bold12);
        button2Green.setFont(new Font("SanSerif",Font.BOLD,state.font12));
        button2Green.setForeground(state.yList2Color);
        
        button2Blue = new Button("Blue Wave");
        //button2Blue.setFont(TheFonts.sanSerif12);
        //button2Blue.setFont(TheFonts.bold12);
        button2Blue.setFont(new Font("SanSerif",Font.BOLD,state.font12));
        button2Blue.setForeground(state.yList3Color);
        
        button2Purple = new Button("Purple Wave");
        //button2Purple.setFont(TheFonts.sanSerif12);
        //button2Purple.setFont(TheFonts.bold12);
        button2Purple.setFont(new Font("SanSerif",Font.BOLD,state.font12));
        button2Purple.setForeground(state.yList4Color);
    }


    public void actionPerformed(ActionEvent evt){
	if(evt.getSource()==button1Green) {
            anyGreen1Answer = true;
        } else if (evt.getSource()==button1Blue) {
            anyBlue1Answer = true;
        } else if (evt.getSource()==button1Purple) {
            anyPurple1Answer = true;
        } else if (evt.getSource()==button2Green) {
            anyGreen2Answer = true;
        } else if (evt.getSource()==button2Blue) {
            anyBlue2Answer = true;
        } else if (evt.getSource()==button2Purple) {
            anyPurple2Answer = true;
        }
        repaint();
    }

    
    public void paint(Graphics g){
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);
	
	//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.setFont(state.ttfFont.deriveFont(14f));
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        
        g.setColor(Color.black);
        //endingX = STR.displayString(sq1,g,state.s140,state.s60);
        int index = sq1.indexOf("*break*");
        if (index == -1)
            endingX = STR.displayString(sq1,g,state.s20,state.s50);
        else {
            endingX = STR.displayString(sq1.substring(0,index),g,state.s20,state.s50);
            endingX = STR.displayString(sq1.substring(index+7),g,state.s30,state.s70);
        }

        //endingX = STR.displayString(sq2,g,state.s140,state.s190);
        index = sq2.indexOf("*break*");
        if (index == -1)
            endingX = STR.displayString(sq2,g,state.s420,state.s50);
        else {
            endingX = STR.displayString(sq2.substring(0,index),g,state.s420,state.s50);
            endingX = STR.displayString(sq2.substring(index+7),g,state.s430,state.s70);
        }

        if (!anyGreen1Answer)
            g.drawImage(blank,state.s5,y1,this);
        else {
            g.drawImage(check,state.s5,y1+state.s5,this);
            endingX = STR.displayString(s1a,g,state.s140,y1+state.s10);
            endingX = STR.displayString(s1b,g,state.s140,y1+state.s30);
            endingX = STR.displayString(s1c,g,state.s140,y1+state.s50);
            endingX = STR.displayString(s1d,g,state.s140,y1+state.s70);
        }
        if (!anyBlue1Answer)
            g.drawImage(blank,state.s5,y2,this);
        else 
            g.drawImage(x,state.s5,y2+state.s5,this);
        if (!anyPurple1Answer)
            g.drawImage(blank,state.s5,y3,this);
        else 
            g.drawImage(x,state.s5,y3+state.s5,this);
        
        if (!anyGreen2Answer)
            g.drawImage(blank,state.s405,y4,this);
        else 
            g.drawImage(x,state.s405,y4+state.s5,this);
        if (!anyBlue2Answer)
            g.drawImage(blank,state.s405,y5,this);
        else {
            g.drawImage(check,state.s405,y5+state.s5,this);
            //endingX = STR.displayString(s2a,g,140,y5+10);
            //endingX = STR.displayString(s2b,g,140,y5+30);
            endingX = STR.displayString(s2a,g,state.s540,y5+state.s10);
            endingX = STR.displayString(s2b,g,state.s540,y5+state.s30);
            endingX = STR.displayString(s2c,g,state.s540,y5+state.s50);
            endingX = STR.displayString(s2d,g,state.s540,y5+state.s70);
        }
        if (!anyPurple2Answer)
            g.drawImage(blank,state.s405,y6,this);
        else 
            g.drawImage(x,state.s405,y6+state.s5,this);
    }
    /*
    public static void main(String[] args) {
        
        Frame f = new Frame("Test 2 panels ... and choice");
        
        f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        
        //f.add("Center",  new Module1_1());
        //f.setSize(new Dimension(700, 600));
        //f.setVisible(true);
        f.setLayout(new GridLayout(1,0));
        f.setSize(700, 600);
        f.setVisible(true);
        Mod3State state = new Mod3State();
        Mod3Input myApplet = new Mod3Input(state);
        f.add(myApplet);
        f.pack();
    }
    */

    
}
