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

2013/6/10

Set image when hovering over button

1
2
SmirkenKerk SmirkenKerk

2013/6/10

#
Hey guys, I was just wondering. I have a button on a menu screen. When the cursor hovers over that button, I want it to change the image of that button. When the cursor is not hovering over that button, set to default image. Any idea on how to do this would be helpful. Thanks.
Zamoht Zamoht

2013/6/10

#
Here you go: Remember to import the List class. import java.util.List;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void act()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        //Change Button.class to the name of your button class.
        if (mouse != null) {
            //change the file to what you want for when the mouse is not over the button.
            setImage("button1.png");
            List objects = getWorld().getObjectsAt(mouse.getX(), mouse.getY(), Attempt.class);
            for (Object object : objects)
            {
                if (object == this)
                {
                    //change the file to what you want for when the mouse is over the button.
                    setImage("button2.png");
                }
            }
        }
    }
Put this as the buttons act method. Please ask if you have any questions.
alagon alagon

2013/11/8

#
It throws me an error with the "List", which is fixed by importing the java.util.List; but...how do i fix the "Attempt.class"
alagon alagon

2013/11/8

#
Ahh i got it! Sorry :c
alagon alagon

2013/11/8

#
But a problem i am facing now is that the button for me is showing both images.... this is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import greenfoot.*;
import java.util.List;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class start extends Actor
{
    public void act()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo(); 
        //Change Button.class to the name of your button class. 
        if (mouse != null) { 
            //change the file to what you want for when the mouse is not over the button. 
            setImage("Start/1.png"); 
            List objects = getWorld().getObjectsAt(mouse.getX(), mouse.getY(), start.class); 
            for (Object object : objects) 
            
                if (object == this
                
                    //change the file to what you want for when the mouse is over the button. 
                    setImage("Start/2.png"); 
                
 
            
        }
        setLocation(500,400);
        if (Greenfoot.mouseClicked(this)) 
        
            Greenfoot.setWorld(new Level1());
        
    }  
}
danpost danpost

2013/11/8

#
The conditions to check for to determine when which image to use are: (1) if (Greenfoot.mouseMoved(this)) // for mouse moved over (2) if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this)) // for mouse moved off However, to avoid unnecessarily setting the image to its current image, you should add an instance boolean field to the class of the button, call it 'mouseOver'. Then the act method (or a method it calls) would have code something like this:
1
2
3
4
5
6
7
8
9
10
if (!mouseOver && Greenfoot.mouseMoved(this))
{
    setImage("Start/2.png");
    mouseOver = true;
}
if (mouseOver && Greenfoot.mouseMoved(null) && ! Greenfoot.mouseMoved(this))
{
    setImage("Start/1.png");
    mouseOver = false;
}
alagon alagon

2013/11/15

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import greenfoot.*;
import java.util.List;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
 
public class start extends Actor
{
    boolean mouseOver = false;
 
    public void act()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo(); 
         
       if (!mouseOver && Greenfoot.mouseMoved(this)) 
        
            setImage("Start/2.png"); 
            mouseOver = true
         
         if (mouseOver && Greenfoot.mouseMoved(null) && ! Greenfoot.mouseMoved(this)) 
         
             setImage("Start/1.png"); 
             mouseOver = false
         }
 
        setLocation(500,400);
        if (Greenfoot.mouseClicked(this)) 
        
            Greenfoot.setWorld(new Level1());
        
         
    }  
}
Still doesn't work...help?
danpost danpost

2013/11/15

#
(1) remove line 11 ('mouse' is not used anywhere in the method) (2) remove line 2 (you do not create any lists in the method) (3) I cannot be sure, but I do not think line 24 needs to be there either Now ... how is it not working? is the image changing at all? describe what is happening and what should be happening and be specific and clear.
alagon alagon

2013/11/18

#
Alright it worked, Thanks.
alagon alagon

2013/11/18

#
Where do i set the location though?
danpost danpost

2013/11/18

#
The location is set when you add the object into the world.
alagon alagon

2013/11/19

#
Even if I set the location in the world or the actual class,it still does the dual imaging.
danpost danpost

2013/11/19

#
First, drag the button to make sure you only have one. Then, if you only have one, describe exactly what behavior you are getting (you may need to show your world class code).
alagon alagon

2013/11/19

#
If i set a location in the world class or in the actual "start" class, it does the dual imaging. However if i don't set a location it doesn't do that. Here's my world class:
1
2
3
4
5
6
7
8
9
10
11
12
13
public class Menu extends World
{
    public Menu()
    {   
        super(1000, 600, 1);
        setBackground("Back.png");
    }
    public void act()
    {
        addObject(new start(),0,0);
        addObject(new TitleScreen(),0,0);
    }
}
And this is my start:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class start extends Actor
{
     
    boolean mouseOver = false;
    public start()
    {
    }
    public void act()
    {   
   if (!mouseOver && Greenfoot.mouseMoved(this)) 
        
            setImage("Start/2.png"); 
            mouseOver = true
         
         if (mouseOver && Greenfoot.mouseMoved(null) && ! Greenfoot.mouseMoved(this)) 
         
             setImage("Start/1.png"); 
             mouseOver = false
         }
 
        
        if (Greenfoot.mouseClicked(this)) 
        
            Greenfoot.setWorld(new Level1());
        
         
    }  
}
danpost danpost

2013/11/19

#
The problem is that you are adding the objects (the start button and title screen) into the world from within an 'act' method. The 'act' method for the world is executed many times every second while the scenario is running; meaning a multitude of start buttons and title screens will be added to the world. Add the objects from within the world constructor, which is only executed once per world instance creation, to have the objects added one time, and one time only (or remove lines 7 through 9 in the Menu world class).
There are more replies on the next page.
1
2