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

2017/4/9

CursorImage

Nosson1459 Nosson1459

2017/4/9

#
What is the reason that when I use the following code the image on my mouse is so big, at least one-hundred pixels each way, and the hot spot is still in the corner? What's supposed to happen (I think) is I'm supposed to have a black circle with a 10 pixel diameter and the hot spot being in the center.
import greenfoot.Actor;
import greenfoot.Font;
import greenfoot.Greenfoot;
import greenfoot.GreenfootImage;
import greenfoot.MouseInfo;
import greenfoot.World;
import greenfoot.core.WorldHandler;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.JColorChooser;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * Write a description of class Paper here.
 *
 * @author Yehuda (1/2 of Nosson1459 - greenfoot.org user name)
 * @version 04/07/2017 - MM/DD/YYYY
 */
public class Paper extends World {

    private int x, y;
    private greenfoot.Color backgroundColor = Color.WHITE, drawingColor = Color.BLACK;
    private GreenfootImage mouseImage = new GreenfootImage(10, 10);
    private Actor chooseColorButton = new Actor() {
        public void act() {
            if (Greenfoot.mouseClicked(chooseColorButton)) {
                drawingColor = Color.toGreenfootColor(JColorChooser.showDialog(null,
                        "Choose Drawing Color", java.awt.Color.WHITE));
            }
        }
    };

    /**
     * Constructor for objects of class Paper.
     *
     */
    public Paper() {
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        try {
            // Set System L&F
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (UnsupportedLookAndFeelException | ClassNotFoundException |
                InstantiationException | IllegalAccessException e) {
            System.err.println(e.getMessage());
        }
        GreenfootImage buttonImage = new GreenfootImage(125, 25);
        buttonImage.setColor(new Color(125, 125, 125));
        buttonImage.fillRect(0, 0, buttonImage.getWidth(), buttonImage.getHeight());
        buttonImage.setColor(Color.BLACK);
        buttonImage.drawRect(0, 0, buttonImage.getWidth() - 2, buttonImage.getHeight() - 2);
        buttonImage.drawRect(1, 1, buttonImage.getWidth() - 3, buttonImage.getHeight() - 3);
        buttonImage.setColor(Color.RED);
        buttonImage.setFont(new Font(false, true, 11));
        buttonImage.drawString("Choose Drawing Color", 5, buttonImage.getHeight() / 2 + 5);
        chooseColorButton.setImage(buttonImage);
        addObject(chooseColorButton, chooseColorButton.getImage().getWidth() / 2,
                getHeight() - chooseColorButton.getImage().getHeight() / 2);
    }

    /**
     *
     */
    public void act() {
        mouseImage.setColor(drawingColor);
        mouseImage.fillOval(0, 0, mouseImage.getWidth(), mouseImage.getHeight());
        changeMouseImage(mouseImage, mouseImage.getWidth() / 2, mouseImage.getHeight() / 2, "Brush");
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse == null) {
            return;
        }
        // going to add code here, so the 'if' and 'return' are going to be doing something
    }

    /**
     * Use this method to change the image of your mouse in the world. If the
     * image is too large it will automatically be scaled to a certain size.
     *
     * @param cursor the image to display when the cursor is activated
     * @param xHotSpot the x coordinate of the mouse's hot spot
     * @param yHotSpot the y coordinate of the mouse's hot spot
     * @param name a localized description of the cursor, for Java Accessibility
     * use
     */
    public void changeMouseImage(GreenfootImage cursor, int xHotSpot, int yHotSpot, String name) {
        WorldHandler.getInstance().getWorldCanvas().setCursor(Toolkit.getDefaultToolkit().createCustomCursor(cursor.getAwtImage(),
                new Point(xHotSpot, yHotSpot), name));
    }
}
danpost danpost

2017/4/9

#
It does not seem right to execute lines 66 through 68 every act cycle. Try moving those lines into the constructor of the class.
Nosson1459 Nosson1459

2017/4/9

#
I tried that already. If I do that then the color of the mouse won't change since the changeMouseImage method is only being called once. Anyway I don't see how that's supposed help fix my initial problem(s). It didn't help, it's not helping, and it won't help.
You need to login to post a reply.