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

2020/12/13

Making a png transparent

Roshan123 Roshan123

2020/12/13

#
How will i make a png transparent Actually I have drawn it in background....
1
2
3
GreenfootImage img=new GreenfootImage ("danpost.png");
img.scale(x,y);
getBackground ().drawImage(img,x,y);
Due to some reason I want to make it transparent after getBackground().drawImage....(3rd line) and not before it Is it possible to do like that???
danpost danpost

2020/12/13

#
Roshan123 wrote...
How will i make a png transparent
There is this.
Roshan123 Roshan123

2020/12/13

#
I tried it 2nd time and now also its not working as i already said that I want to make the img transparent after 3rd line
Roshan123 Roshan123

2020/12/13

#
f.clear(); I wrote it in 4th line
Roshan123 Roshan123

2020/12/13

#
setTransparency is aslo not working after 3rd line
Super_Hippo Super_Hippo

2020/12/13

#
Roshan123 wrote...
Due to some reason (...)
What reason is that? With ‘setTransparency’ just like with ‘setColor’, you change how following changes are done. It doesn’t change anything on its own. So if you add it before line 3, it will work.
Roshan123 Roshan123

2020/12/14

#
Roshan123 wrote...
Due to some reason I want to make it transparent after getBackground().drawImage....(3rd line) and not before it Is it possible to do like that???
As i already said that i want to add it in 4th line Mechanism which I want:- I have drawn a button figure on the world (drawRect.....fillRect)and if the mouse is clicked on that particular area(mouseInfo get x and getY)then it will add a png and if its clicked 2nd time it should be cleared (img.clear()) I just wanted to know that Is it possible after drawing the image in the world and without making a button class
Roshan123 Roshan123

2020/12/14

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//globally
short count=1;
GreenfootImage img=new GreenfootImage ("danpost.png");
//Act
MouseInfo mouse=Greenfoot.getMouseInfo () ;
if (Greenfoot . mousePressed (null) ){
int x = mouse.getX();
int y = mouse.getY();
if (x> 100 && x<150 && y>100 && y<150) {
count+=1;
   }
if(count%2==0)
{
img.scale(x,y);
getBackground ().drawImage(img,x,y);
}
if(count%2!=0)
img.clear();
}
Super_Hippo Super_Hippo

2020/12/14

#
No, you can’t do it like that because in line 15, you draw the content of the image on the background. Changing the image afterwards doesn’t change anything about what you drew on the background. However, GF doesn’t really like transparency very much. You can find that out like this: If your whole code is the following (using the GF bee image), the bee will become more and more opaque when the scenario is running because the image is drawn on top of the previous one every act cycle instead of clearing the background image and redrawing it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import greenfoot.*;
 
public class MyWorld extends World
{
    public MyWorld()
    {
        super(600, 400, 1);
         
        GreenfootImage img1 = new GreenfootImage("bee.png");
        img1.scale(getWidth(), getHeight());
        GreenfootImage img2 = new GreenfootImage(img1.getWidth(), img1.getHeight());
        img2.setTransparency(1);
        img2.drawImage(img1, 0, 0);
        setBackground(img2);
    }
}
Roshan123 Roshan123

2020/12/15

#
I just wanted to know that is it possible to do something after drawing in the background of the world And i got the answer thanks to both of the genius!!!
You need to login to post a reply.