Back to index

wims  3.65+svn20090927
rooster.java
Go to the documentation of this file.
00001 // J.M.Evers 16/2/2008 
00002 // This is all amateur scriblings...so No Copyrights
00003 // the applet presents a simple grid...E6/H1 pupils will be asked to
00004 // determine the perimetre and surface area (or ratio of these) of a "freestyle" figure
00005 // build from at least $x squares...
00006 // In the non-interactive mode, a random figure will be shown.
00007 // size_x and size_y may be different: the perimetre will be a float : calulated with x=1 y=size_y/size_x 
00008 
00009 // example html page:
00010 //<html>
00011 //     <body>
00012 //            <script language="javascript" type="text/javascript">
00013 //                   function lees(){
00014 //                          var input=document.applets[0].ReadApplet(); 
00015 //                          alert(input);
00016 //                          //<--- applet returns: String S=total+","+original+","+changed+","+cnt;
00017 //                          //!! total squares,left original squares,changed squares,perimetre of changed squares
00018 //                          //-->
00019 //                   }
00020 //            </script>
00021 //            <applet id="MathView" code="rooster.class" codebase="." archive="rooster.jar" width="640" height="480">
00022 //                   <param name="max_x" value="5">
00023 //                   <param name="max_y" value="5">         
00024 //                   <param name="size_x" value="25">
00025 //                   <param name="size_y" value="50">
00026 //                   <param name="color1" value="25:100:76">
00027 //                   <param name="color2" value="orange">
00028 //                   <param name="interactive" value="0/1">
00029 //                   <param name="level" value="$level">
00030 //                   <param name="percentage" value="0/1">
00031 //            </applet>
00032 //            <p>
00033 //            <input type="button" name=".....TEST......" value=".....TEST....." onclick="javascript:lees();">
00034 //     </body>
00035 //</html>
00036 // 
00037 // color1="background"
00038 // color2="clicked" in "interactive"=1
00039 // color2="questioncolor" in "interactive"=0
00040 // color may also be "red","green","blue","orange","yellow"
00041 
00042 // 21/3/2008 added:
00043 // level: integer 0,1,2,3 only interesting when "interactive=0" (and not of importance for "percentage")
00044 // level=0 will produce somewhat easier random figures: level=3 will give maximum randomness.
00045 
00046 // percentage: only activated when "interactive=0" . Will give a random figure : but a non-random placement of the coloured squares.
00047 // thus an very easy reading of a "percentage coloured" surface. Use together with max_x and max_y to build easy exercises.
00048 // percentage=1 and level>0 will produce alternating coloures
00049 // percentage=1 and level=0 will produce a coloured block
00050 
00051 // 22/3/2008 added
00052 // color names : lightgreen,lightblue,brown,pink,cyan,salmon
00053 
00054 // for both params level/percentage a full coloured figure is inhibited (so never 0% coloured or 100% coloured)
00055 
00056 
00057 import java.applet.*;
00058 import java.awt.*;
00059 import java.awt.event.*;                                                                                                                        
00060 import java.util.*;
00061 import java.lang.Math;                                                                                                                        
00062 
00063 public class rooster extends Applet implements Runnable{
00064     private static final int serialVersionUID = 1;
00065     int max_x=20;int max_y=20;
00066     int rooster[][];
00067     // = new int[max_y+2][max_x+2];
00068     int dx;int dy;Thread thread = null;
00069     Image plaatje;Graphics tekening;
00070     int hokje_x=20;int hokje_y=20;
00071     int xsize=hokje_x*max_x;
00072     int ysize=hokje_y*max_y;
00073     int R1=111;int G1=222;int B1=040;   
00074     int R2=040;int G2=222;int B2=111;   
00075     int interactive=1;int percentage=0;int level=3;
00076 
00077     public void init(){
00078        String c;
00079        c=getParameter("color1");
00080        if(c!=null && c.length()>0){
00081            // don't know how to do this in 1 regexp
00082            if(c.indexOf(',')== -1 && c.indexOf(':')== -1 && c.indexOf(';')== -1 ){
00083               if(c.equalsIgnoreCase("red")){R1=255;G1=0;B1=0;}
00084               else
00085               if(c.equalsIgnoreCase("green")){R1=0;G1=255;B1=0;}
00086               else
00087               if(c.equalsIgnoreCase("blue")){R1=0;G1=0;B1=255;}
00088               else
00089               if(c.equalsIgnoreCase("orange")){R1=238;G1=154;B1=0;}
00090               else
00091               if(c.equalsIgnoreCase("yellow")){R1=255;G1=255;B1=0;}
00092               else
00093               if(c.equalsIgnoreCase("purple")){R1=160;G1=32;B1=240;}
00094               else
00095               if(c.equalsIgnoreCase("lightgreen")){R1=144;G1=238;B1=144;}
00096               else
00097               if(c.equalsIgnoreCase("lightblue")){R1=173;G1=216;B1=230;}
00098               else
00099               if(c.equalsIgnoreCase("cyan")){R1=0;G1=255;B1=255;}
00100               else
00101               if(c.equalsIgnoreCase("brown")){R1=165;G1=24;B1=24;}
00102               else
00103               if(c.equalsIgnoreCase("salmon")){R1=250;G1=128;B1=114;}
00104               else
00105               if(c.equalsIgnoreCase("pink")){R1=255;G1=192;B1=203;}
00106               else{R1=255;G1=0;B1=0;}
00107            }
00108            else
00109            {
00110               c=c.replace(':',',');c=c.replace(';',',');
00111               StringTokenizer q = new StringTokenizer(c, ",");
00112               String k;int rgb;
00113               for( int a=0; a<3 ; a++){
00114                   k=q.nextToken();
00115                   rgb=Integer.parseInt(k, 10);
00116                   if(rgb<0){rgb=0;}
00117                   if(rgb>255){rgb=255;}
00118                   if(a==0){R1=rgb;}
00119                   if(a==1){G1=rgb;}
00120                   if(a==2){B1=rgb;}
00121               }
00122            }
00123        }
00124 
00125        c=getParameter("color2");
00126        if(c!=null && c.length()>0){
00127            if(c.indexOf(',')== -1 && c.indexOf(':')== -1 && c.indexOf(';')== -1 ){
00128               if(c.equalsIgnoreCase("red")){R2=255;G2=0;B2=0;}
00129               else
00130               if(c.equalsIgnoreCase("green")){R2=0;G2=255;B2=0;}
00131               else
00132               if(c.equalsIgnoreCase("blue")){R2=0;G2=0;B2=255;}
00133               else
00134               if(c.equalsIgnoreCase("orange")){R2=238;G2=154;B2=0;}
00135               else
00136               if(c.equalsIgnoreCase("yellow")){R2=255;G2=255;B2=0;}
00137               else
00138               if(c.equalsIgnoreCase("purple")){R2=160;G2=32;B2=240;}
00139               else
00140               if(c.equalsIgnoreCase("lightgreen")){R2=144;G2=238;B2=144;}
00141               else
00142               if(c.equalsIgnoreCase("lightblue")){R2=173;G2=216;B2=230;}
00143               else
00144               if(c.equalsIgnoreCase("cyan")){R2=0;G2=255;B2=255;}
00145               else
00146               if(c.equalsIgnoreCase("brown")){R2=165;G2=24;B2=24;}
00147               else
00148               if(c.equalsIgnoreCase("salmon")){R2=250;G2=128;B2=114;}
00149               else
00150               if(c.equalsIgnoreCase("pink")){R2=255;G2=192;B2=203;}
00151               else{R2=0;G2=0;B1=255;}
00152            }
00153            else
00154            {
00155               c=c.replace(':',',');c=c.replace(';',',');
00156               StringTokenizer q = new StringTokenizer(c, ",");
00157               String k;int rgb;
00158               for( int a=0; a<3 ; a++){
00159                   k=q.nextToken();
00160                   rgb=Integer.parseInt(k, 10);
00161                   if(rgb<0){rgb=0;}
00162                   if(rgb>255){rgb=255;}
00163                   if(a==0){R2=rgb;}
00164                   if(a==1){G2=rgb;}
00165                   if(a==2){B2=rgb;}
00166               }
00167            }
00168        }
00169 
00170        c=getParameter("max_x");
00171        if(c!=null && c.length()>0){
00172            max_x = Integer.parseInt(c, 10);
00173            if(max_x>20){max_x=20;}
00174            if(max_x<1){max_x=1;}                                                                                                              
00175         }
00176        c=getParameter("max_y");
00177        if(c!=null && c.length()>0){
00178            max_y = Integer.parseInt(c, 10);
00179            if(max_y>20){max_y=20;}
00180            if(max_y<1){max_y=1;}                                                                                                              
00181         }                                                                                                                              
00182        c=getParameter("size_x");
00183        if(c!=null && c.length()>0){
00184            hokje_x = Integer.parseInt(c, 10);
00185            if(hokje_x>60){hokje_x=60;}
00186            if(hokje_x<20){hokje_x=20;}
00187         }
00188        c=getParameter("size_y");
00189        if(c!=null && c.length()>0){
00190            hokje_y = Integer.parseInt(c, 10);
00191            if(hokje_y>60){hokje_y=60;}
00192            if(hokje_y<20){hokje_y=20;}
00193         }
00194 
00195 // 21-3-2008 
00196        c=getParameter("level");
00197        if(c!=null && c.length()>0){
00198            level = Integer.parseInt(c, 10);
00199            if(level>3){level=3;}
00200            if(level<0){level=0;}
00201         }
00202        c=getParameter("percentage");
00203        if(c!=null && c.length()>0){
00204            percentage = Integer.parseInt(c, 10);
00205            if(percentage>0){percentage=1;}else{percentage=0;}
00206         }
00207 // eind
00208        xsize=hokje_x*(max_x+2);
00209        ysize=hokje_y*(max_y+2);
00210        rooster=new int[max_y+2][max_x+2];
00211        plaatje = createImage(xsize,ysize);
00212        tekening = plaatje.getGraphics();
00213        // hele veld met 0 == wit    
00214        for(int x=0;x<max_x+2;x++){;
00215            for(int y=0;y<max_y+2;y++){
00216               rooster[y][x]=0;
00217            }
00218        }
00219 
00220        c=getParameter("interactive");
00221        if(c.equals("0")){
00222            interactive=0;
00223            double p;
00224            int hit1=0;int hit2=0;
00225            if(percentage==0){
00226               for(int x=1;x<max_x+1;x++){
00227                   for(int y=1;y<max_y+1;y++){
00228                      if(level==3){p=Math.random();if(p<0.5){rooster[y][x] = -1;hit1++;}else{rooster[y][x] = 1;hit2++;}}
00229                      else
00230                      if(level==2){p=Math.random();if(p<0.7){rooster[y][x] = -1;hit1++;}else{rooster[y][x] = 1;hit2++;}}
00231                       else
00232                       if(level==1){p=Math.random();if(p<0.8){rooster[y][x] = -1;hit1++;}else{rooster[y][x] = 1;hit2++;}}
00233                       else 
00234                       if(level==0){p=Math.random();if(p<0.9){rooster[y][x] = -1;hit1++;}else{rooster[y][x] = 1;hit2++;}}
00235                   }
00236               }
00237            }
00238            else
00239            {
00240               p=Math.random();
00241               int chk=0;double perc=p*max_x*max_y;int alt=0;
00242               for(int x=1;x<max_x+1;x++){
00243                   for(int y=1;y<max_y+1;y++){
00244                      if(level<2){chk++; if(chk<perc){rooster[y][x] = -1;hit1++;}else{rooster[y][x] = 1;hit2++;}}
00245                      else
00246                      {
00247                          chk++; if(chk<perc && alt==0){rooster[y][x] = -1;hit1++;alt=1;}else{rooster[y][x] = 1;hit2++;alt=0;}
00248                      }
00249                   }
00250               }          
00251            }
00252            if(hit1==0){rooster[1][1]=-1;}
00253            if(hit2==0){rooster[1][1]=1;}
00254        }
00255        // plaatje tussen x=1 ...max_x+1 x=0/x=max_x+2===0
00256        if(interactive==1){
00257            for(int x=1;x<max_x+1;x++){;
00258               for(int y=1;y<max_y+1;y++){
00259                   rooster[y][x]=1;
00260               }
00261            }
00262            addMouseListener(
00263               new MouseAdapter(){
00264                   public void mousePressed(MouseEvent e){
00265                      dx = e.getX() / hokje_x;
00266                      dy = e.getY() / hokje_y;
00267                      if(dx<max_x+1 && dy<max_y+1 && dx>0 && dy>0){
00268                          rooster[dy][dx] = -1 * rooster[dy][dx];
00269                          //System.out.println("rooster[dy][dx]="+rooster[dy][dx]+" dx="+dx+" dy="+dy);
00270                          repaint();
00271                      }
00272                   }
00273               }
00274            );
00275        }
00276     }
00277 
00278    public void paint(Graphics g){
00279       tekening.setColor(Color.white);
00280       tekening.fillRect(0,0,xsize,ysize);
00281 
00282       for(int t1 = 0;t1 < max_x+2;t1 ++){
00283          for(int t2 = 0;t2 < max_y+2;t2 ++){
00284            if(rooster[t2][t1]!=0){
00285               if(rooster[t2][t1] == 1){tekening.setColor(new Color(R1,G1,B1));}else{tekening.setColor(new Color(R2,G2,B2));}
00286               tekening.fill3DRect(hokje_x*t1,hokje_y*t2,hokje_x,hokje_y,true);
00287             }
00288         }
00289       }
00290       g.drawImage(plaatje,0,0,this);
00291    }
00292 
00293    public void start(){
00294       if(thread == null){
00295          thread = new Thread(this);
00296          thread.start();
00297       }
00298    }
00299 
00300    public void stop(){
00301       thread =  null;
00302    }
00303 
00304    public void run(){
00305       while(thread != null){
00306          try{
00307             Thread.sleep(100);
00308          }
00309          catch (InterruptedException e){
00310          }
00311       }
00312    }
00313 
00314    public void update( Graphics g ){
00315       paint( g ) ;
00316    }
00317    
00318    public String ReadApplet(){
00319        // x==1
00320        double hy=1.000*hokje_y;
00321        double hx=1.000*hokje_x;
00322        double factor=hy/hx;
00323        //double hy=Double.valueOf(hokje_y);
00324        //double hx=Double.valueOf(hokje_x);
00325        //double factor=hy/hx;
00326         int changed=0;int original=0;int total=0;
00327         // rooster[y][x]=0 white border
00328         // rooster[y][x]=-1 clicked
00329         // rooster[y][x]=1  unchanged
00330         for(int t1 = 0;t1 < max_x+2;t1 ++){
00331            for(int t2 = 0;t2 < max_y+2;t2 ++){
00332               if(rooster[t2][t1] == -1){ changed++; total++;  }
00333               if(rooster[t2][t1] ==  1){ original++; total++; }
00334            }
00335         }
00336 
00337         double border[][]=new double[max_y+4][2*max_x+4];
00338         int c1;int c2;double t=0;
00339         for( c1 = 1; c1 < max_x+1 ;c1++){
00340            for( c2 =1; c2 < max_y+1 ;c2++){
00341               if(rooster[c2][c1] == -1 ){t=0;
00342                   if((rooster[c2][c1-1] != -1) && (rooster[c2][c1+1] != -1) ){t=t+factor*2;}else{if( (rooster[c2][c1-1] != -1) || (rooster[c2][c1+1] != -1) ){t=t+factor;}}
00343                   if( (rooster[c2-1][c1] != -1) && (rooster[c2+1][c1] != -1) ){t=t+2;}else{if( (rooster[c2-1][c1] != -1) || (rooster[c2+1][c1] != -1) ){t=t+1;}}
00344                   border[c2][c1]=t;
00345               }
00346            }
00347        }
00348        double cnt=0;
00349        for( c1=1 ; c1<max_x+1 ; c1++ ){
00350            for( c2=1 ; c2<max_y+1 ; c2++ ){
00351               cnt=cnt+border[c2][c1];
00352            }
00353        }
00354         String S=total+","+original+","+changed+","+cnt;
00355         return S;
00356     }
00357 }