This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kartoffelbrot
Kartoffelbrot wrote ...

2013/4/3

How to display the cursor?

Kartoffelbrot Kartoffelbrot

2013/4/3

#
Hello everybody, how can I display the mouse cursor in Greenfoot? I saw it already, for example in Nightmare Emergence, but i wasn't able to find that method. Thanks for help.
danpost danpost

2013/4/3

#
Gevater_Tod4711 Gevater_Tod4711

2013/4/3

#
To change the cursor image you first need this imports:
import greenfoot.core.WorldHandler;
import java.awt.Dimension;
import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import javax.swing.*;
import java.awt.Toolkit;
To change the cursor image you can use this method:
public void changeCursorImage(GreenfootImage cursorImage) {
    JPanel panel = WorldHandler.getInstance().getWorldCanvas();
    Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage.getAwtImage(), new Point(0, 0), "Cursor");
    panel.setCursor(cursor);
}
Maybe you have to change the point (new Point(0, 0), ...) because using this point the cursor point will be at the top left of the image. If you want it somewhere else you have to use anther point. E.g. : new Point(cursorImage().getWidth()/2, cursorImage.getHeight()/2) if you want the point to be in the middle of the image.
Kartoffelbrot Kartoffelbrot

2013/4/3

#
Thank you both!
Kartoffelbrot Kartoffelbrot

2013/4/5

#
Implemented it now. Had first a few problems with the GreenfootImage, because i don't know these methodes very well, but now it works fine! :)
Kartoffelbrot Kartoffelbrot

2013/4/7

#
And how can I redo that, so that the mouse is back? Can i save the normal image of the cursor in a Greenfoot image variable?
danpost danpost

2013/4/7

#
You can save the original Cursor in a field with:
Cursor defaultCursor = panel.getCursor();
or, you can get the system default cursor with:
Cursor defaultCursor = Cursor.getDefaultCursor();
which can be used directly in the setting of the cursor:
panel.setCursor(Cursor.getDefaultCursor());
Kartoffelbrot Kartoffelbrot

2013/4/7

#
Thank you.
danpost danpost

2013/4/7

#
Please refer to the Cursor class documentation for extra info you may want to know about.
You need to login to post a reply.