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

2013/10/3

Changing color when mouse is hovering

Sniper Sniper

2013/10/3

#
I'm trying to change the color of a button when the mouse is hovered over it. I've achieved it by using the mouse info coordinates, but once in a while I get null pointer exceptions. Any suggestions? Its world:
Start start = new Start();
addObject(start, 450, 170);
Start.class:
public class Start extends Buttons
{
    public void act() 
    {
        setImage(new GreenfootImage("New Game", 50, Color.BLUE, null));
        
        if (Greenfoot.getMouseInfo().getX() >= 350 && Greenfoot.getMouseInfo().getX() <= 550 &&
            Greenfoot.getMouseInfo().getY() >= 155 && Greenfoot.getMouseInfo().getY() <= 185) {
               setImage(new GreenfootImage("New Game", 50, Color.RED, null));
        }
        
        if (Greenfoot.mouseClicked(this)) {
            Greenfoot.setWorld(new SnowWorld());
        }
    }
}
The exception:
java.lang.NullPointerException
	at Start.act(Start.java:16)
	at greenfoot.core.Simulation.actActor(Simulation.java:568)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
With line 16 being: if (Greenfoot.getMouseInfo().getX() >= 350 && Greenfoot.getMouseInfo().getX() <= 550 && Once the exception comes up it'll pop up on every run until I comment out the if statement, compile, uncomment, an recompile. Then it'll sometimes work for a little while again. Also, is it possible to register a mouse drag in a direction? So say if I drag my mouse to the right or left I can execute different things? I was thinking it would involve mouseDragged and mouseDragEnded but wasn't sure how to implement it.
bourne bourne

2013/10/3

#
This might be helpful: http://www.greenfoot.org/topics/find/2890#post_2890 For drag direction, remember location when dragging begins and compare to when drag ends
Sniper Sniper

2013/10/3

#
Thanks! That worked for the colors :) How exactly would I store the original location before the drag to compare it with after?
bourne bourne

2013/10/3

#
Something like:
private int dragFromX;
private int dragFromY;
private boolean dragging;

...

MouseInfo mouse = Greenfoot.getMouseInfo();
if (Greenfoot.mouseDragged(null))
{
	if (!dragging)
	{
		dragFromX = mouse.getX();
		dragFromY = mouse.getY();
		dragging = true;
	}
}
if (Greenfoot.mouseDragEnded(null))
{
	// compare dragFromX, dragFromY  with  mouse.getX() and mouse.getY()
	
	dragging = false;
}
kiarocks kiarocks

2013/10/4

#
Remember that getMouseInfo() might return null in some cases.
API wrote...
The info about the current state of the mouse, or null if the mouse cursor is outside the world boundary (unless being dragged).
Nevermind, just read that link.
danpost danpost

2013/10/4

#
For hovering, you could use the following (which does not require getting a MouseInfo object):
// instance field
private boolean hovering;
// with this in act method
if (!hovering && Greenfoot.mouseMoved(this))
{
    //set red text image
    hovering = true;
}
if (hovering && Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this))
{
    // set blue text image
    hovering = false;
}
For dragging you will need to two int instance fields that bourne suggests using; however, you should not need the boolean one. Something like this:
// instance fields
private int dragFromX, dragFromY;
// in act method
if (Greenfoot.mousePressed(null))
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    dragFromX = mouse.getX();
    dragFromY = mouse.getY();
}
if (Greenfoot.mouseDragged(null) || Greenfoot.mouseDraggedEnded(null))
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    int newX = mouse.getX(), newY = mouse.getY();
    // check difference(s) and act upon them
    dragFromX = newX;
    dragFromY = newY;
}
// finally, if needed
if (Geenfoot.mouseDraggeddEnded(null))
{
   // final touch-up
}
Sniper Sniper

2013/10/5

#
Thanks guys! That worked with a little tweeking :D
// touchscreen (mouse drag) detection/movement
        if (Greenfoot.mousePressed(null))  
        {  
            MouseInfo mouse = Greenfoot.getMouseInfo();  
            dragFromX = mouse.getX();  
            dragFromY = mouse.getY();  
        }  
        if (Greenfoot.mouseDragged(null) || Greenfoot.mouseDragEnded(null))  
        {  
            MouseInfo mouse = Greenfoot.getMouseInfo();  
            int newX = mouse.getX(), newY = mouse.getY();  
            // check difference(s) and act upon them 
            
            if (newX > dragFromX + 30)
            {
                move(4);
                setImage("right.png");
            }
            
            if (newX < dragFromX - 30)
            {
                move(-4);
                setImage("left.png");
            }
        }
I'm gonna try and add a little more functionality once I add a jump function so that a drag upwards will act as that function as well. This is mainly being used for touchscreen detection to play without a keyboard :)
You need to login to post a reply.