import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

//****************************
// extension of Applet class
//****************************
public class MainQmPbApplet extends javax.swing.JApplet implements ItemListener , ActionListener, Runnable
{
    //******************
    // additional attributes
    //******************
    Font f = new Font("TimesRoman", Font.BOLD, 24);
    Image workspace;
    Graphics offscreen;
    QmGame qmGame = new QmGame();
    Thread runner;
    JButton trigger = new JButton("Trigger");
    JButton launch = new JButton("Launch");
    Choice  hC = new Choice();     // create drop down menu - to select function
  
public void init()
    {
	hC.addItem("h=0.0001");
	hC.addItem("h=0.001");
	hC.addItem("h=0.01");
	hC.addItem("h=0.1");
	hC.addItem("h=1.0");
	hC.addItem("h=10.0");
	hC.addItem("h=100.0");
	hC.addItem("h=1000.0");
	hC.addItem("h=10000.0");

	workspace = createImage(380,400);
	offscreen = workspace.getGraphics();
	FlowLayout flo = new FlowLayout();
        Container pane = getContentPane();
	pane.setLayout(flo);
	pane.add(trigger);
	pane.add(launch);
	pane.add (hC);
	hC.select(7); // 0 is 1st choice
	setContentPane(pane);
	trigger.addActionListener(this);
	launch.addActionListener(this);
	hC.addItemListener(this);
    }
public void actionPerformed(ActionEvent evt)
    {
	Object source = evt.getSource();
	if (source == trigger)
	    {
		//		System.out.println("TRIGGER PULLED!");
		qmGame.paddle1.activate(qmGame.time);
		qmGame.paddle2.activate(qmGame.time);
		qmGame.hole.activate(qmGame.time);
	    }
	else if (source == launch)
	    {
		// System.out.println("LAUNCHED!");
		if (qmGame.ballActive == false)
		    {
			qmGame.ballActive = true;
			if (qmGame.ballNo == 0)
			    {
				qmGame.ballNo = 4;
				qmGame.score = 0;
			    }
			else
			    qmGame.ballNo--;
		    }
	    }
    }
    public void itemStateChanged(ItemEvent evt)
    {
	int hindex;
	hindex = hC.getSelectedIndex();
	if (hindex == 0)
	    qmGame.h = 0.0001;
	else if (hindex == 1)
	    qmGame.h = 0.001;
	else if (hindex == 2)
	    qmGame.h = 0.01;
	else if (hindex == 3)
	    qmGame.h = 0.1;
	else if (hindex == 4)
	    qmGame.h = 1.0;
	else if (hindex == 5)
	    qmGame.h = 10.0;
	else if (hindex == 6)
	    qmGame.h = 100.0;
	else if (hindex == 7)
	    qmGame.h = 1000.0;
	else if (hindex == 8)
	    qmGame.h = 10000.0;
    }	
    public void paint(Graphics g)
    {
	super.paint(offscreen);
	qmGame.plot(g,f,workspace,offscreen,this);
    }


    public void start()
    {
	if (runner == null)
	{
	    runner = new Thread(this);
	    runner.start();
	}
    }
public void run()
    {
	Thread thisThread = Thread.currentThread();
	while (runner == thisThread)
	    {
		repaint();
		try 
		    {
			Thread.sleep(1000);
		    }
		catch (InterruptedException e){}
	    }
    }
public void stop()
    {
	if (runner != null)
	    runner = null;
    }
}
