import java.awt.*;
import java.applet.AudioClip;
import java.util.*; //for StringTokenizer
// import java.net.*;
import ListStim;

/**	Applet implementing an auditory AX discrimination task.
you may use, modify, redistribute this applet provided that
you keep this remark intact, attached to the source code.
author: C. Pallier (pallier@ruccs.rutgers.edu)
  */




public class AXtask extends java.applet.Applet {
private
 Button ButStart,ButFin;
 Button ButSame,ButDiff;
 Label LabTrialnum;
 Panel Pan;
 
 ListStim Pairs;
 int Trialnum;
 int cs,bs,cd,bd;

 AudioClip e1,e2;
 String s1,s2;
 
 String pourcent(double a) {
	 double b=a*10000.0;
	 double c=(b-(b-Math.floor(b)))/100.0;
         if (c<0.01) c=0;
	 return new String(String.valueOf(c)+"%");
 }

 public void init() {
	/* 
	In a future version, the stimuli could be anywhere on the net.
	
	String stimfilename=getParameter("StimulusFile");
	URLConnection URLconn=new URLConnection(new URL(stimfilename));
	InputStream in;
	try {
		URLconn.connect();
		URLconn.setDoInput(true);
		in=URLconn.getInputStream(); 
	} catch (IOException e) { }

    */

	Pairs=new ListStim();
	Trialnum = 1;
	cs=bs=cd=bd=0;
 
	setLayout(new FlowLayout(FlowLayout.CENTER));
        add(ButStart = new Button("Start"));
	resize(240,60);
    
    ButSame = new Button("Same"); 
    LabTrialnum = new Label("");
    ButDiff = new Button("Different"); 
}

 void showScore() {
 	 remove(ButDiff);
	 remove(LabTrialnum);
	 remove(ButSame);
	 invalidate();

	 setLayout(new GridLayout(2,1,0,5));
	 add(new Label("Score : Same/Same="  + pourcent((double)cs/((double)cs+(double)bs)) + " ; Diff/Diff=" +  pourcent((double)cd/((double)cd+(double)bd))));
	 Pan=new Panel();
	 Pan.add(ButFin=new Button("Finish"));
	 add(Pan);
	 paintAll(getGraphics());  
 }

 void nextTrial() {
   if (!Pairs.hasMoreElements()) { showScore(); }
   else {
   
   LabTrialnum.setText(" "+ String.valueOf(Trialnum) + "/" + String.valueOf(Pairs.NbElements())+ " ");
   LabTrialnum.show();
   ButSame.hide(); 
   ButDiff.hide();
   repaint();
   
   
   StringTokenizer s=new StringTokenizer(Pairs.getNextPair()," ");
   try { s1=s.nextToken(); s2=s.nextToken(); 
	 } catch (NoSuchElementException  e) { };
   
   e1=getAudioClip(getCodeBase(),s1 + ".au");
   e2=getAudioClip(getCodeBase(),s2 + ".au");

   try { Thread.sleep(1500); } catch (InterruptedException e) { }
   e1.play();
   try { Thread.sleep(1000); } catch (InterruptedException e) { }
   e2.play();
   
   ButSame.show(); 
   ButDiff.show();
   paintAll(getGraphics());
   Trialnum++;
   }
 }

 public boolean action(Event evt, Object arg) {
    if (evt.target instanceof Button) {
     String label = (String) arg;
     if (label.equals("Start")) { 
		 remove(ButStart); 
		 add(ButSame); 
		 add(LabTrialnum);
		 add(ButDiff);
		 ButSame.hide();
		 LabTrialnum.show();
		 ButDiff.hide();
		 paintAll(getGraphics()); 
		 nextTrial(); 
	 }
     if (label.equals("Same"))  { 
		 if (s1.equals(s2)) cs++; else bs++;
		 nextTrial(); 
	 }
     if (label.equals("Different")) { 
		 if (!(s1.equals(s2))) cd++; else bd++;
		 nextTrial(); 
	 }
	 if (label.equals("Finish")) {
		 Pan.remove(ButFin);
		 showStatus("Bye, bye...");
  		 Pan.add(new Label("Thank you!"));
   		paintAll(getGraphics());
	 }
  }
  return true;
 }


}
