This is a Java applet. Move your mouse around to create particles of random colors.
To run this snippet, you can either load it into an IDE of your choice or embed it into a webpage using the APPLET tag. More information about the APPLET tag can be found here: http://docs.oracle.com/javase/1.4.2/docs/guide/misc/applet.html
This image is a slightly modified version.
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.LinkedList;
import java.util.List;
public class AppletFun extends Applet implements MouseMotionListener {
public static final int WIDTH = 500;
public static final int HEIGHT = 300;
private List<Particle> list = new LinkedList<Particle>();
private Image backbuffer;
public void init() {
setSize(WIDTH, HEIGHT);
backbuffer = createImage(WIDTH, HEIGHT);
Graphics g = backbuffer.getGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
setBackground(Color.BLACK);
addMouseMotionListener(this);
}
@Override
public void mouseDragged(MouseEvent e) {
mouseMoved(e);
}
@Override
public void mouseMoved(MouseEvent e) {
synchronized (list) {
list.add(new Particle(e.getX(), e.getY()));
}
}
public void start() {
Runnable runnable = new Runnable() {
@Override
public void run() {
while (true) {
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException ignored) { }
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
public void update(Graphics g) {
g.drawImage(backbuffer, 0, 0, this);
synchronized (list) {
for (int i = 0; i < list.size(); i++) {
Particle p = list.get(i);
if (!p.draw(g)) {
list.remove(i);
}
}
}
}
class Particle {
int x;
int y;
int size;
int attenuateAlpha;
double xVel;
double yVel;
Color color;
Particle(int x, int y) {
this.x = x;
this.y = y;
size = (int) random(1, 6);
attenuateAlpha = (int) random(1, 18);
xVel = random(-4, 4);
yVel = random(-4, 4);
color = new Color((int) random(0, 255), (int) random(0, 255), (int) random(0, 255));
}
boolean draw(Graphics g) {
update();
if (color.getAlpha() <= 0) {
return false;
}
g.setColor(color);
g.fillOval(x, y, size, size);
return true;
}
double random(double min, double max) {
return min + Math.random() * ((max - min) + 1d);
}
void update() {
int alpha = color.getAlpha();
alpha -= attenuateAlpha;
if (alpha < 0) {
alpha = 0;
}
color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
x += xVel;
y += yVel += .2d;
}
}
}