home *** CD-ROM | disk | FTP | other *** search
/ PCMania 2 / Pcmania_Ep2_02_CD-01.iso / ARTICULOS / CREATIVIDAD / DEMOSCENE / JAVA / agua.java < prev    next >
Encoding:
Java Source  |  1999-01-01  |  5.9 KB  |  217 lines

  1. import java.awt.image.*;
  2. import java.awt.Image;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Event;
  6.  
  7. public class agua extends java.applet.Applet implements Runnable {
  8.  
  9.     Thread runner;
  10.  
  11.     Image imagen1;
  12.     Image imagen2;
  13.             
  14.     byte PalR [];
  15.     byte PalG [];
  16.     byte PalB [];
  17.  
  18.     byte pant [][];
  19.     float cnt,vel;
  20.     
  21.     int puntos [];
  22.     int maxPuntos = 0;
  23.  
  24.     int ancho,alto,c,tam,gotas,Rval,Gval,Bval,delay;
  25.     
  26.     String st;
  27.     
  28.     boolean imagenBien;
  29.     
  30.     public void init () {
  31.     
  32.         int textMap[];
  33.         
  34.         ancho = this.getBounds().width;
  35.         alto = this.getBounds().height;
  36.         tam = ancho*alto;
  37.         
  38.         st = getParameter ("gotas");
  39.         if (st == null) gotas = 1;
  40.         else gotas = Integer.valueOf (st).intValue();
  41.         
  42.         st = getParameter ("vel");
  43.         if (st == null) vel = 1;
  44.         else vel = Float.valueOf (st).floatValue();
  45.         
  46.         st = getParameter ("Rval");
  47.         if (st == null) Rval = 1;
  48.         else Rval = Integer.valueOf(st).intValue();
  49.                 
  50.         st = getParameter ("Gval");
  51.         if (st == null) Rval = 1;
  52.         else Gval = Integer.valueOf(st).intValue();
  53.  
  54.         st = getParameter ("Bval");
  55.         if (st == null) Rval = 1;
  56.         else Bval = Integer.valueOf(st).intValue();
  57.         
  58.         st = getParameter ("imagen");
  59.         if (st == null) {
  60.             imagenBien = false;
  61.             System.err.println ("No se ha especificado una imagen");
  62.         }
  63.         else {
  64.               try {
  65.                Image textImg = getImage(getDocumentBase(),st);
  66.                
  67.                st = getParameter ("delay");
  68.                if (st == null) delay = 150;
  69.                else delay = Integer.valueOf (st).intValue();
  70.                
  71.                while (textImg.getWidth(null) == -1);
  72.                int width  = textImg.getWidth(null);
  73.                int height = textImg.getHeight(null);
  74.             
  75.                if (width == ancho && height == alto) {
  76.                      textMap = new int[width * height];
  77.                     PixelGrabber pg = new PixelGrabber(textImg, 0, 0, width, height, textMap, 0, width);
  78.                     pg.grabPixels();
  79.                     if ((pg.status() & ImageObserver.ABORT) != 0 ) {
  80.                         System.err.println("Error al cargar los pφxeles de la imagen.");
  81.                         imagenBien = false;
  82.                     }
  83.                    else {
  84.                         for (int cont = 0; cont < tam; cont++) if (textMap [cont] != -1) maxPuntos++;
  85.                         
  86.                         puntos = new int [maxPuntos];
  87.                         
  88.                         int p = 0;
  89.                         int cont = 0;
  90.                         
  91.                         while (p < maxPuntos) {
  92.                             if (textMap [cont] != -1) {
  93.                                 puntos[p] = cont;
  94.                                 p++;
  95.                             }
  96.                             cont++;
  97.                         }
  98.                         imagenBien = true;
  99.                     }
  100.                }
  101.                else {
  102.                    System.err.println ("Error, tama±o del applet distinto de tama±o de la imagen");
  103.                    System.err.println ("Applet : "+ancho+"X"+alto);
  104.                    System.err.println ("Imagen : "+width+"X"+height);
  105.                     imagenBien = false;
  106.                }
  107.             }
  108.             catch (Exception e) {
  109.                 System.out.println("Error al cargar la imagen :  " + e);
  110.                 System.out.println("maxPuntos="+maxPuntos);
  111.                 imagenBien = false;
  112.             }
  113.         }
  114.         //Creamos la paleta;
  115.         PalR = new byte [256];
  116.         PalG = new byte [256];
  117.         PalB = new byte [256];
  118.         
  119.         for (int cont = 0; cont < 256; cont++) {
  120.             if (Rval != 0) PalR [cont] = (byte) (cont+Rval);
  121.             if (Gval != 0) PalG [cont] = (byte) (cont+Gval);
  122.             if (Bval != 0) PalB [cont] = (byte) (cont+Bval);
  123.         }
  124.         
  125.         //Creamos las    pantallas auxiliares
  126.         
  127.         pant = new byte [2][tam]; //definimos 2 pantallas del tama±o del applet
  128.         
  129.         //Definimos las imßgenes
  130.         imagen1 = createImage (new MemoryImageSource (ancho,alto,new IndexColorModel(8,256,PalR,PalG,PalB),pant[0],0,ancho));
  131.         imagen2 = createImage (new MemoryImageSource (ancho,alto,new IndexColorModel(8,256,PalR,PalG,PalB),pant[1],0,ancho));
  132.         
  133.         for (int cont = 0; cont < tam; cont++) {
  134.             pant [0][cont] = 64;
  135.             pant [1][cont] = 64;
  136.         }
  137.         c = 0;
  138.     }
  139.  
  140.     public void start () {
  141.         if (runner == null) {
  142.             runner = new Thread(this);
  143.             runner.start ();
  144.         }
  145.     }
  146.     
  147.     public void stop () {
  148.         if (runner != null) {
  149.             runner.stop ();
  150.             runner = null;
  151.         }
  152.     }
  153.  
  154.     public void run () {
  155.         int opc = 0;
  156.         
  157.         while (true) {
  158.             repaint ();
  159.             //Lluvia y rastro en el agua;
  160.             int offset1 = (int) (ancho/4*Math.sin(cnt/15));
  161.             int offset2 = (int) (alto/4*Math.sin(cnt/21));
  162.             pant[c][(int)(((tam-ancho)/2)+offset1+ancho*offset2)] = 127;
  163.             cnt+=vel;
  164.             for (int cont = 1; cont < gotas; cont ++) pant[c][random(tam-2*ancho)+ancho] = 127;
  165.             
  166.             //Si el parßmetro de la imagen estß y no ha habido fallos cargamos los puntos en la pantalla
  167.             if (imagenBien) {
  168.                 
  169.                 if (opc == delay) {
  170.                     for (int cont = 1; cont < maxPuntos; cont ++) pant[c][puntos[cont]] = 127;
  171.                     opc = 0;
  172.                 }
  173.                 else opc++;
  174.             }
  175.             
  176.             try {Thread.sleep (25);} catch (InterruptedException e) {}
  177.         }
  178.     }
  179.  
  180.     private void water (int c,int c1) {
  181.         for (int cont = ancho; cont < tam-ancho; cont++) {
  182.             int temp = pant[c][cont-ancho]+pant[c][cont-1]+pant[c][cont+1]+pant[c][cont+ancho]-256;
  183.             temp = (temp>>1)-pant[c1][cont]+64;
  184.             if (temp < 0 ) temp = 0; if (temp > 127) temp = (byte)255;
  185.             pant[c1][cont]=(byte)(64+temp-(temp>>7));
  186.         }
  187.     }
  188.     
  189.     private int random (int num) {
  190.         int    sol = (int)(Math.floor(Math.random()*num));
  191.         return sol;
  192.     }
  193.     
  194.     public void update (Graphics g) {
  195.         paint (g);
  196.     }
  197.     
  198.     public void paint (Graphics g) {
  199.         imagen1.flush ();
  200.         imagen2.flush ();
  201.         water (c,c^1);
  202.         c= c^1;
  203.         if (c == 0) g.drawImage (imagen1,0,0,this);
  204.         else g.drawImage (imagen2,0,0,this);
  205.     }
  206.     public boolean mouseDrag (Event evt, int x, int y) {
  207.         if (y != 0 && y != alto-1) pant[c][ancho*y+x] = 127;
  208.         return true;    
  209.     }
  210.     public boolean mouseDown (Event evt, int x, int y) {
  211.         if (evt.metaDown()) {
  212.             if (runner == null) start ();
  213.             else stop();
  214.         }
  215.         return true;
  216.     }
  217. }