Applet fun

By Aion- on Sep 14, 2012

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:

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.fillRect(0, 0, WIDTH, HEIGHT);

    public void mouseDragged(MouseEvent e) {

    public void mouseMoved(MouseEvent e) {
        synchronized (list) {
            list.add(new Particle(e.getX(), e.getY()));

    public void start() {
        Runnable runnable = new Runnable() {
            public void run() {
                while (true) {
                    try {
                    } catch (InterruptedException ignored) { }
        Thread thread = new Thread(runnable);

    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)) {

    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) {
            if (color.getAlpha() <= 0) {
                return false;
            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;


Sign in to comment.
Aion-   -  Sep 14, 2012

Glad to hear it worked.

Just applying some velocity to the x and y coordinates does the trick :p

Sorasyn   -  Sep 14, 2012

Compiled just fine for me under JDK 1.7. Looks good, kind of a neat effect, and the mathematics doesn't look as overwhelming as I had expected from the picture. Lol.

Hawkee   -  Sep 14, 2012

I finally got it to work. Chrome couldn't run it for some reason, but I've been having lots of issues with Chrome lately. It works fine in Firefox.

Aion-   -  Sep 14, 2012
Hawkee   -  Sep 14, 2012

Does this require Java 7? Sometimes that will cause a problem for users who only have Java 6 installed.

Hawkee   -  Sep 14, 2012

Great, much better. I compiled with javac and ran it with appletviewer, but it crashed as soon as it opened. Not sure what went wrong because my system.log didn't throw any errors.

Aion-   -  Sep 14, 2012

My bad. I assumed people would be able to run it.

Edit: Updated description.

Hawkee   -  Sep 14, 2012

Might be nice to offer a little description of what it does and maybe some instruction on how to run it.

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.