Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  1.  
  2. /*
  3. *********************************************************************************
  4. * J.M. Evers 19/11/2009                                                         *
  5. * This is all amateur scriblings... So no copyrights.                           *
  6. * This source code file, and compiled classes derived from it,                  *
  7. * can be used and distributed without restriction, including for commercial use *
  8. * No warrenty whatoever                                                         *
  9. *********************************************************************************
  10. added a public function ReadApplet() and some applet-params
  11. Applet returns String S=number_of_squares+","+used_squares+","+used_objects+","+total_perimeter_of_all_objects;
  12. <html>
  13.     <body>
  14.     <script language="javascript" type="text/javascript">
  15.             function lees(){
  16.                 var input=document.applets[0].ReadApplet();
  17.                 alert(input);
  18.             }
  19.         </script>
  20.         <applet id="MathView" code="Pento.class" codebase="." archive="Pento.jar" width="640" height="480">
  21.             <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">                                                                                                                                                                              
  22.             <param name="helptext" value="what ever">
  23.         </applet>
  24.         <p>
  25.         <input type="button" name=".....TEST......" value=".....TEST....." onclick="javascript:lees();">
  26.     </body>
  27. </html>
  28. */
  29.  
  30. import java.applet.Applet;
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import java.util.*;
  34.  
  35. public class Pento extends Applet implements Runnable{
  36.     private static final long serialVersionUID = -4039349014715451389L;
  37.     Font small_font = new Font("Serif",Font.BOLD,16);FontMetrics small_fm = getFontMetrics(small_font);int small_height = small_fm.getHeight()+20;
  38.     int collision=0;int gebruikte_vierkantjes=0;int aantal_vierkantjes=60;int used_objects=0;int teller=0;
  39.     int figuur_nummer;String objects="";String helptext="";public Image plaatje;
  40.     int mtd[][] = new int[100][140];
  41.     int ttd[][] = new int[5][5],ttds[][] = new int[5][5];
  42.     int ttn,tts = 0;
  43.     int std[] = new int[12];
  44.     int pd[][][] = {
  45.                     {{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}},
  46.                     {{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}},
  47.                     {{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}},
  48.                     {{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}},
  49.                     {{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}},
  50.                     {{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}},
  51.                     {{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}},
  52.                     {{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}},
  53.                     {{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}},
  54.                     {{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}},
  55.                     {{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}},
  56.                     {{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}},
  57.                   };
  58.     int pdl=pd.length;
  59.     Color pc[] = new Color[12],bc;
  60.     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}};
  61.     int dx,dy,pn;
  62.     int mx,my,mcx,mcy;
  63.     Thread th = null;
  64.     Image os;
  65.     Graphics og;
  66.  
  67.    public void init(){
  68.         plaatje = getImage(getCodeBase(), "draai.gif");
  69.       setBackground(Color.white);
  70.       os = createImage(680,420);
  71.       og = os.getGraphics();
  72.       for(int j1 = 0;j1 < 14;j1 ++){
  73.          mtd[0][j1] = -1;mtd[1][j1] = -1;mtd[8][j1] = -1;mtd[9][j1] = -1;
  74.       }
  75.       for(int j2 = 0;j2 < 10;j2 ++){
  76.          mtd[j2][0] = -1;mtd[j2][1] = -1;mtd[j2][12] = -1;mtd[j2][13] = -1;
  77.       }
  78.       for(int k = 0;k < 12;k ++){
  79.          std[k] = 1;
  80.          pc[k] = new Color(cd[k][0],cd[k][1],cd[k][2]);
  81.       }
  82.       bc = new Color(225,225,225);
  83.  
  84.       addMouseListener(
  85.          new MouseAdapter(){
  86.             public void mousePressed(MouseEvent e){
  87.                dx = e.getX() ; dy = e.getY() ;
  88.  
  89.                if((dx > 40) && (dx < 640) && (dy > 260) && (dy < 420)){
  90.                   pn = ((dx - 40) / 100) + ((dy - 260) / 80) * 6;
  91.  
  92.                   if((std[pn] == 1)){
  93.                      if(tts == 0){
  94.                         tts = 1 ; ttn = pn ; std[pn] = 0;
  95.                         D_in(pn);
  96.                      }else{
  97.                         std[ttn] = 1 ; ttn = pn ; std[pn] = 0;
  98.                         D_in(pn);
  99.                      }
  100.                   }else{
  101.                      if(tts == 1){
  102.                         std[ttn] = 1 ; tts = 0 ;
  103.                         D_in(-1);
  104.                      }
  105.                   }
  106.                   repaint();
  107.                }
  108.  
  109.                if((dx > 440) && (dx < 640) && (dy > 20) && (dy < 220)){
  110.                   if(tts == 1){
  111.                      for(int f1 = 0;f1 < 5;f1 ++){
  112.                         for(int f2 = 0;f2 < 5;f2 ++){
  113.                            ttds[f1][4 - f2] = ttd[f2][f1];
  114.                         }
  115.                      }
  116.                      for(int g1 = 0;g1 < 5;g1 ++){
  117.                         for(int g2 = 0;g2 < 5;g2 ++){
  118.                            ttd[g2][g1] = ttds[g2][g1];
  119.                         }
  120.                      }
  121.                   repaint();
  122.                   }
  123.                }
  124.                 collision=0;
  125.                if((dx > 20) && (dx < 420) && (dy > 20) && (dy < 260)){
  126.                    dx = (e.getX() - 20) / 40 ; dy = (e.getY() -20) / 40;
  127.                    if(tts == 1){
  128.                       C_dp();
  129.                       repaint();
  130.                    }else{
  131.                       if(mtd[dy + 2][dx + 2]  > 0){
  132.                          ttn = mtd[dy + 2][dx + 2] - 1;
  133.                          tts = 1;
  134.                          B_dp();
  135.                          repaint();
  136.                       }
  137.                    }
  138.                 }
  139.             }        
  140.          }
  141.       );
  142.  
  143.       addMouseMotionListener(
  144.          new MouseMotionAdapter(){
  145.             public void mouseMoved(MouseEvent e){
  146.                mx = (e.getX() - 20) / 40 ; my = (e.getY() -20) / 40;
  147.                if((mx > -1) && (mx < 10) && (my > -1) && (my < 6)){
  148.                   if((mx != mcx) || (my != mcy)){
  149.                      mcx = mx;mcy = my;
  150.                      repaint();
  151.                   }
  152.                }
  153.             }
  154.          }
  155.       );
  156.  
  157.    }
  158.  
  159.    public void paint(Graphics g){
  160.         objects=getParameter("objects");                                                                                                  
  161.         if(objects!=null && objects.length()>0){                                                                                          
  162.             pd = StringToIntArray(objects);                                                                                                
  163.             pdl=pd.length;                                                                                                                
  164.         }  
  165.       og.setColor(Color.white);
  166.       og.fillRect(0,0,680,420);
  167.       gebruikte_vierkantjes=0;
  168.       for(int lx = 0;lx < 10;lx ++){
  169.          for(int ly = 0;ly < 6;ly ++){
  170.             if(mtd[ly + 2][lx + 2] == 0){
  171.                og.setColor(bc);              
  172.             }else{
  173.                og.setColor(pc[mtd[ly + 2][lx + 2] - 1]);
  174.                gebruikte_vierkantjes++;  
  175.             }
  176.             og.fillRect(lx * 40 + 20,ly * 40 + 20,40,40);
  177.             og.setColor(Color.blue);
  178.             og.draw3DRect(lx * 40 + 20,ly * 40 + 20,40,40,true);
  179.          }
  180.       }
  181.  
  182.       for(int tx = 0;tx < 5;tx ++){
  183.          for(int ty = 0;ty < 5;ty ++){
  184.             if(ttd[ty][tx] == 0){
  185.                og.setColor(bc);
  186.             }else{
  187.                og.setColor(pc[ttn]);
  188.             }
  189.             og.fillRect(tx * 40 + 440,ty * 40 + 60,40,40);
  190.             og.setColor(Color.blue);
  191.             og.draw3DRect(tx * 40 + 440,ty * 40 + 60,40,40,true);
  192.          }
  193.       }
  194.  
  195.       for(int sn = 0;sn < pdl ;sn ++){
  196.          if(std[sn] == 1){
  197.             for(int d1 = 0;d1 < 5;d1 ++){
  198.                for(int d2 = 0;d2 < 5;d2 ++){
  199.                   if(pd[sn][d2][d1] == 1){
  200.                      og.setColor(pc[sn]);
  201.                      og.fillRect(d1 * 20 + (sn % 6) * 100 + 40,d2 * 20 + (sn / 6) * 80 + 240,20,20);
  202.                      og.setColor(Color.black);
  203.                      og.draw3DRect(d1 * 20 + (sn % 6) * 100 + 40,d2 * 20 + (sn / 6) * 80 + 240,20,20,true);
  204.                   }
  205.                }
  206.             }
  207.          }
  208.       }
  209.  
  210.       if((tts == 1) && (mx > -1) && (mx < 10) && (my > -1) && (my < 6)){
  211.          for(int q1 = 0;q1 < 5;q1 ++){
  212.             for(int q2 = 0;q2 < 5;q2 ++){
  213.                if(ttd[q2][q1] == 1){
  214.                   if(((mx + q1 - 2) > -1) && ((mx + q1 - 2) < 10) && ((my + q2 - 2) > -1) && ((my + q2 - 2) < 6)){
  215.                     if(collision==0){og.setColor(Color.lightGray);}else{og.setColor(Color.gray);}
  216.                      og.drawRect(40 * (mx + q1 - 2) + 20,40 * (my + q2 - 2) + 20,40,40);
  217.                      og.fillRect(40 * (mx + q1 - 2) + 20,40 * (my + q2 - 2) + 20,40,40);
  218.                   }
  219.                }
  220.             }
  221.          }  
  222.       }
  223.       g.drawImage(os,0,0,this );
  224.       helptext=getParameter("helptext");
  225.       if(helptext!=null && helptext.length()>0){og.setColor(Color.black);g.drawString(helptext,20,small_height);}
  226.       g.drawImage(plaatje, 500, 0, this);
  227.  
  228.    }
  229. //// einde graphics
  230. ///// converteer applet param String naar Array:  0,0,0,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
  231.  
  232.     public int[][][] StringToIntArray(String tmp0){
  233.         aantal_vierkantjes=0;
  234.         int array[][][] = new int[12][5][5];String tmp1="0";String tmp2="0";String tmp3="0";
  235.         int a = 0;int b = 0;int c = 0;
  236.         StringTokenizer q = new StringTokenizer(tmp0, "@");
  237.         int ma=q.countTokens();int mb=0;int mc=0;
  238.         if( ma > 12 ){ ma = 12;}
  239.         for( a = 0 ; a<ma ; a++){
  240.             tmp1=q.nextToken();
  241.             StringTokenizer r = new StringTokenizer(tmp1,":");
  242.             mb=r.countTokens();
  243.             for(b = 0 ; b<5 ; b++){
  244.                 if ( b < mb ){ tmp2=r.nextToken(); } else { tmp2="0,0,0,0,0";}
  245.                     StringTokenizer t = new StringTokenizer(tmp2,",");
  246.                     mc=t.countTokens();
  247.                     for ( c = 0 ; c<5 ; c++){
  248.                         if( c < mc){ tmp3=t.nextToken(); if(tmp3.equals("1")){ array[a][b][c]=1;aantal_vierkantjes++; } else { array[a][b][c]=0;}}
  249.                         else { array[a][b][c]=0; }
  250.                     }
  251.                 }
  252.             }
  253.         return array;                                                                                                                      
  254.     }
  255.  
  256. //// einde "converteer applet param String naar Array"
  257.  
  258. ///
  259.    public void D_in(int nn){
  260.       for(int k1 = 0;k1 < 5;k1 ++){
  261.          for(int k2 = 0;k2 < 5;k2 ++){
  262.             if(nn == -1){
  263.                ttd[k2][k1] = 0;
  264.             }else{
  265.                ttd[k2][k1] = pd[nn][k2][k1];
  266.             }
  267.          }
  268.       }
  269.    }
  270.  
  271.    public void C_dp(){
  272.       int fg = 1;collision=0;
  273.       for(int c1 = 0;c1 < 5;c1 ++){
  274.          for(int c2 = 0;c2 < 5;c2 ++){
  275.             if((ttd[c2][c1] == 1) && (mtd[dy + c2][dx + c1] != 0)){
  276.                fg = 0;collision=1;
  277.             }
  278.          }
  279.       }
  280.       if(fg == 1){
  281.         used_objects++;
  282.          for(int d1 = 0;d1 < 5;d1 ++){
  283.             for(int d2 = 0;d2 < 5;d2 ++){
  284.                if(ttd[d2][d1] == 1){
  285.                   mtd[dy + d2][dx + d1] = ttn + 1;
  286.                }
  287.             }
  288.          }
  289.          D_in(-1);
  290.          tts = 0;ttn = 0;
  291.       }
  292.    }
  293.  
  294.    public void B_dp(){
  295.       for(int p1 = 0;p1 < 10;p1 ++){
  296.          for(int p2 = 0;p2 < 6;p2 ++){
  297.             if(mtd[p2 + 2][p1 + 2] == ttn + 1){
  298.                mtd[p2 + 2][p1 + 2] = 0;
  299.             }
  300.          }
  301.       }
  302.       for(int p3 = 0;p3 < 5;p3 ++){
  303.          for(int p4 = 0;p4 < 5;p4 ++){
  304.             ttd[p4][p3] = pd[ttn][p4][p3];
  305.          }
  306.       }
  307.       used_objects--;
  308.    }
  309.     public void start(){    
  310.         if(th == null){
  311.             th = new Thread(this);
  312.             th.start();
  313.         }
  314.     }
  315.  
  316.     public void stop(){
  317.         th = null;
  318.     }
  319.  
  320.     public void run(){
  321.         while (th != null){
  322.             try {
  323.                 Thread.sleep(20);
  324.             }
  325.             catch (InterruptedException e){ }
  326.         }
  327.     }
  328.     public void update( Graphics g )
  329.     {
  330.         paint( g ) ;
  331.     }
  332.  
  333.     //  leest applet met javascript...bereken de omtrek...
  334.     public String ReadApplet(){
  335.         int border[][]=new int[20][20];
  336.         int c1;int c2;int t=0;int r=0;
  337.         // c1 vertikaal van boven->beneden   c2 horizontaal van links naar rechts
  338.         for( c1 = 0; c1 < 20 ;c1++){
  339.             for( c2 = 0; c2 < 20 ;c2++){
  340.                 if(mtd[c2][c1] > 0){t=0;
  341.                     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;}}             
  342.                     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;}}     
  343.                     border[c2][c1]=t;
  344.                 }
  345.             }
  346.             for( c2 = 20; c2 < 0 ;c2--){
  347.                 if(mtd[c2][c1] > 0){r=0;
  348.                     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;}}            
  349.                     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;}}    
  350.                     border[c2][c1]=r+border[c2][c1];    
  351.                 }
  352.             }
  353.         }
  354.         int cnt=0;
  355.         for( c1=0 ; c1<20 ; c1++ ){
  356.             for( c2=0 ; c2<20 ; c2++ ){
  357.                 cnt=cnt+border[c2][c1];
  358.             }
  359.         }
  360.         //String S="total_squares="+aantal_vierkantjes+",used_squares="+gebruikte_vierkantjes+"used_objects="+used_objects+",perimeter="+cnt;
  361.         String S=aantal_vierkantjes+","+gebruikte_vierkantjes+","+used_objects+","+cnt;
  362.         return S;
  363.     }          
  364. // einde  "public String ReadApplet()"
  365.     public int readparm(String S){
  366.         int s=0;String c="";
  367.         if(S.equals("interactive")){
  368.             s=1;c=getParameter(S);if(c!=null && c.length()>0){ if (c.equals("0")){s = 0;}}
  369.         }
  370.         if(S.equals("cols")){
  371.             s=10;c=getParameter("cols");
  372.             if(c!=null && c.length()>0){
  373.                 s = Integer.parseInt(c, 10);
  374.                 if(s>10){s=10;}
  375.                 if(s<3){s=3;}
  376.             }
  377.         }
  378.         if(S.equals("rows")){
  379.             s=6;c=getParameter("rows");
  380.             if(c!=null && c.length()>0){
  381.                 s = Integer.parseInt(c, 10);
  382.                 if(s>6){s=6;}
  383.                 if(s<3){s=3;}
  384.             }
  385.         }
  386.         return s;
  387.     }
  388. }
  389.  
  390.