Vectors | Fireworks | Particle Systems | Fading | Snow | Fire | Applet1 Code

Haz clic en el applet. Este muestra un uso básico de partículas.

//	Código de 'fireworks':

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Event;
import java.util.Vector;

public class fireworks extends Applet1
{
	Vector particles;
	
	public void init()
	{
		particles = new Vector();
	}
	
	public void appletRun()
	{
		for ( int j = 0; j < particles.size(); ++j )
		{
			Particle p = (Particle)(particles.elementAt(j));
			if (p.maxage > 0)
			{
				p.x += p.speed * (float)(Math.cos((Math.PI / 180) * p.angle));
				p.y += p.speed * (float)(Math.sin((Math.PI / 180) * p.angle));
				p.maxage--;
				particles.set(j, p);
			}
			else
				particles.removeElementAt(j);
		}
	}
	
	public void paint(Graphics g)
	{
		for ( int j = 0; j < particles.size(); ++j )
		{
			Particle p = (Particle)(particles.elementAt(j));
			g.setColor(p.color);
			g.fillOval ((int)p.x, (int)p.y, p.diameter, p.diameter);
		}
		g.drawString(""+particles.size(), 10, 10);
	}
	
	public boolean mouseDown(Event e, int x, int y)
	{
		for (int i = 0; i < 200; i++)
			particles.addElement(new Particle( x, y, 2, rnd(80), rnd(360), rnd(5f), rndColor(), rndColor() ));
		return true;
	}
	
	public Color rndColor()
	{
		return new Color(rnd(255),rnd(255),rnd(255));
	}

	public class Particle
	{
		public float x;
		public float y;
		public int diameter;
		public int age = 0;
		public int maxage;
		public float angle;
		public float speed;
		public Color color;
		
		public Particle(float x, float y, int diameter, int maxage, float angle, float speed, Color start, Color end)
		{
			this.x = x;
			this.y = y;
			this.diameter = diameter;
			this.maxage = maxage;
			this.angle = angle;
			this.speed = speed;
			this.color = start;
		}
	}
}
1
Hosted by www.Geocities.ws