please dont rip this site

Language Java Nosugar Graphics Pall Code Kaosfire.java

//  Freely distributable, cannibalisable.
//
// bear in mind that this is a
// port from kaosfire.c written by Dr.Kaos (Matthew Pearce)
// to java by the Dustman (sunil@magnetic.demon.co.uk).
//
// in java you can't guarantee screen size, so this is 
// a lot more generic than the original C. However this comes
// at a price.
//
// Also, theres no way to plot a single pixel! You have to create
// an entire image everytime. Java API sucks for fast graphics.
//
// then the indexcolormodel is a kludge. rgb values are stored as 
// bytes which even though in java are signed so making values
// no bigger than 127, can store values upto 255.
//
// Finally, there is a bug in the java garbage collector when using
// image sources which means it has to be kicked off manually.

import java.awt.*;
import java.awt.image.*;
import ThreadedCanvas;

public class kaosfire extends ThreadedCanvas
{	
	//#####################################################################
	//# Constants
	//#####################################################################
	static final int MINIMUM_WIDTH = 50;
	static final int MINIMUM_HEIGHT = 100;
	static final int DEFAULT_FIRESTRENGTH=15;
	
	//#####################################################################
	//# Variables
	//#####################################################################
	int pixels[] = null;
	IndexColorModel palette = null;
    int w,h;
    int strength;
	
	//#####################################################################
	//# overriding parent methods
	//#####################################################################
	void set_mem_image( int w, int h)
	{
		super.set_mem_image(w,h);
		
		//---------------------------------------------------------------
		pixels = new int[w * h];
	}
	
	
	//#####################################################################
	//# implement abstract methods
	//#####################################################################
	public kaosfire()
	{
		super();
		initialise_palette();
	    strength = DEFAULT_FIRESTRENGTH;
	}
	
	public kaosfire( int fire_strength)
	{
		this();
		strength = fire_strength;
	}
	
	public Dimension minimumSize()
	{
		return new Dimension(MINIMUM_WIDTH, MINIMUM_HEIGHT);
	}
	
	//***************************************************************
	public void onRun(Graphics g)
	{
		set_clear_flag(false);
		w = size().width;
		h = size().height;
		black_out();
	}
	
	//***************************************************************
	public void onLoop( Graphics g)
	{
		MemoryImageSource source;
		Image img;
		
		dofire();
		
		source = new MemoryImageSource(w,h,	palette, pixels, 0,w);
		img = createImage(source);
		g.drawImage(img, 0, 0, this);
		
		//-------force deletion or we'll run out of memory-----
		source = null;
		img = null;
		Runtime.getRuntime().gc();
	}
	
	//#####################################################################
	//# get yer privates out
	//#####################################################################
	private void initialise_palette()
	{
		int i2,i3,i4, index;
		byte  r[], g[],b[],i,i255, c;
		
		//--------- set up colour arrays ------------------
		r = new byte[256];
		g = new byte[256];
		b = new byte[256];
		i255 = (byte)255;
		
		//--------- populate ------------------
	    for(i=0; i<64; i++)
	    {
	    	c = (byte) (i*4);
	    	r[i] = 0; g[i] = 0; b[i] = 0;          	// black - black

	    	i2 = i+64;
	    	r[i2] = c; g[i2] = 0; b[i2] = 0;		// black ->red
	    	
			i3 = i+128;
			r[i3] = i255; g[i3] = c; b[i3] = 0;		// red ->yellow;

			i4 = i+192;
			r[i4] = i255; g[i4] = i255; b[i4] =c;	//yellow -> white
    	} 
		palette	= new IndexColorModel(8,256,r,g,b);
	}
	
	//***************************************************************
	private void black_out()
	{
		int i, max;
		
		
		max = w*h;
		for (i = 0; i < max; i++)
			pixels[i]= 0;
	}


	//***************************************************************
	//
	// in essence this is a cellular automata rule.
	// A black border is left all the way around the edge
	//
	private void dofire()
	{
    	int x, y,i,i_start;
    	int avg;
    	int nw,n,ne,west,centre,e,sw,s,se;
    	double seed;

	    // ------------- make a small fire at bottom of screen	----
    	i = 1 + (w * (h-1));
	    
	    for(x = 1; x<w-1; x++)
    	{
            //- - - - - - - - - random black or white dot - - - - - 
    		seed = Math.random() * strength;
    		if (seed > 1.0)
            	pixels[i] = 255;
	        else         
            	pixels[i] = 0;
            
            //- - - - - - - - - next element - - - - - - - - - - - -
            i++;
        }
   		
   		//---------blurring  the pixels feeds the fire--------------
   		i_start = w+1;
   		
		for(y=1; y<h-1; y++)
	    {        
	    	i = i_start;
	    	
	    	//	nw = pixels[i-1-w];
	    	//	 n = pixels[i-w];
		      west = pixels[i-1];
		    centre = pixels[i];
		    	sw = pixels[i+w-1];
		    	 s = pixels[i+w];
		    
	        for(x = 1; x<w-1; x++)
	        {
	        	//-------------------find average of surrounding pixels--
		    	//ne = pixels[i+1-w];
		    	 e = pixels[i+1];
		    	se = pixels[i+1+w];
		    	
	        	//--------average - to speed up, ignore line above ------
	        	//avg = (nw+n+ne +west + e + sw + s + se) / 8;
	        	avg = (west + centre + e + sw + s + se) / 6;
	            pixels[i] = avg;
	            
	            //------------next element------------------------------
	        	//nw = n;
	        	//n = ne;
	            west = centre;		//this was the one that was recalculated
	            centre = e;
	            sw = s;
	            s = se;
	            
	            i++;
	        }
	        i_start +=w;
	    }
	    
	}
	
}

file: /Techref/language/JAVA/nosugar/graphics/pall/code/kaosfire.java, 5KB, , updated: 1997/8/25 13:46, local time: 2024/11/22 14:16,
TOP NEW HELP FIND: 
18.119.142.210:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://sxlist.com/techref/language/JAVA/nosugar/graphics/pall/code/kaosfire.java"> language JAVA nosugar graphics pall code kaosfire</A>

Did you find what you needed?

 

Welcome to sxlist.com!


Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.
 

Welcome to sxlist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .