home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-01-01 | 5.9 KB | 217 lines |
- import java.awt.image.*;
- import java.awt.Image;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Event;
-
- public class agua extends java.applet.Applet implements Runnable {
-
- Thread runner;
-
- Image imagen1;
- Image imagen2;
-
- byte PalR [];
- byte PalG [];
- byte PalB [];
-
- byte pant [][];
- float cnt,vel;
-
- int puntos [];
- int maxPuntos = 0;
-
- int ancho,alto,c,tam,gotas,Rval,Gval,Bval,delay;
-
- String st;
-
- boolean imagenBien;
-
- public void init () {
-
- int textMap[];
-
- ancho = this.getBounds().width;
- alto = this.getBounds().height;
- tam = ancho*alto;
-
- st = getParameter ("gotas");
- if (st == null) gotas = 1;
- else gotas = Integer.valueOf (st).intValue();
-
- st = getParameter ("vel");
- if (st == null) vel = 1;
- else vel = Float.valueOf (st).floatValue();
-
- st = getParameter ("Rval");
- if (st == null) Rval = 1;
- else Rval = Integer.valueOf(st).intValue();
-
- st = getParameter ("Gval");
- if (st == null) Rval = 1;
- else Gval = Integer.valueOf(st).intValue();
-
- st = getParameter ("Bval");
- if (st == null) Rval = 1;
- else Bval = Integer.valueOf(st).intValue();
-
- st = getParameter ("imagen");
- if (st == null) {
- imagenBien = false;
- System.err.println ("No se ha especificado una imagen");
- }
- else {
- try {
- Image textImg = getImage(getDocumentBase(),st);
-
- st = getParameter ("delay");
- if (st == null) delay = 150;
- else delay = Integer.valueOf (st).intValue();
-
- while (textImg.getWidth(null) == -1);
- int width = textImg.getWidth(null);
- int height = textImg.getHeight(null);
-
- if (width == ancho && height == alto) {
- textMap = new int[width * height];
- PixelGrabber pg = new PixelGrabber(textImg, 0, 0, width, height, textMap, 0, width);
- pg.grabPixels();
- if ((pg.status() & ImageObserver.ABORT) != 0 ) {
- System.err.println("Error al cargar los pφxeles de la imagen.");
- imagenBien = false;
- }
- else {
- for (int cont = 0; cont < tam; cont++) if (textMap [cont] != -1) maxPuntos++;
-
- puntos = new int [maxPuntos];
-
- int p = 0;
- int cont = 0;
-
- while (p < maxPuntos) {
- if (textMap [cont] != -1) {
- puntos[p] = cont;
- p++;
- }
- cont++;
- }
- imagenBien = true;
- }
- }
- else {
- System.err.println ("Error, tama±o del applet distinto de tama±o de la imagen");
- System.err.println ("Applet : "+ancho+"X"+alto);
- System.err.println ("Imagen : "+width+"X"+height);
- imagenBien = false;
- }
- }
- catch (Exception e) {
- System.out.println("Error al cargar la imagen : " + e);
- System.out.println("maxPuntos="+maxPuntos);
- imagenBien = false;
- }
- }
- //Creamos la paleta;
- PalR = new byte [256];
- PalG = new byte [256];
- PalB = new byte [256];
-
- for (int cont = 0; cont < 256; cont++) {
- if (Rval != 0) PalR [cont] = (byte) (cont+Rval);
- if (Gval != 0) PalG [cont] = (byte) (cont+Gval);
- if (Bval != 0) PalB [cont] = (byte) (cont+Bval);
- }
-
- //Creamos las pantallas auxiliares
-
- pant = new byte [2][tam]; //definimos 2 pantallas del tama±o del applet
-
- //Definimos las imßgenes
- imagen1 = createImage (new MemoryImageSource (ancho,alto,new IndexColorModel(8,256,PalR,PalG,PalB),pant[0],0,ancho));
- imagen2 = createImage (new MemoryImageSource (ancho,alto,new IndexColorModel(8,256,PalR,PalG,PalB),pant[1],0,ancho));
-
- for (int cont = 0; cont < tam; cont++) {
- pant [0][cont] = 64;
- pant [1][cont] = 64;
- }
- c = 0;
- }
-
- public void start () {
- if (runner == null) {
- runner = new Thread(this);
- runner.start ();
- }
- }
-
- public void stop () {
- if (runner != null) {
- runner.stop ();
- runner = null;
- }
- }
-
- public void run () {
- int opc = 0;
-
- while (true) {
- repaint ();
- //Lluvia y rastro en el agua;
- int offset1 = (int) (ancho/4*Math.sin(cnt/15));
- int offset2 = (int) (alto/4*Math.sin(cnt/21));
- pant[c][(int)(((tam-ancho)/2)+offset1+ancho*offset2)] = 127;
- cnt+=vel;
- for (int cont = 1; cont < gotas; cont ++) pant[c][random(tam-2*ancho)+ancho] = 127;
-
- //Si el parßmetro de la imagen estß y no ha habido fallos cargamos los puntos en la pantalla
- if (imagenBien) {
-
- if (opc == delay) {
- for (int cont = 1; cont < maxPuntos; cont ++) pant[c][puntos[cont]] = 127;
- opc = 0;
- }
- else opc++;
- }
-
- try {Thread.sleep (25);} catch (InterruptedException e) {}
- }
- }
-
- private void water (int c,int c1) {
- for (int cont = ancho; cont < tam-ancho; cont++) {
- int temp = pant[c][cont-ancho]+pant[c][cont-1]+pant[c][cont+1]+pant[c][cont+ancho]-256;
- temp = (temp>>1)-pant[c1][cont]+64;
- if (temp < 0 ) temp = 0; if (temp > 127) temp = (byte)255;
- pant[c1][cont]=(byte)(64+temp-(temp>>7));
- }
- }
-
- private int random (int num) {
- int sol = (int)(Math.floor(Math.random()*num));
- return sol;
- }
-
- public void update (Graphics g) {
- paint (g);
- }
-
- public void paint (Graphics g) {
- imagen1.flush ();
- imagen2.flush ();
- water (c,c^1);
- c= c^1;
- if (c == 0) g.drawImage (imagen1,0,0,this);
- else g.drawImage (imagen2,0,0,this);
- }
- public boolean mouseDrag (Event evt, int x, int y) {
- if (y != 0 && y != alto-1) pant[c][ancho*y+x] = 127;
- return true;
- }
- public boolean mouseDown (Event evt, int x, int y) {
- if (evt.metaDown()) {
- if (runner == null) start ();
- else stop();
- }
- return true;
- }
- }