Back to index

wims  3.65+svn20090927
Pento.java
Go to the documentation of this file.
00001 // no copyrights defined bij the original author:
00002 // toshio-t@tcp-ip.or.jp
00003 // added a public function ReadApplet() and some applet-params
00004 // Applet returns String S=number_of_squares+","+used_squares+","+used_objects+","+total_perimeter_of_all_objects;
00005 //<html>                                                                                                                                     
00006 //<body>                                                                                                                                     
00007 //<script language="javascript" type="text/javascript">                                                                                      
00008 //function lees(){                                                                                                                           
00009 // var input=document.applets[0].ReadApplet();                                                                                               
00010 // alert(input);                                                                                                                             
00011 // }                                                                                                                                         
00012 //</script>                                                                                                                                  
00013 //        <applet id="MathView" code="Pento.class" codebase="." archive="Pento.jar" width="640" height="480">                                                                                                                          
00014 //            <param name="objects" value="0,1:1,0,0,0,1:0,0,0,0,0:1,1,1,1,1@1,1,1,1,1:0,1,1,1,0@1,1,1:0,1,1:0,0,1@1,1,1,1@1:1:1">                                                                                                                                                                              
00015 //            <param name="helptext" value="what ever">                                                                                                                                                                              
00016 //        </applet>                                                                                                                          
00017 //        <p>                                                                                                                                
00018 //        <input type="button" name=".....TEST......" value=".....TEST....." onclick="javascript:lees();">                                   
00019 //</body>                                                                                                                                    
00020 //</html>  
00021 //
00022 import java.applet.Applet;
00023 import java.awt.*;
00024 import java.awt.event.*;
00025 import java.util.*;
00026 
00027 
00028 public class Pento extends Applet implements Runnable{
00032     private static final long serialVersionUID = -4039349014715451389L;
00033     Font small_font = new Font("Serif",Font.BOLD,16);FontMetrics small_fm = getFontMetrics(small_font);int small_height = small_fm.getHeight()+20;
00034     int collision=0;int gebruikte_vierkantjes=0;int aantal_vierkantjes=60;int used_objects=0;int teller=0;
00035     int figuur_nummer;String objects="";String helptext="";public Image plaatje;
00036     int mtd[][] = new int[100][140];
00037     int ttd[][] = new int[5][5],ttds[][] = new int[5][5];
00038     int ttn,tts = 0;
00039     int std[] = new int[12];
00040     int pd[][][] = {
00041                     {{0,0,0,0,0},{0,0,0,0,0},{1,1,1,1,1},{0,0,0,0,0},{0,0,0,0,0}},
00042                     {{0,0,0,0,0},{0,0,0,0,0},{0,1,1,1,1},{0,1,0,0,0},{0,0,0,0,0}},
00043                     {{0,0,0,0,0},{0,0,0,0,0},{0,1,1,1,1},{0,0,1,0,0},{0,0,0,0,0}},
00044                     {{0,0,0,0,0},{0,0,0,0,0},{0,0,1,1,1},{0,1,1,0,0},{0,0,0,0,0}},
00045                     {{0,0,0,0,0},{0,0,0,0,0},{0,1,1,1,0},{0,1,1,0,0},{0,0,0,0,0}},
00046                     {{0,0,0,0,0},{0,0,0,1,0},{0,0,1,1,0},{0,1,1,0,0},{0,0,0,0,0}},
00047                     {{0,0,0,0,0},{0,1,1,1,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,0,0,0}},
00048                     {{0,0,0,0,0},{0,0,0,0,0},{0,1,1,1,0},{0,1,0,1,0},{0,0,0,0,0}},
00049                     {{0,0,0,0,0},{0,0,0,1,0},{0,1,1,1,0},{0,1,0,0,0},{0,0,0,0,0}},
00050                     {{0,0,0,0,0},{0,1,1,0,0},{0,0,1,1,0},{0,0,1,0,0},{0,0,0,0,0}},
00051                     {{0,0,0,0,0},{0,0,1,0,0},{0,1,1,1,0},{0,0,1,0,0},{0,0,0,0,0}},
00052                     {{0,0,0,0,0},{0,0,0,1,0},{0,0,0,1,0},{0,1,1,1,0},{0,0,0,0,0}},
00053                   };
00054     int pdl=pd.length;
00055     Color pc[] = new Color[12],bc;
00056     int cd[][] = {{250,0,0},{0,250,0},{0,0,250},{250,250,0},{250,0,250},{0,250,250},{150,100,0},{150,0,100},{100,150,0},{100,0,150},{0,100,150},{0,150,100}};
00057     int dx,dy,pn;
00058     int mx,my,mcx,mcy;
00059     Thread th = null;
00060     Image os;
00061     Graphics og;
00062 
00063    public void init(){
00064        plaatje = getImage(getCodeBase(), "draai.gif"); 
00065       setBackground(Color.white);
00066       os = createImage(680,420);
00067       og = os.getGraphics();
00068       for(int j1 = 0;j1 < 14;j1 ++){
00069          mtd[0][j1] = -1;mtd[1][j1] = -1;mtd[8][j1] = -1;mtd[9][j1] = -1;
00070       }
00071       for(int j2 = 0;j2 < 10;j2 ++){
00072          mtd[j2][0] = -1;mtd[j2][1] = -1;mtd[j2][12] = -1;mtd[j2][13] = -1;
00073       }
00074       for(int k = 0;k < 12;k ++){
00075          std[k] = 1;
00076          pc[k] = new Color(cd[k][0],cd[k][1],cd[k][2]);
00077       }
00078       bc = new Color(225,225,225);
00079 
00080       addMouseListener(
00081          new MouseAdapter(){
00082             public void mousePressed(MouseEvent e){
00083                dx = e.getX() ; dy = e.getY() ;
00084 
00085                if((dx > 40) && (dx < 640) && (dy > 260) && (dy < 420)){
00086                   pn = ((dx - 40) / 100) + ((dy - 260) / 80) * 6;
00087 
00088                   if((std[pn] == 1)){
00089                      if(tts == 0){
00090                         tts = 1 ; ttn = pn ; std[pn] = 0;
00091                         D_in(pn);
00092                      }else{
00093                         std[ttn] = 1 ; ttn = pn ; std[pn] = 0;
00094                         D_in(pn);
00095                      }
00096                   }else{
00097                      if(tts == 1){
00098                         std[ttn] = 1 ; tts = 0 ;
00099                         D_in(-1);
00100                      }
00101                   }
00102                   repaint();
00103                }
00104 
00105                if((dx > 440) && (dx < 640) && (dy > 20) && (dy < 220)){
00106                   if(tts == 1){
00107                      for(int f1 = 0;f1 < 5;f1 ++){
00108                         for(int f2 = 0;f2 < 5;f2 ++){
00109                            ttds[f1][4 - f2] = ttd[f2][f1];
00110                         }
00111                      }
00112                      for(int g1 = 0;g1 < 5;g1 ++){
00113                         for(int g2 = 0;g2 < 5;g2 ++){
00114                            ttd[g2][g1] = ttds[g2][g1];
00115                         }
00116                      }
00117                   repaint();
00118                   }
00119                }
00120               collision=0;
00121                if((dx > 20) && (dx < 420) && (dy > 20) && (dy < 260)){
00122                    dx = (e.getX() - 20) / 40 ; dy = (e.getY() -20) / 40;
00123                    if(tts == 1){
00124                       C_dp();
00125                       repaint();
00126                    }else{
00127                       if(mtd[dy + 2][dx + 2]  > 0){
00128                          ttn = mtd[dy + 2][dx + 2] - 1;
00129                          tts = 1;
00130                          B_dp();
00131                          repaint();
00132                       }
00133                    }
00134                 }
00135             }         
00136          }
00137       );
00138 
00139       addMouseMotionListener(
00140          new MouseMotionAdapter(){
00141             public void mouseMoved(MouseEvent e){
00142                mx = (e.getX() - 20) / 40 ; my = (e.getY() -20) / 40;
00143                if((mx > -1) && (mx < 10) && (my > -1) && (my < 6)){
00144                   if((mx != mcx) || (my != mcy)){
00145                      mcx = mx;mcy = my;
00146                      repaint();
00147                   }
00148                }
00149             }
00150          }
00151       );
00152 
00153    }
00154 
00155    public void paint(Graphics g){
00156        objects=getParameter("objects");                                                                                                   
00157         if(objects!=null && objects.length()>0){                                                                                           
00158             pd = StringToIntArray(objects);                                                                                                
00159             pdl=pd.length;                                                                                                                 
00160         }   
00161       og.setColor(Color.white); 
00162       og.fillRect(0,0,680,420);
00163       gebruikte_vierkantjes=0;
00164       for(int lx = 0;lx < 10;lx ++){
00165          for(int ly = 0;ly < 6;ly ++){
00166             if(mtd[ly + 2][lx + 2] == 0){
00167                og.setColor(bc);                
00168             }else{
00169                og.setColor(pc[mtd[ly + 2][lx + 2] - 1]);
00170               gebruikte_vierkantjes++;   
00171             }
00172             og.fillRect(lx * 40 + 20,ly * 40 + 20,40,40);
00173             og.setColor(Color.blue);
00174             og.draw3DRect(lx * 40 + 20,ly * 40 + 20,40,40,true);
00175          }
00176       }
00177 
00178       for(int tx = 0;tx < 5;tx ++){
00179          for(int ty = 0;ty < 5;ty ++){
00180             if(ttd[ty][tx] == 0){
00181                og.setColor(bc);
00182             }else{
00183                og.setColor(pc[ttn]);
00184             }
00185             og.fillRect(tx * 40 + 440,ty * 40 + 60,40,40);
00186             og.setColor(Color.blue);
00187             og.draw3DRect(tx * 40 + 440,ty * 40 + 60,40,40,true);
00188          }
00189       }
00190 
00191       for(int sn = 0;sn < pdl ;sn ++){
00192          if(std[sn] == 1){
00193             for(int d1 = 0;d1 < 5;d1 ++){
00194                for(int d2 = 0;d2 < 5;d2 ++){
00195                   if(pd[sn][d2][d1] == 1){
00196                      og.setColor(pc[sn]);
00197                      og.fillRect(d1 * 20 + (sn % 6) * 100 + 40,d2 * 20 + (sn / 6) * 80 + 240,20,20);
00198                      og.setColor(Color.black);
00199                      og.draw3DRect(d1 * 20 + (sn % 6) * 100 + 40,d2 * 20 + (sn / 6) * 80 + 240,20,20,true);
00200                   }
00201                }
00202             }
00203          }
00204       }
00205 
00206       if((tts == 1) && (mx > -1) && (mx < 10) && (my > -1) && (my < 6)){
00207          for(int q1 = 0;q1 < 5;q1 ++){
00208             for(int q2 = 0;q2 < 5;q2 ++){
00209                if(ttd[q2][q1] == 1){
00210                   if(((mx + q1 - 2) > -1) && ((mx + q1 - 2) < 10) && ((my + q2 - 2) > -1) && ((my + q2 - 2) < 6)){
00211                   if(collision==0){og.setColor(Color.lightGray);}else{og.setColor(Color.gray);}
00212                      og.drawRect(40 * (mx + q1 - 2) + 20,40 * (my + q2 - 2) + 20,40,40);
00213                      og.fillRect(40 * (mx + q1 - 2) + 20,40 * (my + q2 - 2) + 20,40,40);
00214                   }
00215                }
00216             }
00217          }   
00218       }
00219       g.drawImage(os,0,0,this );
00220       helptext=getParameter("helptext");
00221       if(helptext!=null && helptext.length()>0){og.setColor(Color.black);g.drawString(helptext,20,small_height);}
00222       g.drawImage(plaatje, 500, 0, this);
00223  
00224    }
00227 
00228     public int[][][] StringToIntArray(String tmp0){
00229        aantal_vierkantjes=0;
00230        int array[][][] = new int[12][5][5];String tmp1="0";String tmp2="0";String tmp3="0";
00231        int a = 0;int b = 0;int c = 0;
00232        StringTokenizer q = new StringTokenizer(tmp0, "@");
00233        int ma=q.countTokens();int mb=0;int mc=0;
00234        if( ma > 12 ){ ma = 12;}
00235        for( a = 0 ; a<ma ; a++){
00236            tmp1=q.nextToken();
00237            StringTokenizer r = new StringTokenizer(tmp1,":");
00238            mb=r.countTokens();
00239            for(b = 0 ; b<5 ; b++){
00240               if ( b < mb ){ tmp2=r.nextToken(); } else { tmp2="0,0,0,0,0";}
00241                   StringTokenizer t = new StringTokenizer(tmp2,",");
00242                   mc=t.countTokens();
00243                   for ( c = 0 ; c<5 ; c++){
00244                      if( c < mc){ tmp3=t.nextToken(); if(tmp3.equals("1")){ array[a][b][c]=1;aantal_vierkantjes++; } else { array[a][b][c]=0;}}
00245                      else { array[a][b][c]=0; }
00246                   }
00247               }
00248            }
00249        return array;                                                                                                                      
00250     } 
00251 
00253 
00255    public void D_in(int nn){
00256       for(int k1 = 0;k1 < 5;k1 ++){
00257          for(int k2 = 0;k2 < 5;k2 ++){
00258             if(nn == -1){
00259                ttd[k2][k1] = 0;
00260             }else{
00261                ttd[k2][k1] = pd[nn][k2][k1];
00262             }
00263          }
00264       }
00265    }
00266 
00267    public void C_dp(){
00268       int fg = 1;collision=0;
00269       for(int c1 = 0;c1 < 5;c1 ++){
00270          for(int c2 = 0;c2 < 5;c2 ++){
00271             if((ttd[c2][c1] == 1) && (mtd[dy + c2][dx + c1] != 0)){
00272                fg = 0;collision=1;
00273             }
00274          }
00275       }
00276       if(fg == 1){
00277         used_objects++;
00278          for(int d1 = 0;d1 < 5;d1 ++){
00279             for(int d2 = 0;d2 < 5;d2 ++){
00280                if(ttd[d2][d1] == 1){
00281                   mtd[dy + d2][dx + d1] = ttn + 1;
00282                }
00283             }
00284          }
00285          D_in(-1);
00286          tts = 0;ttn = 0;
00287       }
00288    }
00289 
00290    public void B_dp(){
00291       for(int p1 = 0;p1 < 10;p1 ++){
00292          for(int p2 = 0;p2 < 6;p2 ++){
00293             if(mtd[p2 + 2][p1 + 2] == ttn + 1){
00294                mtd[p2 + 2][p1 + 2] = 0;
00295             }
00296          }
00297       }
00298       for(int p3 = 0;p3 < 5;p3 ++){
00299          for(int p4 = 0;p4 < 5;p4 ++){
00300             ttd[p4][p3] = pd[ttn][p4][p3];
00301          }
00302       }
00303       used_objects--;
00304    }
00305     public void start(){    
00306         if(th == null){
00307             th = new Thread(this);
00308             th.start();
00309         }
00310     }
00311 
00312     public void stop(){
00313         th = null;
00314     }
00315 
00316     public void run(){
00317         while (th != null){
00318             try {
00319                 Thread.sleep(20);
00320             }
00321             catch (InterruptedException e){ }
00322         }
00323     }
00324     public void update( Graphics g )
00325     {
00326         paint( g ) ;
00327     }
00328 
00329     //  leest applet met javascript...bereken de omtrek...
00330     public String ReadApplet(){
00331        int border[][]=new int[20][20];
00332        int c1;int c2;int t=0;int r=0;
00333        // c1 vertikaal van boven->beneden   c2 horizontaal van links naar rechts
00334        for( c1 = 0; c1 < 20 ;c1++){
00335            for( c2 = 0; c2 < 20 ;c2++){
00336               if(mtd[c2][c1] > 0){t=0;
00337                   if( (mtd[c2][c1-1] < 1) && (mtd[c2][c1+1] < 1) ){t=t+2;}else{if( (mtd[c2][c1-1] < 1) || (mtd[c2][c1+1] < 1) ){t=t+1;}}           
00338                   if( (mtd[c2-1][c1] < 1) && (mtd[c2+1][c1] < 1) ){t=t+2;}else{if( (mtd[c2-1][c1] < 1) || (mtd[c2+1][c1] < 1) ){t=t+1;}}    
00339                   border[c2][c1]=t; 
00340               }
00341            } 
00342            for( c2 = 20; c2 < 0 ;c2--){
00343               if(mtd[c2][c1] > 0){r=0;
00344                   if( (mtd[c2][c1-1] < 1) && (mtd[c2][c1+1] < 1) ){r=r+2;}else{if( (mtd[c2][c1-1] < 1) || ( mtd[c2][c1+1] < 1) ){r=r+1;}}          
00345                   if( (mtd[c2-1][c1] < 1) && (mtd[c2+1][c1] < 1) ){r=r+2;}else{if( (mtd[c2-1][c1] < 1) || ( mtd[c2+1][c1] < 1) ){r=r+1;}}   
00346                   border[c2][c1]=r+border[c2][c1];    
00347               }
00348            }
00349        }
00350        int cnt=0;
00351        for( c1=0 ; c1<20 ; c1++ ){
00352            for( c2=0 ; c2<20 ; c2++ ){
00353               cnt=cnt+border[c2][c1];
00354            }
00355        }
00356        //String S="total_squares="+aantal_vierkantjes+",used_squares="+gebruikte_vierkantjes+"used_objects="+used_objects+",perimeter="+cnt;
00357        String S=aantal_vierkantjes+","+gebruikte_vierkantjes+","+used_objects+","+cnt;
00358         return S;
00359     }          
00360 // einde  "public String ReadApplet()"
00361     public int readparm(String S){
00362        int s=0;String c="";
00363        if(S.equals("interactive")){
00364            s=1;c=getParameter(S);if(c!=null && c.length()>0){ if (c.equals("0")){s = 0;}}
00365        }
00366         if(S.equals("cols")){
00367            s=10;c=getParameter("cols");
00368            if(c!=null && c.length()>0){
00369               s = Integer.parseInt(c, 10); 
00370               if(s>10){s=10;}
00371               if(s<3){s=3;}
00372            }
00373        }
00374        if(S.equals("rows")){
00375            s=6;c=getParameter("rows");
00376            if(c!=null && c.length()>0){
00377               s = Integer.parseInt(c, 10); 
00378               if(s>6){s=6;}
00379               if(s<3){s=3;}
00380            }
00381        }
00382        return s;
00383     }
00384 }
00385