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: http://docs.oracle.com/javase/1.4.2/docs/guide/misc/applet.html

This image is a slightly modified version.

m_204352_tcYKdmfYhPz0wcuLqFW2tMxb4.gif
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;
        }
    }
}

Comments

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

Glad to hear it worked.

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

 Respond  
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.

 Respond  
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.

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

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

 Respond  
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.

 Respond  
Aion-   -  Sep 14, 2012

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

Edit: Updated description.

 Respond  
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.

 Respond  
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.