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

2014/8/11

How do I cover my background image with a translucent image?

jogit666 jogit666

2014/8/11

#
I want my background image to be greenish under certain conditions. Here's my code (MyWorld class):
1
2
3
4
5
6
7
8
9
10
11
12
public void greenBg()
    {
        GreenfootImage image = new GreenfootImage(getWidth(), getHeight());
         
        int alpha = 50;
        Color color = new Color(0, 255, 0, alpha);
         
        image.setColor(color);
        image.fill();
         
        getBackground().drawImage(image, 0, 0);
    }
What it does is every act it draws a new translucent green layer on top of the background image, but doesn't remove the old one, so the background becomes completely green after a few acts. Any ideas?
erdelf erdelf

2014/8/11

#
You could reference the original background and change the code to this
1
2
3
4
5
6
7
8
9
10
11
12
public void greenBg() 
    
        GreenfootImage image = new GreenfootImage(getWidth(), getHeight()); 
           
        int alpha = 50
        Color color = new Color(0, 255, 0, alpha); 
           
        image.setColor(color); 
        image.fill(); 
        setBackground(new GreenfootImage(background));
        getBackground().drawImage(image, 0, 0); 
    }
or you could place the green layer in its own class
jogit666 jogit666

2014/8/11

#
Yep! Thanks.
danpost danpost

2014/8/11

#
Or, you could limit the adding of green to only one act by adding an instance boolean field. You should probably also have instance GreenfootImage fields to hold both the initial background image and the green image.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// add instance fields
private boolean greenApplied = false;
private GreenfootImage background, greenCover;
// in constructor
background = new GreenfootImage(getBackground());
greenCover = new GreenfootImage(getWidth(), getHeight());
greenCover.setColor(Color.green);
greenCover.setTransparency(50);
// your 'greenBG' method
private void greenBG()
{
    if (greenApplied != (greenApplied = true)) getBackground().drawImage(greenCover, 0, 0);
}
// method to remove the green tint
private void normalBG()
{
    if (greenApplied != (greenApplied = false)) setBackground(new GreenfootImage(background));
}
With this, the system will not have to keep recreating the image every act.
You need to login to post a reply.