home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 12,000 to 12,999 / 12000.zip / AOLDLs / Online-Tools / Java-Applets / JAVAAPPS.lzh / JAVAAPPS / FIRE / FIRE.EXE / fire.java < prev   
Encoding:
Java Source  |  1996-04-13  |  7.1 KB  |  230 lines

  1.  
  2.  
  3.  
  4.  
  5. /* 
  6.  
  7.  * Permission to use, copy, modify and distribute this software and its
  8.  
  9.  * documentation without fee for NON-COMMERCIAL purposes is hereby granted
  10.  
  11.  * provided that this notice with a reference to the original source and
  12.  
  13.  * the author appears in all copies or derivatives of this software.
  14.  
  15.  *
  16.  
  17.  * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  18.  
  19.  * THIS SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  20.  
  21.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  22.  
  23.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR
  24.  
  25.  * ANY DAMAGES SUFFERED BY ANYBODY AS A RESULT OF USING, MODIFYING OR
  26.  
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  
  29.  */
  30.  
  31.  
  32.  
  33. import java.awt.*;
  34.  
  35. import java.lang.*;
  36.  
  37.  
  38.  
  39. /**
  40.  
  41.  * fire - A firelike display
  42.  
  43.  * Inspired by VangelisTeam's PC fire demo
  44.  
  45.  * 
  46.  
  47.  * @version 1.0, 12/21/95
  48.  
  49.  * @author <A HREF="http://www.data.net.mx/~jrodrig/">Javier Rodriguez</A> (jrodrig@data.net.mx) 
  50.  
  51.  */
  52.  
  53.  
  54.  
  55. public class fire extends java.applet.Applet implements Runnable {
  56.  
  57.   boolean first=true;
  58.  
  59.   int ROWS = 50;
  60.  
  61.   int COLS = 64;
  62.  
  63.   int HIDDEN = 4;
  64.  
  65.   int ROWS_SEED = 4;
  66.  
  67.   int ROWS_RESEED = 48;
  68.  
  69.   int MAX_SEED = 8;
  70.  
  71.   int PALETTE_SIZE = 64;
  72.  
  73.   int COOLING_LIMIT = 32;
  74.  
  75.   int COOLING_ROWS = 42;
  76.  
  77.   int COOLING_FACTOR = 2;
  78.  
  79.   java.awt.Color palette[] = new java.awt.Color[PALETTE_SIZE];
  80.  
  81.   byte Buffer[],Buffer2[];
  82.  
  83.   String message,textfont;
  84.  
  85.   int textsize,textX,textY;
  86.  
  87.   Color textcolor;
  88.  
  89.   Image offScrImage=null;
  90.  
  91.   Graphics offScrGC;
  92.  
  93.   Dimension offScrSize;
  94.  
  95.  
  96.  
  97.   Thread kicker=null;
  98.  
  99.  
  100.  
  101.   public String getAppletInfo() {
  102.  
  103.     return "Fire applet by Javier Rodriguez <jrodrig@data.net.mx>";
  104.  
  105.   }
  106.  
  107.  
  108.  
  109.   public String[][] getParameterInfo() {
  110.  
  111.     String[][] info = {
  112.  
  113.     {"coolingrows",        "int",        "number of rows to cool"},
  114.  
  115.     {"coolingfactor",    "int",        "cooling factor"},
  116.  
  117.     {"coolinglimit",    "int",        "cooling threshold"},
  118.  
  119.     {"soundtrack",        "url",        "background sound"},
  120.  
  121.     {"text",        "String",    "message"},
  122.  
  123.     {"textcolor",        "String",    "text color"},
  124.  
  125.     {"textfont",        "String",    "text font"},
  126.  
  127.     {"textsize",        "int",        "text size"}
  128.  
  129.     };
  130.  
  131.     return info;
  132.  
  133.   }
  134.  
  135.  
  136.  
  137.   public void init() {
  138.  
  139.     int r,i;
  140.  
  141.     String aux;
  142.  
  143.     // Set some constants
  144.  
  145.     COLS = size().width;
  146.  
  147.     ROWS = size().height + HIDDEN;
  148.  
  149.     // Get parameters
  150.  
  151.     aux=getParameter("coolinglimit");
  152.  
  153.     if((aux!=null)&&(aux.endsWith("%"))) { aux=aux.substring(0,aux.length()-1); }
  154.  
  155.     COOLING_LIMIT = (aux==null)?(int)(PALETTE_SIZE*0.5):(int)(PALETTE_SIZE*(Integer.valueOf(aux).intValue())/100);
  156.  
  157.     aux=getParameter("coolingrows");
  158.  
  159.     if((aux!=null)&&(aux.endsWith("%"))) { aux=aux.substring(0,aux.length()-1); }
  160.  
  161.     COOLING_ROWS = (aux==null)?(int)(ROWS*0.8):(int)(ROWS*(Integer.valueOf(aux).intValue())/100);
  162.  
  163.     aux=getParameter("coolingfactor");
  164.  
  165.     COOLING_FACTOR= (aux==null)?2:(int)(Integer.valueOf(aux).intValue());
  166.  
  167.     ROWS_RESEED = (int)(ROWS*0.96);
  168.  
  169.     // Get text parameters
  170.  
  171.     aux = getParameter("text");
  172.  
  173.     message=(aux==null)?"":aux;
  174.  
  175.     aux = getParameter("textfont");
  176.  
  177.     textfont=(aux==null)?"TimesRoman":aux;
  178.  
  179.     aux=getParameter("textsize");
  180.  
  181.     textsize=(aux==null)?18:(Integer.valueOf(aux).intValue());
  182.  
  183.     aux=getParameter("textcolor");
  184.  
  185.     textcolor=hexColor(aux,Color.white);
  186.  
  187.     // Setup buffers;
  188.  
  189.     Buffer = new byte[COLS*ROWS];
  190.  
  191.     Buffer2 = new byte[COLS*ROWS];
  192.  
  193.     // Setup palette
  194.  
  195.     for(i=0; i<16; ++i)
  196.  
  197.       palette[i]= new Color(16*i,0,0);
  198.  
  199.     for(i=0; i<16; ++i)
  200.  
  201.       palette[16+i] = new Color(255, 16*i, 0);
  202.  
  203.     for(i=0; i<32; ++i)
  204.  
  205.       palette[32+i] = new Color(255,255,8*i);
  206.  
  207.     // Setup text
  208.  
  209.     Font myFont=new Font(textfont, Font.BOLD, textsize);
  210.  
  211.     FontMetrics myMetrix=getFontMetrics(myFont);
  212.  
  213.     int textH=myMetrix.getHeight();
  214.  
  215.     int textW=myMetrix.stringWidth(message);
  216.  
  217.     textX=(int)((COLS-textW)/2);
  218.  
  219.     textY=ROWS-HIDDEN-(int)((ROWS-HIDDEN-textH)/2)-myMetrix.getDescent();
  220.  
  221.     setFont(myFont);
  222.  
  223.     // Seed image
  224.  
  225.     for(r=COLS*(ROWS-ROWS_SEED); r<(ROWS*COLS); ++r) {
  226.  
  227.       Buffer[r]=(byte)(Math.random()*(PALETTE_SIZE-1));
  228.  
  229.     }
  230.  
  231.   }
  232.  
  233.  
  234.  
  235. void MainLoop() {
  236.  
  237.   int r,a,i;
  238.  
  239.   for(r=COLS+1;r<(COLS*(ROWS-1))-1;++r) {
  240.  
  241.     a=Buffer[r-COLS-1]+Buffer[r-COLS]+Buffer[r-COLS+1]+Buffer[r-1]+Buffer[r+1]+
  242.  
  243.       Buffer[r+COLS-1]+Buffer[r+COLS]+Buffer[r+COLS+1];
  244.  
  245. //    a=(a>>3)%PALETTE_SIZE;
  246.  
  247.     a=(a>>3);
  248.  
  249.     // Cool flames
  250.  
  251.     if(a<COOLING_LIMIT) {
  252.  
  253.       if((r<COOLING_ROWS*COLS)&&(a>COOLING_FACTOR)) a-=COOLING_FACTOR;
  254.  
  255.     }
  256.  
  257.     Buffer2[r]=(byte)(a);
  258.  
  259.   }
  260.  
  261.   // Seed at base
  262.  
  263.   for(r=COLS*(ROWS_RESEED);r<COLS*(ROWS);++r) {
  264.  
  265.     a=Buffer2[r];
  266.  
  267.     Buffer2[r]=(byte)((a-(Math.random()*MAX_SEED))%(PALETTE_SIZE*1.1));
  268.  
  269.     //Buffer2[r]=(byte)((a-(Math.random()*MAX_SEED))%PALETTE_SIZE);
  270.  
  271.   }
  272.  
  273.   // Scroll image
  274.  
  275.   for(i=0;i<COLS*(ROWS-1);++i) 
  276.  
  277.     Buffer[i]=Buffer2[i+COLS];
  278.  
  279. }
  280.  
  281.  
  282.  
  283.   public final synchronized void update(Graphics g) {
  284.  
  285.     // Setup off-screen buffer
  286.  
  287.     Dimension d=size();
  288.  
  289.     if((offScrImage==null)||(d.width!=offScrSize.width)||(d.height!=offScrSize.height)) {
  290.  
  291.       offScrImage=createImage(d.width,d.height);
  292.  
  293.       offScrSize=d;
  294.  
  295.       offScrGC=offScrImage.getGraphics();
  296.  
  297.       offScrGC.setFont(getFont());
  298.  
  299.     }
  300.  
  301.     if (offScrGC!=null) {
  302.  
  303.       offScrGC.fillRect(0,0,d.width,d.height);
  304.  
  305.       paint(offScrGC);
  306.  
  307.       g.drawImage(offScrImage,0,0,null);
  308.  
  309.     }
  310.  
  311.   }
  312.  
  313.  
  314.  
  315. public void paint(Graphics g) {
  316.  
  317.     int a;
  318.  
  319.     Color c;
  320.  
  321.     // Do main loop
  322.  
  323.     MainLoop();
  324.  
  325.     // Copy buffer to off-screen buffer
  326.  
  327.     for(int y=0;y<(ROWS-HIDDEN);++y)
  328.  
  329.       for(int x=0;x<COLS;++x) {
  330.  
  331.         a=Buffer[y*COLS+x];
  332.  
  333.         a=a<0?-a:a; // Patch nasty bug 
  334.  
  335.         a=a<(PALETTE_SIZE-1)?(a):(PALETTE_SIZE-1);
  336.  
  337.         c=palette[a];
  338.  
  339.         try {
  340.  
  341.           offScrGC.setColor(c);
  342.  
  343.           offScrGC.drawLine(x,y,x+1,y);
  344.  
  345.         } catch (Exception e) { }
  346.  
  347.       }
  348.  
  349.     try {
  350.  
  351.     // Write text
  352.  
  353.       offScrGC.setColor(textcolor);
  354.  
  355.       offScrGC.drawString(message,textX,textY);
  356.  
  357.     // Paint off-screen buffer
  358.  
  359.       g.drawImage(offScrImage,0,0,this);
  360.  
  361.     } catch (Exception e) { }
  362.  
  363.  
  364.  
  365.   }
  366.  
  367.  
  368.  
  369.   public void start() {
  370.  
  371.     if (kicker==null) {
  372.  
  373.       kicker=new Thread(this);
  374.  
  375. //      kicker.setPriority(kicker.MAX_PRIORITY);
  376.  
  377.       kicker.start();
  378.  
  379.     }
  380.  
  381.   }
  382.  
  383.  
  384.  
  385.   public void stop() {
  386.  
  387.     kicker=null;
  388.  
  389.   }
  390.  
  391.  
  392.  
  393.   public void run() {
  394.  
  395.     while(kicker!=null) {
  396.  
  397.       repaint();
  398.  
  399.       try {kicker.sleep(15);} catch (InterruptedException e) {}
  400.  
  401.     }
  402.  
  403.   }
  404.  
  405.  
  406.  
  407.   // Place fire bubble on click
  408.  
  409.   public boolean mouseDown(java.awt.Event evt, int x, int y) {
  410.  
  411.     int i;
  412.  
  413.     i=x+y*COLS; 
  414.  
  415.     if(i>81) {
  416.  
  417.       Buffer[i]=(byte)255;
  418.  
  419.       Buffer[i-COLS]=(byte)255;
  420.  
  421.       Buffer[i+COLS]=(byte)255;
  422.  
  423.       Buffer[i-1]=(byte)255;
  424.  
  425.       Buffer[i+1]=(byte)255;
  426.  
  427.     }
  428.  
  429.     return true;
  430.  
  431.   }
  432.  
  433.  
  434.  
  435.   public Color hexColor(String hex, Color std) {
  436.  
  437.     try {
  438.  
  439.       Integer rgb=new Integer(0);
  440.  
  441.       hex.replace('#',' ');
  442.  
  443.       hex.trim();
  444.  
  445.       rgb=Integer.valueOf(hex,16);
  446.  
  447.       return new Color(rgb.intValue());
  448.  
  449.     } catch (Exception e) {
  450.  
  451.       return std;
  452.  
  453.     }
  454.  
  455.   }
  456.  
  457. }
  458.  
  459.