FileDocCategorySizeDatePackage
JumpingBox.javaAPI DocSun JDK 1.4.2 Example5968Thu May 12 00:35:29 BST 2005None

JumpingBox

public class JumpingBox extends Applet implements ComponentListener, MouseListener, MouseMotionListener

Fields Summary
private int
mx
private int
my
private Dimension
size
private int
onaroll
Constructors Summary
Methods Summary
public voidcomponentHidden(java.awt.event.ComponentEvent e)

public voidcomponentMoved(java.awt.event.ComponentEvent e)

public voidcomponentResized(java.awt.event.ComponentEvent e)

        repaint();
    
public voidcomponentShown(java.awt.event.ComponentEvent e)

        repaint();
    
public voiddestroy()

        removeMouseListener(this);
        removeMouseMotionListener(this);
    
public java.lang.StringgetAppletInfo()

        return "Title: JumpingBox\n"
            + "Author: Anonymous";
    
public voidinit()

        onaroll = 0;
        setSize(500, 500);
        size = getSize();
        addMouseListener(this);
        addMouseMotionListener(this);
        addComponentListener(this);
    
public voidmouseClicked(java.awt.event.MouseEvent e)

public voidmouseDragged(java.awt.event.MouseEvent e)

public voidmouseEntered(java.awt.event.MouseEvent e)

        repaint();
    
public voidmouseExited(java.awt.event.MouseEvent e)

        repaint();
    
public voidmouseMoved(java.awt.event.MouseEvent e)

        e.consume();
        if ((e.getX() % 3 == 0) && (e.getY() % 3 == 0)) {
            repaint();
        }
    
public voidmousePressed(java.awt.event.MouseEvent e)

        int x = e.getX();
        int y = e.getY();
        e.consume();
        requestFocus();
        if (mx < x && x < mx + getSize().width / 10 - 1 && 
            my < y && y < my + getSize().height / 10 - 1) {  //determine if hit
            if (onaroll > 0) {   //not first hit
                switch (onaroll%4) {   //play a sound
                case 0:
                    play(getCodeBase(), 
                         "sounds/tiptoe.thru.the.tulips.au");
                    break;
                case 1:
                    play(getCodeBase(), "sounds/danger.au");
                    break;
                case 2:
                    play(getCodeBase(), "sounds/adapt-or-die.au");
                    break;
                case 3:
                    play(getCodeBase(), "sounds/cannot.be.completed.au");
                    break;
                }
                onaroll++;
                if (onaroll > 5) {
                    getAppletContext()
                        .showStatus("You're on your way to THE HALL OF FAME:"
                                    + onaroll + "Hits!");
                } else {
                    getAppletContext().showStatus("YOU'RE ON A ROLL:" 
                                                  + onaroll + "Hits!");
                }
            } else {   //first hit
                getAppletContext().showStatus("HIT IT AGAIN! AGAIN!");
                play(getCodeBase(), "sounds/that.hurts.au");
                onaroll = 1;
            }
        } else {   //miss
            getAppletContext().showStatus("You hit nothing at (" + x + ", " 
                                          + y + "), exactly");
            play(getCodeBase(), "sounds/thin.bell.au");
            onaroll = 0;
        }
        repaint();
    
public voidmouseReleased(java.awt.event.MouseEvent e)

public voidpaint(java.awt.Graphics g)

        g.setColor(Color.black);
        g.drawRect(0, 0, size.width - 1, size.height - 1);
        g.drawRect(mx, my, (size.width / 10) - 1, 
                   (size.height / 10) - 1);
    
public voidupdate(java.awt.Graphics g)

        Dimension newSize = getSize();
        if (size.equals(newSize)) {
            // Erase old box
            g.setColor(getBackground());
            g.drawRect(mx, my, (size.width / 10) - 1, 
                       (size.height / 10) - 1);
        } else {
            size = newSize;
            g.clearRect(0, 0, size.width, size.height);
        }
        // Calculate new position
        mx = (int) (Math.random() * 1000) % 
            (size.width - (size.width / 10));
        my = (int) (Math.random() * 1000) % 
            (size.height - (size.height / 10));
        paint(g);