Programming

Fire Java Applet

Go, go, go... here is the first final version of my Fire Applet! As you see, it's getting hotter and hotter... : )
Jump directly to the Result!

What's the trick of Fire programming?
1. The Algorithm
The basic idea is that for a new image, you take from every point of the old image all the surrounding heatpoints (8 points that is), take the average and placing the new value one line higher in the new image. This gives a 'natural' fire effect.
Lets look at an example:

0

0

0

0

0

0

0

0

0

0

1

0

2

0

0

1

0

2

1

4

0

0

5

4

5

0

3

0

1

6

8

8

0

7

2

The new value of the red point we are going to calculate... wel that's the average of all the points around it... the green ones.
So (1+0+2+5+5+6+8+8)/8 = 35/8 = 4 (without remainder).
We then place it one line higher in the new image:

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

4

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

Now we do the rest of the points, without the edges, so only the purple ones, to keep it simple:

0

0

0

0

1

0

0

0

1

2

1

2

0

0

0

2

4

2

3

1

0

-

-

-

-

-

-

-

-

-

-

-

-

-

-

I use the 2 lines at the bottom for the 'hot' spots. There you randomly place 'hot' spots to get the 'wild' appearance of the fire...

2. The colors or palette
A color exists of 3 color 'parts': Red, Green and Blue. If you want a fire-like color palette, mostly you want to begin with black, then to red, then to orange, yellow, bright yellow and finally white. This is in a increasing order of temperature (coz the value of every point stands for the temperature of the point).
1. Let the Red part increase to it's maximum
2. Add an increasing Green part
3. Add an increasing Blue part
When you increase the Green part while increasing the Red part, it will cause the palette to be less Red biased. So then there will be no pure red and the orange colors will overrule the red colors much faster.

Let's take a look
The source: FireApplet.java
Get the AnimatedApplet here
A 'doubled' version is here, the pixels are 2 times as big in both directions.


I used AnimatedApplet by Ewald / Gattman, which simplified the Java-to-screen stuff very much.

Mail me at anarchriz(at)hotmail(dot)com.