Ucigame - Gallery - LunchTimer

Home | Getting Started | Reference | Introduction to Java | Gallery

Your browser cannot show Java applets. This thrilling Ucigame demo illustrates timers and sprite rotation. Each of the five letters in "LUNCH" appears, after a delay, at a random spot on the canvas, rotates one or more times, and then disappears. The sprite images are all in images/lunch.png.

LunchTimer.java
import ucigame.*;

public class LunchTimer extends Ucigame
{
    Sprite bigL, bigU, bigN, bigC, bigH;

    double bigLRotation = 0;
    double bigURotation = 0;
    double bigNRotation = 0;
    double bigCRotation = 0;
    double bigHRotation = 0;

    int WIDTH = 250, HEIGHT = 250;

    public void setup()
    {
        window.size(WIDTH, HEIGHT);
        window.title("LunchTimer");
        framerate(30);

        canvas.background(230, 230, 180);

        // Create the five sprites from five parts of lunch.png
        bigL = makeSprite(50,50);
        bigL.addFrame(getImage("images/lunch.png", 255), 0, 0);
        bigL.position(random(WIDTH - 50), random(HEIGHT - 50));
        bigL.hide();
        bigU = makeSprite(50,50);
        bigU.addFrame(getImage("images/lunch.png", 255), 50, 0);
        bigU.position(random(WIDTH - 50), random(HEIGHT - 50));
        bigU.hide();
        bigN = makeSprite(50,50);
        bigN.addFrame(getImage("images/lunch.png", 255), 100, 0);
        bigN.position(random(WIDTH - 50), random(HEIGHT - 50));
        bigN.hide();
        bigC = makeSprite(50,50);
        bigC.addFrame(getImage("images/lunch.png", 255), 150, 0);
        bigC.position(random(WIDTH - 50), random(HEIGHT - 50));
        bigC.hide();
        bigH = makeSprite(50,50);
        bigH.addFrame(getImage("images/lunch.png", 255), 200, 0);
        bigH.position(random(WIDTH - 50), random(HEIGHT - 50));
        bigH.hide();

        startTimer("bigLappear", 2000);
        startTimer("bigUappear", 3000);
        startTimer("bigNappear", 4000);
        startTimer("bigCappear", 5000);
        startTimer("bigHappear", 7000);
    }

    public void draw()
    {
        canvas.clear();

        bigL.rotate(bigLRotation);
        bigL.draw();
        bigU.rotate(bigURotation);
        bigU.draw();
        bigN.rotate(bigNRotation);
        bigN.draw();
        bigC.rotate(bigCRotation);
        bigC.draw();
        bigH.rotate(bigHRotation);
        bigH.draw();
    }

    public void bigLappearTimer()
    {
        bigL.show();
        stopTimer("bigLappear");
        startTimer("bigLrotator", 100);
    }

    public void bigLrotatorTimer()
    {
        bigLRotation += 360/80;
        if (bigLRotation >= 360)
        {
            stopTimer("bigLrotator");
            startTimer("bigLdisappear", 2000);
        }
    }

    public void bigLdisappearTimer()
    {
        bigL.hide();
        stopTimer("bigLdisappear");
    }


    public void bigUappearTimer()
    {
        bigU.show();
        stopTimer("bigUappear");
        startTimer("bigUrotator", 50);
    }

    public void bigUrotatorTimer()
    {
        bigURotation += 720/140;
        if (bigURotation >= 720)
        {
            stopTimer("bigUrotator");
            startTimer("bigUdisappear", 3000);
        }
    }

    public void bigUdisappearTimer()
    {
        bigU.hide();
        stopTimer("bigUdisappear");
    }


    public void bigNappearTimer()
    {
        bigN.show();
        stopTimer("bigNappear");
        startTimer("bigNrotator", 500);
    }

    public void bigNrotatorTimer()
    {
        bigNRotation += 360/12;
        if (bigNRotation >= 360)
        {
            stopTimer("bigNrotator");
            startTimer("bigNdisappear", 4000);
        }
    }

    public void bigNdisappearTimer()
    {
        bigN.hide();
        stopTimer("bigNdisappear");
    }


    public void bigCappearTimer()
    {
        bigC.show();
        stopTimer("bigCappear");
        startTimer("bigCrotator", 50);
    }

    public void bigCrotatorTimer()
    {
        bigCRotation += 1080/100;
        if (bigCRotation >= 1080)
        {
            stopTimer("bigCrotator");
            startTimer("bigCdisappear", 5000);
        }
    }

    public void bigCdisappearTimer()
    {
        bigC.hide();
        stopTimer("bigCdisappear");
    }


    public void bigHappearTimer()
    {
        bigH.show();
        stopTimer("bigHappear");
        startTimer("bigHrotator", 50);
    }

    public void bigHrotatorTimer()
    {
        bigHRotation += 360/60;
        if (bigHRotation >= 360)
        {
            stopTimer("bigHrotator");
            startTimer("bigHdisappear", 6000);
        }
    }

    public void bigHdisappearTimer()
    {
        bigH.hide();
        stopTimer("bigHdisappear");
    }
}