/********************************
*
* Miro Kazakoff
* 047-76-5220
* Final Project
* temp version
*******************************/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Clubs extends Applet implements ItemListener, ActionListener{
private blinkThread Thread1, Thread2, Thread3,
Thread4, Thread5;
boolean toggle = false;
int x, y;
int clicks;
int hits;
int iterations;
int xArray[];
int yArray[];
Image picture, xPic;
int width, height;
MyCanvas myCanvas;
CheckboxGroup speed;
Checkbox slow, medium, fast;
Panel speedPanel;
Label speedLabel;
String chosenSpeed;
CheckboxGroup repetitions;
Panel repPanel;
Checkbox twenty, fourty, sixty;
Label repLabel;
int target;
Panel buttonPanel;
Button begin;
boolean moreThanOnce = false;
public void init(){
chosenSpeed = new String("slow");
xPic = getImage( getDocumentBase(), "images/x.gif" );
myCanvas = new MyCanvas(this);
myCanvas.setBackground(Color.green);
myCanvas.setSize(500,300);
xArray = new int[5];
yArray = new int[5];
for(int i = 0; i < 5; i++){
xArray[i] = -1;
yArray[i] = -1;
}
speedPanel = new Panel();
speedLabel = new Label( "Choose your Speed");
speedPanel.add(speedLabel);
speed = new CheckboxGroup();
slow = new Checkbox( "Slow", speed, true);
slow.addItemListener(this);
speedPanel.add(slow);
medium = new Checkbox( "Medium", speed, false);
medium.addItemListener(this);
speedPanel.add(medium);
fast = new Checkbox( "Fast", speed, false);
fast.addItemListener(this);
speedPanel.add(fast);
repPanel = new Panel();
repLabel = new Label( "Choose Your Number of Tries");
repPanel.add(repLabel);
repetitions = new CheckboxGroup();
twenty = new Checkbox( "20", repetitions, true);
twenty.addItemListener(this);
repPanel.add(twenty);
fourty = new Checkbox( "40", repetitions, false);
fourty.addItemListener(this);
repPanel.add(fourty);
sixty = new Checkbox( "60", repetitions, false);
sixty.addItemListener(this);
repPanel.add(sixty);
buttonPanel = new Panel();
begin = new Button( "Start");
begin.addActionListener(this);
buttonPanel.add(begin);
add(myCanvas);
add(speedPanel);
add(repPanel);
add(begin);
}
/*
public void start()
{
Thread1 = new blinkThread(this, "Homer");
Thread2 = new blinkThread(this, "Marge");
Thread3 = new blinkThread(this, "Bart");
Thread4 = new blinkThread(this, "Lisa");
Thread5 = new blinkThread(this, "Maggie");
}
*/
public void goToIt()
{
Thread1 = new blinkThread(this, "Homer");
Thread2 = new blinkThread(this, "Marge");
Thread3 = new blinkThread(this, "Bart");
Thread4 = new blinkThread(this, "Lisa");
Thread5 = new blinkThread(this, "Maggie");
Thread1.start();
Thread2.start();
Thread3.start();
Thread4.start();
Thread5.start();
}
public void stop()
{
/* Thread1.stop();
Thread2.stop();
Thread3.stop();
Thread4.stop();
Thread5.stop();
*/
}
public void buttonPressed(int x, int y){
++clicks;
if ( Thread1.hit == false &&
x > Thread1.returnX() &&
x < (Thread1.returnX() + Thread1.returnWidth()) &&
y > Thread1.returnY() &&
y < (Thread1.returnY() + Thread1.returnHeight()) ){
Thread1.hit = true;
(Thread1.returnSound()).play();
myCanvas.makeRect( xPic, Thread1.returnX(), Thread1.returnY() );
++hits;
}
else if ( Thread2.hit == false &&
x > Thread2.returnX() &&
x < (Thread2.returnX() + Thread2.returnWidth()) &&
y > Thread2.returnY() &&
y < (Thread2.returnY() + Thread2.returnHeight()) ){
Thread2.hit = true;
myCanvas.makeRect( xPic, Thread2.returnX(), Thread2.returnY() );
(Thread2.returnSound()).play();
++hits;
}
else if ( Thread3.hit == false &&
x > Thread3.returnX() &&
x < (Thread3.returnX() + Thread3.returnWidth()) &&
y > Thread3.returnY() &&
y < (Thread3.returnY() + Thread3.returnHeight()) ){
Thread3.hit = true;
myCanvas.makeRect( xPic, Thread3.returnX(), Thread3.returnY() );
(Thread3.returnSound()).play();
++hits;
}
else if ( Thread4.hit == false &&
x > Thread4.returnX() &&
x < (Thread4.returnX() + Thread4.returnWidth()) &&
y > Thread4.returnY() &&
y < (Thread4.returnY() + Thread4.returnHeight()) ){
Thread4.hit = true;
myCanvas.makeRect( xPic, Thread4.returnX(), Thread4.returnY() );
(Thread4.returnSound()).play();
++hits;
}
else if ( Thread5.hit == false &&
x > Thread5.returnX() &&
x < (Thread5.returnX() + Thread5.returnWidth()) &&
y > Thread5.returnY() &&
y < (Thread5.returnY() + Thread5.returnHeight()) ){
Thread5.hit = true;
myCanvas.makeRect( xPic, Thread5.returnX(), Thread5.returnY() );
(Thread5.returnSound()).play();
++hits;
}
else
showStatus( "Chances: " + Integer.toString(iterations) +
" Clicks: " + Integer.toString(clicks) +
" Hits: " + Integer.toString(hits));
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource() == slow)
chosenSpeed = "slow";
if(e.getSource() == medium)
chosenSpeed = "medium";
if(e.getSource() == fast)
chosenSpeed = "fast";
if(e.getSource() == twenty)
target = 20;
if(e.getSource() == fourty)
target = 40;
if(e.getSource() == sixty)
target = 60;
}
public void actionPerformed( ActionEvent e)
{
if(speed.getSelectedCheckbox() == slow)
chosenSpeed = "slow";
if(speed.getSelectedCheckbox() == medium)
chosenSpeed = "medium";
if(speed.getSelectedCheckbox() == fast)
chosenSpeed = "fast";
if(repetitions.getSelectedCheckbox() == twenty)
target = 20;
if(repetitions.getSelectedCheckbox() == fourty)
target = 40;
if(repetitions.getSelectedCheckbox() == sixty)
target = 60;
resetThreads();
myCanvas.eraseRect(0, 0, 500, 300);
goToIt();
}
public void resetThreads()
{
clicks = 0;
hits = 0;
iterations = 0;
showStatus( "Chances: " + Integer.toString(iterations) +
" Clicks: " + Integer.toString(clicks) +
" Hits: " + Integer.toString(hits));
}
}
class blinkThread extends Thread{
Clubs c;
int x, y;
int showTime, restTime;
public AudioClip sound;
public Image picture;
public boolean hit;
String speed;
public blinkThread( Clubs club, String threadName)
{
c = club;
setSpeeds();
setName(threadName);
setSound();
setPic();
}
public synchronized void run()
{
blinkThread curThread;
curThread = (blinkThread)Thread.currentThread();
try {
sleep( 500 + (int) (Math.random() * 1000));
} catch (InterruptedException e) {}
while(c.iterations <= c.target ){
hit = false;
c.showStatus( "Chances: " + Integer.toString(c.iterations) +
" Clicks: " + Integer.toString(c.clicks) +
" Hits: " + Integer.toString(c.hits));
setPosition();
setTime();
try {
hit = false;
c.myCanvas.makeRect( picture, x, y);
sleep( showTime );
c.myCanvas.eraseRect( x, y, picture.getWidth(c),
picture.getHeight(c) );
hit = true;
sleep( restTime );
}
catch (InterruptedException e)
{
e.printStackTrace();
}
++c.iterations;
}
}
public void setPosition()
{
x = (int) (Math.random() * 400 );
y = (int) (Math.random() * 250 );
}
public void setSpeeds()
{
speed = c.chosenSpeed;
}
public void setTime()
{
if( (speed).equals("slow")){
showTime = 3000 + (int) (Math.random() * 1000 );
restTime = 1500 + (int) (Math.random() * 500 );
}
if( (speed).equals("medium")){
showTime = 2000 + (int) (Math.random() * 500 );
restTime = 1000 + (int) (Math.random() * 500 );
}
if( (speed).equals("fast")){
showTime = 1250 + (int) (Math.random() * 1000 );
restTime = 500 + (int) (Math.random() * 500 );
}
}
public int returnWidth()
{
return picture.getWidth(c) ;
}
public int returnHeight()
{
return picture.getHeight(c) ;
}
public int returnX()
{
return x;
}
public int returnY()
{
return y;
}
public void setPic()
{
if( getName().equals( "Homer") )
picture = c.getImage( c.getDocumentBase(), "images/homer.gif" );
if( getName().equals( "Marge") )
picture = c.getImage( c.getDocumentBase(), "images/marge.gif" );
if( getName().equals( "Bart") )
picture = c.getImage( c.getDocumentBase(), "images/bart.gif" );
if( getName().equals( "Lisa") )
picture = c.getImage( c.getDocumentBase(), "images/lisa.gif" );
if( getName().equals( "Maggie") )
picture = c.getImage( c.getDocumentBase(), "images/maggie.gif" );
}
public void setSound()
{
if( getName().equals( "Homer") )
sound = c.getAudioClip( c.getDocumentBase(), "sounds/doh.au" );
if( getName().equals( "Marge") )
sound = c.getAudioClip( c.getDocumentBase(), "sounds/hmmm.au" );
if( getName().equals( "Bart") )
sound = c.getAudioClip( c.getDocumentBase(), "sounds/aycar.au" );
if( getName().equals( "Lisa") )
sound = c.getAudioClip( c.getDocumentBase(), "sounds/room.au" );
if( getName().equals( "Maggie") )
sound = c.getAudioClip( c.getDocumentBase(), "sounds/suck.au" );
}
public AudioClip returnSound()
{
return sound;
}
}