Greenfoot gets mouse support

Greenfoot is finally getting proper support for mouse input. This has been the single most requested feature for some time. This means that you can now write games that use the mouse as a control. Such as Minesweeper.


Adding mouse support to Greenfoot was a bit tricky, since Greenfoot already processed many mouse events. Previously, mouse gestures were interpreted by the Greenfoot environment itself, for things such as dragging objects, and popping up their menus.

It needed some careful rework to pass the right mouse events on to the user application, and processing some in the environment.

But now it’s all done, and you can download the new Greenfoot with mouse support.

In designing the mouse support, we aimed at making it both flexible, and easy to use. I think we have succeeded. Here are some code samples:

To make an actor react to mouse clicks, use the following code:

        if(Greenfoot.mouseClicked(this)) {
            System.out.println("click");
        }

Not too hard, is it? Initially, we had discussed an alternative design, where the ‘mouseClicked’ method would be in the Actor class, and then would not need a parameter. The code then would have been slightly simpler here:

        if(mouseClicked()) {
            System.out.println("click");
        }

The reason we decided on the first version is flexibility. Consider this: sometimes you want to react to a mouse click that is not on an actor. So you also need a ‘mouseClicked’ version in the World class. That’s fine. But sometimes you want to react to mouse events in the world independent of the actor underneath it. If, for example, you want to write a scenario where an actor always follows the mouse (Balloons is such a scenario), then you need to process a mouse motion event in any case: whether the mouse is on an actor or not. The version of the methods without the parameters would have passed the event to the actor if the mouse happens to be over an actor, or to the world if not.

In our version of the API, you can add a method to the world, that gets always called:

        if(Greenfoot.mouseClicked(null)) {
            System.out.println("click");
        }

Put this method in your world, and you will receive every click. Think of the parameter as a filter: if you specify a parameter, only clicks on that object will be reported. If you specify null, you will get all clicks. You can do the same with other types of events:

        if(Greenfoot.mouseMoved(null)) {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            setLocation(mouse.getX(), mouse.getY());
        }

As you can see here, if you get notified about a mouse event, you can retrieve a MouseInfo object to check further details, such as the mouse’s location. Or its click count. The following code will only react to double-clicks:

        if(Greenfoot.mouseClicked(this)) {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if(mouse.getClickCount() == 2)
                System.out.println("double-click");
        }

In a similar manner you can react to mouse drag events, or to specific button press and releases. For details, see the new Greenfoot API description, or have a look at the source of the two demo scenarios, Balloons and Minesweeper. Minesweeper is also playable online.

One thought on “Greenfoot gets mouse support

  1. Hi
    can someone help me?
    I actually wanna know how can i use onclick methode on a button ?
    can anyone put some example here for me??

Leave a Reply to sam Cancel reply

Your email address will not be published. Required fields are marked *