Ucigame - Gallery - BallFollower

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

Your browser cannot show Java applets. BallFollower uses mouse.x() and mouse.y() to get the current mouse location. (0, 0) is the applet's upper left hand corner. When the mouse is not over the applet area, the applet is only sent updated mouse positions if the mouse is being "dragged" or moved with a mouse button held down.

BallFollower.java
import ucigame.*;

public class BallFollower extends Ucigame
{
    int x, y;
    int ballRadius = 30;
    Sprite ball;

    public void setup()
    {
        window.size(250, 250);
        window.title("Ball Follower");
        window.showFPS();
        canvas.background(255, 200, 200);

        ball = makeSprite(getImage("images/ball.gif", 255, 255, 255));
        x = canvas.width() / 2;
        y = canvas.height() / 2;
        ball.position(x, y);

        framerate(30);
    }

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

        ball.nextX((9 * ball.x() + mouse.x()) / 10);
        ball.nextY((9 * ball.y() + mouse.y()) / 10);

        ball.draw();
    }
}