import java.awt.*;
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.math.*;

class Md2 extends Frame 
 implements MouseListener, MouseMotionListener {
	
	InterfaceMd2 inf = new InterfaceMd2();
	
	static Md2 theFrame;
	
	// Handle mouse clicked.
	public void mouseClicked(MouseEvent me) {
		//me.consume();
		inf.handleMouseClick(me,this.getGraphics());
	}
	
	// Handle mouse entered.
	public void mouseEntered(MouseEvent me) {
		inf.draw(this.getGraphics());
		// System.out.println("MouseEntered");
		// System.out.println(me);
	}
	
	// Handle mouse exited.
	public void mouseExited(MouseEvent me) {
		
	}
	
	// Handle button pressed.
	public void mousePressed(MouseEvent me) {		
	}
	
	// Handle button released.
	public void mouseReleased(MouseEvent me) {
		// save coordinates
		// mouseX = me.getX();
		// mouseY = me.getY();
		
		// mouseRel = true;
	}
	
	// Handle mouse dragged.
	public void mouseDragged(MouseEvent me) {
		// save coordinates
		// mouseX = me.getX();
		// mouseY = me.getY();
	}
	
	// Handle mouse moved.
	public void mouseMoved(MouseEvent me) {
		inf.handleMouseOver(me,this.getGraphics());
	}
	
	public void delay(long ms) { 
		try { 
			Thread.sleep(ms);
		} 
		catch(InterruptedException e) { } 
	}
	
	public Md2() {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
				stop();
				System.exit(0);
			}
		});
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		
		inf.setState(inf.statePause);
		theFrame = this;
	}
	
	
	
	public int toInt(byte b) {
		int a = 0;
		a |= (b << 24);
		return a;
	}
	
	int frameno=0;
	public void paint(Graphics g) {
		inf.draw(g);
	}

	public static boolean animate = true;
	public static Morph morphFrame;
	public static Md2 md2Frame;
	
	class Animate extends Thread{
		
		boolean running = true;
		
		public void run() {
			Calendar startTimeAndDate = Calendar.getInstance();  
			Calendar endTime = Calendar.getInstance();
			long lStartTimeAndDate,lEndTime;
			while(animate && running) {
				if(inf.state == inf.statePause ) {
					
					if(inf != null) {
						if(inf.md2 != null) {
							
							startTimeAndDate = Calendar.getInstance();  
							//lStartTimeAndDate = startTimeAndDate.getTimeInMillis();
							lStartTimeAndDate = startTimeAndDate.MILLISECOND;
							inf.md2.repaintModel(true);
							endTime = Calendar.getInstance();
							lEndTime = endTime.MILLISECOND; //.getTimeInMillis();
							if(lEndTime < lStartTimeAndDate)
							{
								lEndTime += 1000;
							}
							long toSleep = 45 - (lEndTime - lStartTimeAndDate);
							
							if(toSleep > 0) {
								try { Thread.sleep(toSleep); } catch( Exception e ) {}
							}
						}
						else {
							try { Thread.sleep(40); } catch( Exception e ) {}
							inf.md2.reset();
						}
					}
					else {
						try { Thread.sleep(40); } catch( Exception e ) {}
						inf.md2.reset();
					}
				}
				else {
					try { Thread.sleep(20); } catch( Exception e ) {}
				}
				
			}
		}
	}
	
	Animate anim;
	public void init() {
		md2Frame.setVisible(true);
		
		md2Frame.delay(500);
		md2Frame.repaint();
		
		anim = new Animate();
		anim.running = true;
		anim.start();
	}
	
	public void stop() {
		// anim.yield();
		// anim.stop();
		anim.running = false;
	}
	
	// running main of this class ?
	public boolean singleFrameMe = false;
	
	public static void main(String args[]) {
		System.out.println("Starting Rajesh Md2...");
		Md2 mainFrame = new Md2();
		mainFrame.setSize(800, 570);
		mainFrame.setTitle("Md2");
		mainFrame.setVisible(true);
		
		mainFrame.md2Frame = mainFrame;
		
		// mainFrame.delay(500);
		// mainFrame.repaint();
		
		mainFrame.singleFrameMe = true;
		
		mainFrame.init();
	}
}

