Ucigame - Gallery - MouseTrap

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

Your browser cannot show Java applets. MouseTrap illustrates some of Ucigame's mouse handling features, using onMouseXXX() methods and the mouse object.

Left and right clicks rotate the mouse (more if Ctrl or Alt buttons are down). The mouse wheel moves the mouse up and down. The mouse can be dragged (x and y are swapped if the right button is held down).

MouseTrap.java
import ucigame.*;

public class MouseTrap extends Ucigame
{
    Sprite maxwell;
    double maxwellRotation = 0;

    public void setup()
    {
        window.size(250, 250);
        window.title("MouseTrap");
        framerate(30);

        canvas.background(200, 230, 255);

        maxwell = makeSprite(getImage("images/mouse.png", 255, 0, 0));
        maxwell.position(50, 50);
        maxwell.motion(0, 0);
    }

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

        maxwell.move();
        maxwell.rotate(maxwellRotation);
        maxwell.draw();
    }

    public void onMousePressed()
    {
        double rotationDegrees = 5;
        if (mouse.isControlDown())
            rotationDegrees = 15;
        if (mouse.isAltDown())
            rotationDegrees = 30;
        if (mouse.sprite() == maxwell)
        {
            if (mouse.button() == mouse.LEFT)
                maxwellRotation = maxwellRotation - rotationDegrees;
            else if (mouse.button() == mouse.RIGHT)
                maxwellRotation = maxwellRotation + rotationDegrees;
            else
                println(mouse.button());
        }
    }

    public void onMouseDragged()
    {
        if (mouse.button() == mouse.LEFT)
        {
            maxwell.motion(mouse.Xchange(), mouse.Ychange(), ADDONCE);
        }
        else // weird
        {
            maxwell.motion(mouse.Ychange(), mouse.Xchange(), ADDONCE);
        }
    }

    public void onMouseWheelMoved()
    {
        maxwell.motion(0, mouse.wheelClicks(), ADDONCE);
    }
}