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

2017/4/30

How do I delete Parts of a GreenfootImage?

Benni Benni

2017/4/30

#
I want to add a black screen over my game, where parts, I was will be discovered. I already managed to create the black screen, but I don't know, how I can clear Parts of the black Image now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Dunkelheit extends Actor
{
    public Dunkelheit(int breite, int hoehe)
    {
        GreenfootImage Dunkelheit = new GreenfootImage(breite,hoehe);
        Dunkelheit.setColor(Color.BLACK);
        Dunkelheit.fill();
        setImage(Dunkelheit);
    }
 
    public void act()
    {
    }
 
    public void quadratLoeschen(int x,int y,int laenge)
    {
        // Here should a square be deleted on position x,y with the side-length laenge
    }
danpost danpost

2017/4/30

#
About the only way to make the square transparent is by set each pixel individually (there is another way which involves creating the four side images and drawing them onto a blank image of equals size to the original image).
1
2
3
4
5
6
7
8
public void quaratLoeschen(int x, int y, int laenge)
{
    Color trans = new Color(0, 0, 0, 0);
    for (int col=0; col<laenge; col++) for (int row=0; row<laenge; row++)
    {
        getImage().setColorAt(x+row, y+col, trans);
    }
}
Busch2207 Busch2207

2017/4/30

#
Hey! In this case you have to work with Non-Greenfoot-Classes (AlphaComposite, Graphics2D and Rectangle). You can use this: (I also added a 'reset'-method in case you want to be able to reset the black screen)
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
public class Dunkelheit extends Actor
{
    private Graphics2D g2d;
    public Dunkelheit(int breite, int hoehe)
    {
        GreenfootImage Dunkelheit = new GreenfootImage(breite,hoehe);
        Dunkelheit.setColor(Color.BLACK);
        Dunkelheit.fill();
        setImage(Dunkelheit);
        g2d=Dunkelheit.getAwtImage().createGraphics();
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR,1));
    }
 
    public void act()
    {
    }
 
    public void quadratLoeschen(int x,int y,int laenge)
    {
        g2d.fill(new Rectangle(x-laenge/2,y-laenge/2,laenge,laenge));
    }
 
    public void reset()
    {
        getImage().fill();
    }
}
Don't forget following imports:
1
2
3
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.AlphaComposite;
You need to login to post a reply.