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

  1. import java.awt.Graphics;
  2. import java.awt.Color;
  3. import java.awt.Image;
  4. import java.awt.image.*;
  5. import java.awt.Font;
  6. import java.awt.FontMetrics;
  7. import java.awt.Event;
  8. import java.awt.*;
  9.  
  10. public class fuego extends java.applet.Applet implements Runnable {
  11.  
  12.       Thread runner;
  13.       Font letra;
  14.       FontMetrics fmetrics;
  15.     
  16.     
  17.       Image imagenVirtual;
  18.           
  19.       byte PalR [];
  20.       byte PalG [];
  21.       byte PalB [];
  22.       byte aux [];
  23.       int intensidad;
  24.       int frio;
  25.       int alto,ancho,longitud,longReal;
  26.       int tamTexto,estiloTexto,posX,posY,rebote,avance;
  27.       int Rval,Gval,Bval;
  28.       Color colorTexto;
  29.       String st,texto,tipoLetra;
  30.       boolean seMueve;
  31.       Graphics pant;
  32.   
  33.       public void init () {
  34.         
  35.         ancho = this.getBounds().width;
  36.         alto = this.getBounds().height;
  37.         
  38.         st = getParameter ("Rval");
  39.         if (st == null) Rval = 1;
  40.         else Rval = Integer.valueOf(st).intValue();
  41.                 
  42.         st = getParameter ("Gval");
  43.         if (st == null) Rval = 1;
  44.         else Gval = Integer.valueOf(st).intValue();
  45.  
  46.         st = getParameter ("Bval");
  47.         if (st == null) Rval = 1;
  48.         else Bval = Integer.valueOf(st).intValue();
  49.  
  50.         st = getParameter ("intensidad");
  51.         if (st == null) intensidad = 40;
  52.         else intensidad = Integer.valueOf(st).intValue();
  53.         
  54.         st = getParameter ("frio");
  55.         if (st == null) frio = 1;
  56.         else frio = Integer.valueOf(st).intValue();
  57.         
  58.         st = getParameter ("texto");
  59.         if (st != null) {
  60.                 texto = st;
  61.                 
  62.                 st = getParameter ("seMueve");
  63.                 if (st == null) seMueve = false;
  64.                 else {
  65.                             if (Integer.valueOf(st).intValue() == 1) {
  66.                                 seMueve = true;
  67.                                 st = getParameter ("avance");
  68.                                 if (st == null) avance = 1;
  69.                                 else avance = Integer.valueOf(st).intValue();
  70.                             }
  71.                             else seMueve = false;
  72.                 }
  73.                 
  74.                 st = getParameter ("colorTexto");
  75.                 if (st == null) colorTexto = Color.red;
  76.                 else colorTexto = new Color(Integer.parseInt(st,16));
  77.                 
  78.                 st = getParameter ("tamTexto");
  79.                 if (st == null) tamTexto = 15;
  80.                 else tamTexto = Integer.valueOf(st).intValue();
  81.                 
  82.                 st = getParameter ("estiloTexto");
  83.                 if (st == null) estiloTexto = 0;
  84.                 else estiloTexto = Integer.valueOf (st).intValue();
  85.                 
  86.                 st = getParameter ("tipoLetra");
  87.                 if (st == null) tipoLetra = "TimesRoman";
  88.                 else tipoLetra = st;
  89.                                         
  90.                 letra = new Font (tipoLetra,estiloTexto,tamTexto);
  91.                 fmetrics = getFontMetrics (letra);
  92.                 rebote = -(fmetrics.stringWidth(texto));
  93.                 
  94.                 st = getParameter ("posX");
  95.                 if (st == null) posX = 1;
  96.                 else posX = Integer.valueOf (st).intValue();
  97.                 
  98.                 st = getParameter ("posY");
  99.                 if (st == null) posY = alto-1;
  100.                 else posY = Integer.valueOf (st).intValue();
  101.         }
  102.         
  103.         if (seMueve) posX = ancho;
  104.         
  105.         PalR = new byte [256];
  106.         PalG = new byte [256];
  107.         PalB = new byte [256];
  108.                 
  109.         longitud = ancho*alto;
  110.         longReal = ancho*(alto+1);
  111.         
  112.         aux  = new byte [longReal];
  113.             
  114.         for (int cont = 0; cont < 256; cont++) {
  115.             PalR [cont] = (byte) (cont+Rval);
  116.             PalG [cont] = (byte) (cont+Gval);
  117.             PalB [cont] = (byte) (cont+Bval);
  118.         }
  119.     
  120.         imagenVirtual = createImage (new MemoryImageSource (ancho,alto,new IndexColorModel(8,256,PalR,PalG,PalB),aux,0,ancho));
  121.     }
  122.  
  123.     private void fuego () {
  124.         
  125.        int col;
  126.                 
  127.      cambia ();
  128.      for (int cont = ancho; cont < longitud; cont++) {
  129.                   col = (aux[cont]+aux[cont+1]+aux[cont-1]+aux[cont+ancho])>>2;
  130.                   if (col > frio) col-= frio;
  131.              aux[cont-ancho] = (byte) col;
  132.      }
  133.       }    
  134.      
  135.     private void cambia () {
  136.     
  137.         int tam,pos,color;
  138.         
  139.         
  140.         for (int cont = 1; cont < intensidad; cont++) {
  141.             tam = (int) Math.floor(Math.random()*15);
  142.             pos = (int) Math.floor(Math.random()*ancho);
  143.             color = (int) (105 + Math.floor(Math.random()*30));
  144.             if (color >= 128) color = 0;    
  145.             for (int cont1 = pos; cont1 < (pos+tam); cont1++) {
  146.                 aux [cont1 + longitud - ancho] = (byte)color;
  147.             }
  148.         }
  149.     }
  150.     
  151.     public void start () {
  152.         if (runner == null) {
  153.             runner = new Thread(this);
  154.             runner.start ();
  155.         }
  156.     }
  157.     
  158.     public void stop () {
  159.         if (runner != null) {
  160.             runner.stop ();
  161.             runner = null;
  162.         }
  163.     }
  164.     
  165.     public void run () {
  166.         while (true) {
  167.             repaint ();
  168.             if (seMueve) {if (posX > rebote) posX -= avance; else posX = ancho;}
  169.             try {Thread.sleep(15);} catch (InterruptedException e) {}
  170.         }
  171.     }
  172.     
  173.     public boolean mouseDown (Event evt, int x, int y) {
  174.         posX = (int) (x - (fmetrics.stringWidth(texto))/2);
  175.         posY = y;
  176.         stop ();
  177.         return true;
  178.     }
  179.     
  180.     public boolean mouseDrag (Event evt, int x, int y) {
  181.         posX = (int) (x - (fmetrics.stringWidth(texto))/2);
  182.         posY = y;
  183.         stop ();
  184.         return true;
  185.     }
  186.     
  187.     public boolean mouseUp (Event evt, int x, int y) {
  188.         start ();
  189.         return true;
  190.     }
  191.     
  192.     public boolean mouseEnter (Event evt, int x, int y) {
  193.         Cursor cruz = new Cursor (Cursor.MOVE_CURSOR);
  194.         setCursor (cruz);
  195.         return true;
  196.     }
  197.     
  198.     public boolean mouseExit (Event evt, int x, int y) {
  199.         Cursor normal = new Cursor (Cursor.DEFAULT_CURSOR);
  200.         setCursor (normal);
  201.         return true;
  202.     }
  203.     
  204.     synchronized public void update (Graphics g) {
  205.         
  206.         imagenVirtual.flush();
  207.         fuego ();
  208.         g.setFont (letra);
  209.         g.drawImage(imagenVirtual,0,0,this);
  210.         g.setColor (colorTexto);
  211.         g.drawString (texto,posX,posY);
  212.     }
  213.         
  214.     public void paint (Graphics g) {
  215.         update (g);
  216.     }
  217. }