home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1998 April A
/
Pcwk4a98.iso
/
Lotus
/
Beanmach
/
DATA1.CAB
/
Application_Files
/
beans
/
Fireworks.java
< prev
next >
Wrap
Text File
|
1997-11-05
|
5KB
|
233 lines
//------------------------------------------------------------------------------
// BeanMachine IBM copyright 1996, 1997
//
// Class: Fireworks
//
// Description:
// A sample class that illustrates how to import Java applets
// for the BeanMachine Palette.
//
//------------------------------------------------------------------------------
import java.applet.*;
import java.awt.*;
import java.lang.*;
import java.util.*;
import java.net.*;
public class Fireworks extends Applet implements Runnable
{
public int speed;
private int BURSTS;
private int ENERGY;
private int LINES;
private int LENGTH;
private int w,h;
private Thread thread=null;
private Color color[];
private boolean finished[];
private int energy[];
private int lines[];
private int length[];
private int steps[];
private Point pt[];
private Point origin[];
private int[] vx[];
private int[] vy[];
private static String parameterInfo[][] = {
{"speed", "1-100", "How fast the fireworks go"}
};
public void init()
{
int i;
String p;
p=getParameter("speed");
speed=(p==null) ? 10:Integer.valueOf(p).intValue();
w=getSize().width;
h=getSize().height;
BURSTS = (int)(Math.random()*3) + 1;
ENERGY = 400 + (int)(Math.random()*100);
LINES = (int)(Math.random()*20) + 30;
LENGTH = w/3;
color = new Color[BURSTS];
energy = new int[BURSTS];
lines = new int[BURSTS];
length = new int[BURSTS];
finished = new boolean[BURSTS];
steps = new int[BURSTS];
pt = new Point[BURSTS];
origin = new Point[BURSTS];
vx = new int[LINES][BURSTS];
vy = new int[LINES][BURSTS];
for(i=0;i<BURSTS;i++)
{
finished[i] = false;
}
}
public void start()
{
if(thread==null)
{
thread=new Thread(this);
thread.start();
}
}
public void stop()
{
if(thread!=null)
{
thread.stop();
thread=null;
}
}
public void run()
{
int i;
Graphics g=getGraphics();
while(true)
{
try
{
thread.sleep(1000/speed);
}
catch(InterruptedException x)
{
// do nothing
}
for(i=0;i<BURSTS;i++)
{
if (!finished[i])
{
startBurst(i);
}
finishBurst(i, g);
}
}
}
public void paint(Graphics g)
{
g.setColor(Color.black);
g.fillRect(0,0,w,h);
}
public void startBurst(int i)
{
long seed=(long)(Math.random()*10000);
int j;
int loss;
energy[i]=(int)(Math.random()*ENERGY);
lines[i]=(int)(Math.random()*LINES);
length[i]=(int)(Math.random()*LENGTH);
vx[i]=new int[lines[i]];
vy[i]=new int[lines[i]];
int red=(int)(Math.random()*128)+127;
int blue=(int)(Math.random()*128)+127;
int green=(int)(Math.random()*128)+127;
color[i] = new Color(red, green, blue);
int ox = (int)(Math.random()*w/2)+w/4;
int oy = (int)(Math.random()*h/2)+h/4;
origin[i] = new Point(ox, oy);
for(j=0;j<lines[i];j++)
{
loss = energy[i] / 4;
vx[i][j]=(int)(Math.random()*energy[i]) - (loss*2);
vy[i][j]=(int)(Math.random()*loss*3);
}
steps[i]=0;
finished[i]=true;
}
public void finishBurst(int i, Graphics g)
{
Point p, o;
if (finished[i])
if (steps[i]<length[i])
{
int j,c;
double step;
int red, green, blue;
// make the color closer to white
red = (int)(Math.random()*64) + color[i].getRed() - 32;
blue = (int)(Math.random()*64) + color[i].getBlue() - 32;
green = (int)(Math.random()*64) + color[i].getGreen() - 32;
if ((red > 0 && red < 255) &&
(blue > 0 && blue < 255) &&
(green > 0 && green < 255))
color[i] = new Color(red, green, blue);
int len = length[i]/2;
for(j=0;j<lines[i];j++)
{
p = getNextPoint(i,j, steps[i]);
o = origin[i];
g.setColor(color[i]);
g.drawLine(o.x+p.x, o.y-p.y, o.x+p.x, o.y-p.y);
// At the halfway point, redraw in black to make the line disappear
if (steps[i]>=len)
{
step = ((steps[i]-len)*2);
p = getNextPoint(i,j,step);
g.setColor(Color.black);
g.drawLine(o.x+p.x, o.y-p.y, o.x+p.x, o.y-p.y);
step = step + 1;
p = getNextPoint(i,j,step);
g.setColor(Color.black);
g.drawLine(o.x+p.x, o.y-p.y, o.x+p.x, o.y-p.y);
}
}
steps[i]++;
}
else
{
finished[i]=false;
}
}
private Point getNextPoint(int i, int j, double step)
{
double s = step/LENGTH;
double gravity = LINES * s * s;
int x =(int)(vx[i][j]*s);
int y =(int)(vy[i][j]*s-gravity);
pt[i] = new Point(x, y);
return pt[i];
}
public Dimension getPreferredSize()
{
return (new Dimension(100, 100));
}
public String[][] getParameterInfo()
{
return parameterInfo;
}
}