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

2016/4/3

Strange bug with GreenfootImage.clear()

TinyTertle TinyTertle

2016/4/3

#
So I'm trying to make a menu where a box appears around a button only when that button is hovered over. I have the following code...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
getOwner().getBackground().drawImage(buttonMenu,
        getOwner().getWidth()/2 - buttonMenu.getWidth()/2,
        getOwner().getHeight()/2 - buttonMenu.getHeight()/2); //Draw button menu to World
         
        MouseInfo info = Greenfoot.getMouseInfo();
        if(info != null){
            buttonMenu.setColor(java.awt.Color.RED);
            if(startButton.contains(info.getX(), info.getY())){  //if start button is being hovered over      
                buttonMenu.drawRect(0, 0, startButton.getWidth()-1, startButton.getHeight()-1);
            }
            else if(helpButton.contains(info.getX(), info.getY())){ //help button is being hovered over
                buttonMenu.drawRect(0, buttonMenu.getHeight()/2, helpButton.getWidth()-1, helpButton.getHeight()-1);
            }
            else buttonMenu.clear(); //if neither are being hovered over.
        }
helpButton and startButton are both Rectangles (a class I made). The issue is that after I hover over one of the buttons and the rectangle appears, it stays there permanently. Just as a note, I tested the execution sequence by adding a few System.out.println's and the code executes properly (if, else if, and else are all executed in an exepected manner). I can see that buttonMenu.clear() is being called. but no change is made to the image. I've been stuck on this for hours! Please help!
Super_Hippo Super_Hippo

2016/4/3

#
You draw an image on the background. If you change the image, the drawn image isn't changed. So you draw one image over another. If you use 'clear', it is a transparent image which will then be drawn on the background again (expecting this code is in or called from the act-method). If you draw a transparent image on something, you won't see a difference. To actually remove the red hover rectangles, you have to use the 'clear' method on the background too and draw it again. As an alternative, you can use an actor which has two images (hovered and not-hovered) and you change its image when the mouse is over it.
TinyTertle TinyTertle

2016/4/3

#
That makes so much sense! Thank you so much!
You need to login to post a reply.