Back to index

wims  3.65+svn20090927
VreemdeEend1.java
Go to the documentation of this file.
00001 // horrid amateur scriblings: no need for copyrights...
00002 // needs "rewriting"... 
00003 // js-function ReadApplet() returns: total time
00004 // and lines with:
00005 // time1:freq1 \n
00006 // time2:freq2 \n
00007 // time3:freq1 \n
00008 // ...
00009 // time_max:freq_max \n
00010 
00011 // Applet params: 
00012 // max=number of correct hits [default 20 : minimal 5, maximal 60]
00013 // renew=yes : every click a new image [default]
00014 // renew=no  : only a new image if clicked on the correct image
00015 // lang=nl,en,fr [default en]
00016 // show_result=yes,no : show the stat-data in the applet in a primitive tabel  [default yes]
00017 
00018 import java.applet.*;
00019 import java.awt.*;
00020 import java.util.*;
00021 import java.awt.event.*;
00022 
00023 public class VreemdeEend1 extends Applet implements Runnable{
00024     private static final long serialVersionUID = 1L;
00025     Graphics canvas;
00026     String replystring="";
00027     int xsize=160;int ysize=160;int sx=80;
00028     //enkel plaatje 160x160 heeft 4 hokjes ...hokje is dus 80x80
00029     int xmax=4*xsize;int ymax=ysize;    
00030     int keuze[]={0,1,2,3};int kans[]={0,1};int polies[]={0,1,2,3};int kleuren[]={0,1,2,3,4,5,6,7};
00031     int xobject[][][]={
00032     {{10,70,30,70,10,10},{10,70,70,10,50,10},{10,70,70,40,10,10},{10,40,70,70,10,10}},
00033     {{10,70,70,10,10},{10,70,70,10,10},{10,70,70,10,10},{10,70,70,10,10}},
00034     {{40,70,10,40},{10,70,40,10},{10,70,10,10},{70,10,70,70}},
00035     {{10,70,10,10},{10,70,70,10},{10,70,70,10},{10,70,10,10}}
00036     };
00037     int yobject[][][]={
00038     {{10,10,40,70,70,10},{10,10,70,70,40,10},{10,10,70,40,70,10},{10,40,10,70,70,10}},
00039     {{10,10,70,70,10},{10,10,70,70,10},{10,10,70,70,10},{10,10,70,70,10}},
00040     {{10,70,70,10},{10,10,70,10},{10,40,70,10},{10,40,70,10}},
00041     {{10,70,70,10},{70,10,70,70},{10,10,70,10},{10,10,70,10}}
00042     };
00043     int colors[][] = {{250,0,0},{0,250,0},{0,0,250},{250,250,0},{250,0,250},{0,250,250},{0,0,0},{255,255,255}}; 
00044 
00045     int fout=0;int anders=0;long begin;int klaar;int goedzo=0;
00046     int dx;int dy;long start;int foutcounter=1;double diff;
00047     int max=20;String duration="" ;int counter=1;int first_try=1;
00048     long stop;public long totaltime=0;Font defaultFont = new Font("Courier",0, 14);
00049     public Image ok;public Image nok;String time="time in seconds";
00050     String freq="attempts";String teller="number";String ready_string="DONE";int show_result=1;int do_paint=1;
00051     
00052     public void init(){
00053        ok=getImage(getCodeBase(), "ok.gif");nok=getImage(getCodeBase(), "nok.gif"); 
00054        begin=System.currentTimeMillis();klaar=0;
00055        String c=getParameter("max");if(c!=null && c.length()>0){max = Integer.parseInt(c, 10); if(max>60){max=60;} if(max<2){max=5;}}
00056        String renew=getParameter("renew");if(renew!= null && renew.equals("no")){first_try=0;}
00057        String lang=getParameter("language");
00058        if(lang != null){
00059            if(lang.equals("nl")){time="tijd in seconden";freq="pogingen";teller="nummer";ready_string="KLAAR";}
00060            if(lang.equals("fr")){time="temps (sec.)";freq="tentatives";teller="numero";ready_string="FINI";}
00061        }          
00062        String show=getParameter("show_result");if(show != null){if(show.equals("no")){show_result=0;}}   
00063        addMouseListener(
00064            new MouseAdapter(){
00065               public void mousePressed(MouseEvent e){
00066                   dx = e.getX();
00067                   dy = e.getY();
00068                   if(klaar==0){
00069                      for(int p=0;p<4;p++){
00070                          if(dy>0 && dy<ymax){
00071                             if(dx>p*xsize && dx<(p+1)*xsize){
00072                                 if(fout==p){
00073                                    stop=System.currentTimeMillis();
00074                                    diff=(double)(Math.round(1000*(stop-start)))/1000000;
00075                                    duration=duration+","+diff;
00076                                    replystring=replystring+"\n"+diff+" : "+foutcounter;
00077                                    foutcounter=1;
00078                                     counter++;goedzo=1;do_paint=1;
00079                                    if(counter> max){klaar=1;}
00080                                    start=System.currentTimeMillis();
00081                                 }
00082                                 else
00083                                 {
00084                                    goedzo=0;
00085                                    foutcounter++;
00086                                    if(first_try==0){do_paint=0;}else{do_paint=1;}
00087                                 }
00088                             }
00089                          }
00090                      }
00091                   repaint();
00092                   }
00093               }
00094            }
00095        );
00096     
00097 }
00098 
00099     public static int[] Shuffle(int[] S){
00100            int ll= S.length;
00101 
00102            for (int i=0;i<ll;i++){
00103               int r = (int) (Math.random() * (i+1));
00104               int swap = S[r];
00105               S[r] = S[i];
00106               S[i] = swap;
00107            }
00108            for(int i=0;i<ll;i++){
00109             //System.out.println(S[i]);
00110            }
00111         return S;
00112     }
00113     
00114    public void paint(Graphics canvas){
00115        Graphics2D g2 = (Graphics2D) canvas;
00116        g2.setStroke( new BasicStroke(4.0f) );
00117     if(klaar==0){
00118        g2.setColor(Color.black);
00119        g2.clearRect(xsize,ysize,2*xsize,ysize+10);
00120        g2.drawString(teller+" : "+counter,xsize,ysize+10);
00121        if(do_paint==1){
00122            int basis=0;int t;int p0;int p1;int p2;int p3;int geweest[]=new int[3];
00123            Polygon poly0;Polygon poly1;Polygon poly2;Polygon poly3;
00124            keuze=Shuffle(keuze);fout=keuze[0];kans=Shuffle(kans);anders=kans[0];
00125            kleuren=Shuffle(kleuren);int k0=kleuren[0];int k1=kleuren[1];int k2=kleuren[2];
00126            Color kleur1=new Color(colors[k0][0],colors[k0][1],colors[k0][2]); 
00127            Color kleur2=new Color(colors[k1][0],colors[k1][1],colors[k1][2]); 
00128            for(int p=0;p<4;p++){
00129                g2.setColor(kleur1);
00130               g2.fillRect(p*(xsize),1,(p+1)*(xsize),ymax-2);
00131                g2.setColor(kleur2);basis=p*xsize;
00132               polies=Shuffle(polies);p0=polies[0];p1=polies[1];p2=polies[2];p3=polies[3];
00133                //System.out.println("p0="+p0+" p1="+p1+" p2="+p2+" p3="+p3);
00134                poly0=new Polygon(xobject[p0][p],yobject[p0][p],xobject[p0][p].length);
00135                poly1=new Polygon(xobject[p1][p],yobject[p1][p],xobject[p1][p].length);
00136               poly2=new Polygon(xobject[p2][p],yobject[p2][p],xobject[p2][p].length);
00137                poly0.translate(basis,0);
00138                poly1.translate(basis+sx,0);
00139                poly2.translate(basis,sx);
00140               if(anders==0){g2.fillPolygon(poly0);g2.fillPolygon(poly1);g2.fillPolygon(poly2);}
00141               else{g2.drawPolygon(poly0);g2.drawPolygon(poly1);g2.drawPolygon(poly2);}
00142               
00143               if(fout!=p){
00144                   poly3=new Polygon(xobject[p3][p],yobject[p3][p],xobject[p3][p].length);
00145                   poly3.translate(basis+sx,sx);
00146                   if(anders==0){g2.fillPolygon(poly3);}else{g2.drawPolygon(poly3);}
00147               }else{
00148                   geweest[0]=p0;geweest[1]=p1;geweest[2]=p2;geweest=Shuffle(geweest);t=geweest[0];
00149                   poly3=new Polygon(xobject[t][p],yobject[t][p],xobject[t][p].length);
00150                   poly3.translate(basis+sx,sx);
00151                   if(anders==0){g2.fillPolygon(poly3);}else{g2.drawPolygon(poly3);}
00152               }
00153                              
00154               Color kleur3=new Color(colors[k2][0],colors[k2][1],colors[k2][2]); 
00155                g2.setColor(kleur3);
00156               g2.drawRect(p*(xsize),1,(p+1)*(xsize),ymax-2);
00157            }
00158               
00159            if(counter==1){ start=begin;}
00160            else{
00161               g2.clearRect(2*xsize,ysize,50,50);
00162               if(goedzo==1){g2.drawImage(ok,2*xsize,ysize, this);}else{g2.drawImage(nok,2*xsize,ysize, this);}
00163            }
00164         }
00165         else
00166         {
00167            g2.clearRect(2*xsize,ysize,50,50);
00168            g2.drawImage(nok,2*xsize,ysize, this);
00169         }
00170        }
00171        else
00172        {
00173            if(show_result == 1){
00174            // finished: show "table" with time:freq
00175               g2.clearRect(2*xsize,ysize,50,50);
00176                g2.setColor(Color.black);g2.fillRect(0,0,xmax,ymax);
00177               g2.setFont(defaultFont);
00178                StringTokenizer q = new StringTokenizer(replystring, "\n");
00179               int m=q.countTokens();
00180                String sub="";int X=10;int Y=10;
00181                g2.setColor(Color.white);
00182                g2.drawString("t : T",X,Y);
00183               for(int s=0;s<m;s++){
00184                   Y=Y+14;
00185                   sub=q.nextToken(); 
00186                   g2.drawString(sub,X,Y);
00187                   if(Y>(ymax-2*14)){
00188                      X=X+90;
00189                       Y=10;
00190                      g2.drawString("t : T",X,Y);
00191                   }
00192               }
00193               // legenda 
00194               g2.drawString("t= "+time+"   T= "+freq,xsize,ysize-2);      
00195            }
00196            else
00197            {
00198               g2.clearRect(0,0,xmax,ymax+50);
00199               g2.setColor(Color.white);
00200               g2.fillRect(0,0,xmax,ymax);
00201               g2.setColor(Color.black);
00202               g2.drawString(ready_string,2*xsize,ymax-100);
00203            }
00204        }
00205     }
00206     
00207    public void start(){}
00208 
00209    public void stop(){}
00210 
00211    public void run(){}
00212 
00213    public String ReadApplet(){
00214        if(counter<max){return "error : not finished";}
00215        else
00216        {
00217            double totaltime=(double) (stop-begin)/1000;
00218            return totaltime+":"+max+"\n"+replystring;
00219         }
00220     }
00221 }
00222 
00223