以下是一個用java演示的產生圖像煙花的算法
import java
import java
import java
import java
public class jhanabi extends Applet
implements Runnable
{
// 類成員變量定義域開始
private int m_nAppX;
private int m_nAppY;
private int m_centerX;
private int m_centerY;
private int m_mouseX;
private int m_mouseY;
private int m_sleepTime;
private boolean isError;
private boolean m_isPaintFinished;
boolean isRunning;
boolean isInitialized;
Thread runner;
int pix
MemoryImageSource offImage;
Image dbImg;
int pixls;
int pixls
Random rand;
int bits;
double bit_px[];
double bit_py[];
double bit_vx[];
double bit_vy[];
int bit_sx[];
int bit_sy[];
int bit_l[];
int bit_f[];
int bit_p[];
int bit_c[];
int bit_max;
int bit_sound;
int ru;
int rv;
AudioClip sound
AudioClip sound
// 類成員變量定義域結束 < 初始化過程祥見構造方法 >
public jhanabi// 構造方法 (如有必要
{
m_mouseX =
m_mouseY =
m_sleepTime =
isError = false;
isInitialized = false;
rand = new Random;
bits =
bit_px = new double[bits];
bit_py = new double[bits];
bit_vx = new double[bits];
bit_vy = new double[bits];
bit_sx = new int[bits];
bit_sy = new int[bits];
bit_l = new int[bits];
bit_f = new int[bits];
bit_p = new int[bits];
bit_c = new int[bits];
ru =
rv =
}// jhanabi 結束
//構造方法結束
public void init
{
String s = getParameter(
if(s != null)
bits = Integer
s = getParameter(
if(s != null)
bit_max = Integer
s = getParameter(
if(s != null)
ru = Integer
s = getParameter(
if(s != null)
rv = Integer
s = getParameter(
if(s != null)
bit_sound = Integer
m_nAppX = size
m_nAppY = size
m_centerX = m_nAppX /
m_centerY = m_nAppY /
m_mouseX = m_centerX;
m_mouseY = m_centerY;
resize(m_nAppX
pixls = m_nAppX * m_nAppY;
pixls
pix
offImage = new MemoryImageSource(m_nAppX
offImage
dbImg = createImage(offImage);
for(int i =
pix
sound
sound
for(int j =
bit_f[j] =
isInitialized = true;
start;
}//init 結束
public void run
{
while(!isInitialized)
try
{
Thread
}
catch(InterruptedException interruptedexception) { }
do
{
for(int j =
{
int k = pix
int l = pix
int i
int j
int i = (k &
int k
i = (k &
int l
i = k &
int i
i = (i
int j
i = (i
int k
i = i
int l
int i
int j
int k
pix
}//for 結束
rend;
offImage
try
{
Thread
}
catch(InterruptedException interruptedexception
}// do 結束
while(true);
}//run 結束
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
g
}
public void start
{
if(isError)
return;
isRunning = true;
if(runner == null)
{
runner = new Thread(this);
runner
}
}
public void stop
{
if(runner != null)
{
runner
runner = null;
}
}
public boolean mouseMove(Event event
{
m_mouseX = i;
m_mouseY = j;
return true;
}
public boolean mouseDown(Event event
{
m_mouseX = i;
m_mouseY = j;
int k = (int)(rand
int l = (int)(rand
int i
int j
int k
for(int l
{
if(bit_f[l
continue;
bit_px[l
bit_py[l
double d = rand
double d
bit_vx[l
bit_vy[l
bit_l[l
bit_p[l
bit_c[l
bit_sx[l
bit_sy[l
bit_f[l
if(++k
break;
}
if(bit_sound >
sound
return true;
}//mouseDown 結束
public boolean mouseExit(Event event
{
m_mouseX = i;
m_mouseY = j;
return true;
}
void rend
{
boolean flag = false;
boolean flag
boolean flag
for(int k =
switch(bit_f[k])
{
default:
break;
case
bit_vy[k] += rand
bit_px[k] += bit_vx[k];
bit_py[k] += bit_vy[k];
bit_l[k]
if(bit_l[k] ==
{
bit_c[k] =
bit_f[k] =
}
else
if(bit_p[k] ==
{
if((int)(rand
bit_set((int)bit_px[k]
}
else
{
bit_set((int)bit_px[k]
}
break;
case
bit_sy[k]
if((double)bit_sy[k] <= bit_py[k])
{
bit_f[k] =
flag
}
if((int)(rand
{
int i = (int)(rand
int j = (int)(rand
bit_set(bit_sx[k] + i
}
break;
}//switch 結束
if(flag
sound
}// rend 結束
void bit_set(int i
{
int l = i + j * m_nAppX;
pix
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26065.html