// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MovingBalls extends JFrame {
    public MovingBalls() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Moving balls");
        getContentPane().setLayout(new BorderLayout());
        Ball red = new Ball(200,200,50,Color.RED);
        Ball green = new Ball(400,200,50,Color.GREEN);
        Ball blue = new Ball(400,400,50,Color.BLUE);
        Ball yellow = new Ball(200,400,50,Color.YELLOW);
        MultiBalls mb = new MultiBalls(600, 600);
        mb.addBall(red);
        mb.addBall(green);
        mb.addBall(blue);
        mb.addBall(yellow);
        getContentPane().add(mb);
        (new BallMover(red, mb)).start();
        (new BallMover(green, mb)).start();
        (new BallMover(blue, mb)).start();
        (new BallMover(yellow, mb)).start();
        pack();
        
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new MovingBalls();
                f.setVisible(true);
            }
        });
    }
}
class MultiBalls extends JPanel {
    private List<Ball> balls;
    public MultiBalls(int w, int h) {
        setPreferredSize(new Dimension(w, h));
        balls = new ArrayList<Ball>();
    }
    public void addBall(Ball b) {
        balls.add(b);
    }
    @Override
    public void paint(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0,getWidth(), getHeight());
        for(Ball b : balls) {
            g.setColor(b.getC());
            g.fillOval(b.getX() - b.getR(), b.getY() - b.getR(), 2*b.getR(), 2*b.getR());
        }
    }
}
class Ball {
    private volatile int x;
    private volatile int y;
    private int r;
    private Color c;
    public Ball(int x, int y, int r, Color c) {
        this.x = x;
        this.y = y;
        this.r = r;
        this.c = c;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public int getR() {
        return r;
    }
    public void setR(int r) {
        this.r = r;
    }
    public Color getC() {
        return c;
    }
    public void setC(Color c) {
        this.c = c;
    }
}
class BallMover extends Thread {
    private Random rng = new Random(getId() + System.currentTimeMillis());
    private Ball b;
    private JPanel p;
    public BallMover(Ball b, JPanel p) {
        this.b = b;
        this.p = p;
    }
    @Override
    public void run() {
        for(;;) {
            try {
                Thread.sleep(250);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int x = b.getX() + rng.nextInt(21) - 10;
            int y = b.getY() + rng.nextInt(21) - 10;
            x = Math.max(x, b.getR());
            x = Math.min(x, p.getWidth() - b.getR());
            y = Math.max(y, b.getR());
            y = Math.min(y, p.getHeight() - b.getR());
            b.setX(x);
            b.setY(y);
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    p.repaint();
                }
            });
        }
    }
}
