FileDocCategorySizeDatePackage
Balls.javaAPI DocExample11574Wed Aug 08 15:13:48 BST 2001demos.Mix

Balls

public class Balls extends AnimatingControlsSurface
Animated color bouncing balls with custom controls.

Fields Summary
private static Color[]
colors
private long
now
private long
deltaT
private long
lasttime
private boolean
active
protected Ball[]
balls
protected boolean
clearToggle
protected JComboBox
combo
Constructors Summary
public Balls()



      
        setBackground(Color.white);
        for (int i = 0; i < colors.length; i++) {
            balls[i] = new Ball(colors[i], 30);
        }
        balls[0].isSelected = true;
        balls[3].isSelected = true;
        balls[4].isSelected = true;
        balls[6].isSelected = true;
        setControls(new Component[] { new DemoControls(this) });
    
Methods Summary
public static voidmain(java.lang.String[] argv)

        createDemoFrame(new Balls());
    
public voidrender(int w, int h, java.awt.Graphics2D g2)

        for (int i = 0; i < balls.length; i++) {
            Ball b = balls[i];
            if (b == null || b.imgs[b.index] == null || !b.isSelected) {
                continue;
            }
            g2.drawImage(b.imgs[b.index], (int) b.x, (int) b.y, this);
        }
        lasttime = now;
    
public voidreset(int w, int h)

        if (w > 400 && h > 100) {
            combo.setSelectedIndex(5);
        }
    
public voidstep(int w, int h)

        if (lasttime == 0) {
            lasttime = System.currentTimeMillis();
        }
        now = System.currentTimeMillis();
        deltaT = now - lasttime;
        active = false;
        for (int i = 0; i < balls.length; i++) {
            if (balls[i] == null) {
                return;
            }
            balls[i].step(deltaT, w, h);
            if (balls[i].Vy > .02 || -balls[i].Vy > .02 ||
                    balls[i].y + balls[i].bsize < h) {
                active = true;
            }
        }
        if (!active) {
            for (int i = 0; i < balls.length; i++) {
                balls[i].Vx = (float)Math.random() / 4.0f - 0.125f;
                balls[i].Vy = -(float)Math.random() / 4.0f - 0.2f;
            }
            clearToggle = true;
        }