import java.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.net.URL;
import java.text.*;
import java.util.Map;
import java.util.Hashtable;
import java.util.jar.Attributes;
//import javax.swing.*;

// Module 1 Exercises 2
public class Mod2 extends Frame implements AdjustmentListener,
                                           MouseListener,
                                           ActionListener,
                                           WindowListener {
    TitlePanel titlePanel;
    Mod2Input inputPanel;
    Mod2State state;
    Mod2Plot plotPanel;
    Mod2Info infoPanel;

    Font ttfFont, phiFont, italicFont;

    private static final boolean useOldFonts = true;    


  public void start() {
  }

  public void stop(){}

  public void destroy(){}

    
    public static void main(String[] args){
        Mod2 f = new Mod2();
        int xmove = 20;
        int ymove = 41;
        f.setSize(897+xmove,620+ymove);
        f.setVisible(true);
        f.setLayout(null);
    }

  public Mod2() { 
    setLayout(null);    
    String lcOSName = System.getProperty("os.name").toLowerCase();
    boolean MAC_OS_X = lcOSName.startsWith("mac os x");
    boolean PC_OS = lcOSName.startsWith("windows");
    //System.out.println(lcOSName);
    TheFonts.setMAC_OS_X(MAC_OS_X);
    TheFonts.setPC_OS(PC_OS);
  
    if (!useOldFonts) {
        //getUlabyFonts();
        state = new Mod2State();
        state.ttfFont = ttfFont;
        state.sanSerifFont = ttfFont;
        state.serifFont = ttfFont;
        state.symbolFont = phiFont;
        state.italicFont = phiFont;
        TheFonts.setNewFonts(ttfFont, phiFont, italicFont);
    } else {
        state = new Mod2State();
        state.ttfFont = new Font("SanSerif",Font.PLAIN,12);
        state.sanSerifFont = new Font("SanSerif",Font.PLAIN,12);
        state.serifFont = new Font("Serif",Font.PLAIN,12);
        state.symbolFont = new Font("Symbol",Font.PLAIN,12);
        state.italicFont = new Font("Serif",Font.ITALIC,12);
        TheFonts.setOldFonts();
    }
    
    int xmove = 10;
    int ymove = 34;

    //titlePanel = new TitlePanel(subtitleFont,"Module 1.2","Traveling Waves");
    titlePanel = new TitlePanel(state.ttfFont.deriveFont(Font.BOLD,15f),
                                "Module 1.2","Traveling Waves");
    titlePanel.setBounds(10+xmove,10+ymove,569,40);
    Panel blankTitle = new Panel();
    blankTitle.setBounds(586+xmove,10+ymove,299,40);
    blankTitle.setBackground(Color.lightGray);
	
    inputPanel = new Mod2Input(state);
    inputPanel.setBounds(588+xmove,59+ymove,297,199);

    plotPanel = new Mod2Plot(state);
    plotPanel.setVisible(true);
    plotPanel.setBounds(12+xmove,267+ymove,874,337);

    infoPanel = new Mod2Info(state);
    infoPanel.setBounds(12+xmove,59+ymove,565,199);

    // backpanels for input, plot and info
    ////inputPanel.setBounds(588,59,297,199);
    Panel p_i_w = new Panel();
    p_i_w.setBackground(Color.white);
    p_i_w.setBounds(587+xmove,58+ymove,299,201);
    Panel p_i_b = new Panel();
    p_i_b.setBackground(Color.black);
    p_i_b.setBounds(586+xmove,57+ymove,301,203);
    ////plotPanel.setBounds(12,267,874,337);
    Panel p_p_w = new Panel();
    p_p_w.setBackground(Color.white);
    p_p_w.setBounds(11+xmove,266+ymove,876,339);
    Panel p_p_b = new Panel();
    p_p_b.setBackground(Color.black);
    p_p_b.setBounds(10+xmove,265+ymove,878,341);
    //// infoPanel.setBounds(12,59,565,199);
    Panel p_inf_w = new Panel();
    p_inf_w.setBackground(Color.white);
    p_inf_w.setBounds(11+xmove,58+ymove,567,201);
    Panel p_inf_b = new Panel();
    p_inf_b.setBackground(Color.black);
    p_inf_b.setBounds(10+xmove,57+ymove,569,203);

    //backpanels for applet
    Panel p0 = new Panel();
    p0.setBackground(Color.cyan);
    p0.setBounds(3+xmove,3+ymove,891,610);
	    
    Panel p00 = new Panel();
    p00.setBackground(Color.black);
    p00.setBounds(xmove,ymove,897,616);

            
    add(titlePanel);
    add(blankTitle);
    add(inputPanel);
    add(plotPanel);
    add(infoPanel);

    add(p_i_w);
    add(p_i_b);
    add(p_p_w);
    add(p_p_b);
    add(p_inf_w);
    add(p_inf_b);

    add(p0);
    add(p00);

    this.addWindowListener(this);
    inputPanel.slider1.addAdjustmentListener(this);
    inputPanel.slider2.addAdjustmentListener(this);
    inputPanel.slider3.addAdjustmentListener(this);
    inputPanel.text1.addActionListener(this);
    inputPanel.text2.addActionListener(this);
    inputPanel.text3.addActionListener(this);
    plotPanel.ssc.button1.addMouseListener(this);
    plotPanel.ssc.button2.addMouseListener(this);
    plotPanel.reset.addActionListener(this);
    plotPanel.initialPlot();

  }

/*
    private void getUlabyFonts() {
        String path1 = getCodeBase().getPath() + "times.ttf";
        String path2 = getCodeBase().getPath() + "palai.ttf";
        String path3 = getCodeBase().getPath() + "timesi.ttf";

        try {

            // SWAP COMMENT TO OTHER fi= STATEMENT WHEN FINAL VERSION OK
            //  TO RUN ON THE BROWSER (use the other one when debugging ...)

            //File fi = new File ("/home/jar/newcd/fonts/times.ttf");
            File fi = new File (path1);
            FileInputStream in = new FileInputStream (fi);
            ttfFont = Font.createFont (Font.TRUETYPE_FONT, in);

            //fi = new File ("/home/jar/newcd/fonts/palai.ttf");
            fi = new File (path2);
            in = new FileInputStream (fi);
            phiFont = Font.createFont (Font.TRUETYPE_FONT, in);
            phiFont = phiFont.deriveFont (14f);  // SAVE

            fi = new File (path3);
            in = new FileInputStream (fi);
            italicFont = Font.createFont (Font.TRUETYPE_FONT, in);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
*/

    
    public void paint(Graphics g){
	Graphics2D g2d = (Graphics2D)g;
        g.setColor(Color.white);	
	g.fillRect(0,0,626,585);  	
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    }



public void windowClosing(WindowEvent e) {
        dispose();
        System.exit(0);
    }
    
    public void windowOpened(WindowEvent evt){}
    
    public void windowIconified(WindowEvent evt){}
    
    public void windowClosed(WindowEvent evt){}
    
    public void windowDeiconified(WindowEvent evt){}
    
    public void windowActivated(WindowEvent evt){}
    
    public void windowDeactivated(WindowEvent evt){}


    public void mouseClicked(MouseEvent evt){mouseHandler(evt);}
    public void mouseEntered(MouseEvent evt){;}
    public void mouseExited(MouseEvent evt){;}
    public void mousePressed(MouseEvent evt){;}
    public void mouseReleased(MouseEvent evt){;}
    
     private synchronized void mouseHandler(MouseEvent evt){
	if(evt.getSource() == plotPanel.ssc.button1){
            //state.IsTronRunning = true;
            plotPanel.ssc.IsStop = false;
            plotPanel.ssc.repaint();
	}
	if(evt.getSource() == plotPanel.ssc.button2){
            //state.IsTronRunning = false;
            plotPanel.ssc.IsStop = true;
            plotPanel.ssc.repaint();
	}
    }



    public void actionPerformed(ActionEvent evt) {
        //public Scrollbar slider1, slider2, slider3;
        if(evt.getSource()==inputPanel.text1){
            inputPanel.readTextUpdateSliders();
            plotPanel.getGreenText();
            plotPanel.checkGetPointsGreenwave();
	} else if(evt.getSource()==inputPanel.text2){
            inputPanel.readTextUpdateSliders();
            plotPanel.getGreenText();
            plotPanel.checkGetPointsGreenwave();
	} else if(evt.getSource()==inputPanel.text3){
            inputPanel.readTextUpdateSliders();
            plotPanel.getGreenText();
            plotPanel.checkGetPointsGreenwave();
	} else if (evt.getSource()==plotPanel.reset) {
            inputPanel.reset();
            plotPanel.reset();
        }
    }


    public void adjustmentValueChanged(AdjustmentEvent evt){
	if(evt.getSource()==inputPanel.slider1){
            plotPanel.getGreenText();
            plotPanel.checkGetPointsGreenwave();
	}
	if(evt.getSource()==inputPanel.slider2){
            plotPanel.getGreenText();
            plotPanel.checkGetPointsGreenwave();
	}
	if(evt.getSource()==inputPanel.slider3){
            plotPanel.getGreenText();
            plotPanel.checkGetPointsGreenwave();
        }
    }
}
