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 1 thru 3
public class Mod3 extends Frame implements WindowListener{
    TitlePanel titlePanel;
    Mod3Input inputPanel;
    Mod3State state;
    Mod3Plot plotPanel;

    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){
        Mod3 f = new Mod3();
        int xmove = 20;
        int ymove = 41;
        f.setSize(809+xmove,611+ymove);
        f.setVisible(true);
        f.setLayout(null);
    }

  public Mod3() { 
    setLayout(null);    
    String lcOSName = System.getProperty("os.name").toLowerCase();
    boolean MAC_OS_X = lcOSName.startsWith("mac os x");
    //System.out.println(lcOSName);
    
    if (!useOldFonts) {
        //getUlabyFonts();
        state = new Mod3State();
        state.ttfFont = ttfFont;
        state.sanSerifFont = ttfFont;
        state.serifFont = ttfFont;
        state.symbolFont = phiFont;
        state.italicFont = phiFont;
        TheFonts.setNewFonts(ttfFont, phiFont, italicFont);
    } else {
        state = new Mod3State();
        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;

    // Get focus off any visible component ... looks nicer.
    Panel nullPanel = new Panel();
    Button nullButton = new Button();
    nullPanel.add(nullButton);
    nullPanel.setVisible(false);

    //titlePanel = new TitlePanel(subtitleFont,"Module 1.3","Phase Lead/Lag");
    titlePanel = new TitlePanel(state.ttfFont.deriveFont(Font.BOLD,15f),
                                "Module 1.3","Phase Lead/Lag");
    titlePanel.setBounds(10+xmove,10+ymove,405,40);
    Panel blankTitle = new Panel();
    blankTitle.setBounds(424+xmove,10+ymove,376,40);
    blankTitle.setBackground(Color.lightGray);
	
    plotPanel = new Mod3Plot(state);
    plotPanel.setVisible(true);
    plotPanel.setBounds(11+xmove,261+ymove,786,337);

    inputPanel = new Mod3Input(state);
    inputPanel.setBounds(12+xmove,60+ymove,784,190);

    // backpanels for input, plot and info
    ////inputPanel.setBounds(12,60,784,190);
    Panel p_i_w = new Panel();
    p_i_w.setBackground(Color.white);
    p_i_w.setBounds(11+xmove,59+ymove,786,192);
    Panel p_i_b = new Panel();
    p_i_b.setBackground(Color.black);
    p_i_b.setBounds(10+xmove,58+ymove,788,194);
    ////plotPanel.setBounds(11,261,786,337);
    Panel p_p_b = new Panel();
    p_p_b.setBackground(Color.black);
    p_p_b.setBounds(10+xmove,260+ymove,788,339);

    //backpanels for applet
    Panel p0 = new Panel();
    p0.setBackground(Color.cyan);
    p0.setBounds(3+xmove,3+ymove,803,603);
	    
    Panel p00 = new Panel();
    p00.setBackground(Color.black);
    p00.setBounds(xmove,ymove,809,609);
            
    add(nullPanel);
    add(titlePanel);
    add(blankTitle);
    add(inputPanel);
    add(plotPanel);

    add(p_i_w);
    add(p_i_b);
    add(p_p_b);

    plotPanel.initialPlot();
    add(p0);
    add(p00);

    nullButton.requestFocus();

    this.addWindowListener(this);
  }

    
/*
    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);
        g.setColor(Color.black);
    }



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){}
}
