import java.io.*;
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.*;

// Chapter 6 Module 1
public class Mod1 extends Frame implements MouseListener, WindowListener {
    Instructions instructions;
    TitlePanel titlePanel;
    //Mod1Input inputPanel;
    Mod1State state;
    Mod1Plot plotPanel;
    //Mod1Info 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){
        Mod1 f = new Mod1();
        int xmove = 20;
        int ymove = 41;
        f.setSize(1007+xmove,620+ymove);
        f.setVisible(true);
        f.setLayout(null);
    }


  public Mod1() { 
    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 Mod1State();
        state.ttfFont = ttfFont;
        state.sanSerifFont = ttfFont;
        state.serifFont = ttfFont;
        state.symbolFont = phiFont;
        state.italicFont = phiFont;
        TheFonts.setNewFonts(ttfFont, phiFont, italicFont);
    } else {
        state = new Mod1State();
        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 6.1",
                                "Circular Loop in Time-varying Magnetic Field");
    //titlePanel.setBounds(10,10,487,40);
    titlePanel.setBounds(10+xmove,10+ymove,569,40);
    Panel blankTitle = new Panel();
    blankTitle.setBounds(586+xmove,10+ymove,410,40);
    blankTitle.setBackground(Color.lightGray);

    instructions = new Instructions(state);
    instructions.setBounds(586+xmove,59+ymove,410,546);
    instructions.setVisible(true);
    
    plotPanel = new Mod1Plot(state);
    plotPanel.setVisible(true);
    plotPanel.setBounds(12+xmove,59+ymove,567,546);

    // backpanels for plot and instructions
    ////plotPanel.setBounds(12,59,567,546);
    Panel p_p_b = new Panel();
    p_p_b.setBackground(Color.black);
    p_p_b.setBounds(11+xmove,58+ymove,569,548);
    ////instructions.setBounds(586,59,400,546);
    Panel p_instr_b = new Panel();
    p_instr_b.setBackground(Color.black);
    p_instr_b.setBounds(585+xmove,58+ymove,412,548);


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

            
    add(titlePanel);
    add(blankTitle);
    add(instructions);
    add(plotPanel);
    add(p_p_b);
    add(p_instr_b);

    add(p0);
    add(p00);

    this.addWindowListener(this);
    plotPanel.ssc.button1.addMouseListener(this);
    plotPanel.ssc.button2.addMouseListener(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();
	}
    }

}
