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

2012/5/18

Light in a Dark Room

1
2
3
RM6442 RM6442

2012/5/18

#
Is there any way to essentially "cut-out" a circle out of a black image? What I want to do is create a solid black image that covers the world with a small circle of transparency around one object (or more). Any way of doing this?
danpost danpost

2012/5/18

#
Use the fillOval method on the image, using 'new Color(0, 0, 0, 0)' as the color (transparent).
RM6442 RM6442

2012/5/18

#
That doesn't sound like it should work.
RM6442 RM6442

2012/5/18

#
Do you want something like this?
    public void light(int x, int y)
    {
        dark.clear();
        dark.setColor(Color.BLACK);
        dark.fillRect(0,0,dark.getWidth(),dark.getHeight());
        dark.setColor(new Color(0,0,0,0));
        dark.fillOval(x-5,y-5,x+5,y+5);
        setImage(dark);
    }
... because that doesn't work.
Builderboy2005 Builderboy2005

2012/5/18

#
It doesn't, it will just draw a completely transparent oval (ie: nothing!). What you would need to do is actually a bit more complicated. We will use AlphaComposite's in order to control how drawing effects what is changed in the image. In order to do this, we also need to create a Graphics2D to write to the Image:
GreenfootImage myImage = ...;
BufferedImage myBuffer = myImage.getAwtImage();
Graphics2D myGraphics = myBuffer.createGraphics();
AlphaComposite compClear = AlphaComposite.CLEAR;

myGraphics.setComposite(compClear);
myGraphics.fillOval(...);
That should work! This will cause anything you draw to instead "paint" transparency onto the image.
RM6442 RM6442

2012/5/18

#
When I try this: AlphaComposite compClear = AlphaComposite.CLEAR; it brings up "incompatible types".
Builderboy2005 Builderboy2005

2012/5/18

#
Oh whoops, I should have said AlphaComposite.Clear instead of AlphaComposite.CLEAR.
kartikitrak kartikitrak

2012/5/18

#
I don't want to interrupt this discussion, but could someone help me with this. http://www.greenfoot.org/scenarios/5196
RM6442 RM6442

2012/5/18

#
How dare you interrupt my very important conversation! JK. I like the game. Are you having problems having the entire set of aliens dropping down, instead of just the ones that touch the sides?
Builderboy2005 Builderboy2005

2012/5/18

#
He explains the issues more completely in his other thread, you can check out the code and info there! I myself already offered a suggestion that I think will make life a bit easier.
RM6442 RM6442

2012/5/18

#
OK, changed my code. And...
public void light(int x, int y)
    {
        GreenfootImage myImage = new GreenfootImage(600, 400); 
        java.awt.image.BufferedImage myBuffer = myImage.getAwtImage();  
        Graphics2D myGraphics = myBuffer.createGraphics();  
        AlphaComposite compClear = AlphaComposite.Clear;  
      
        myGraphics.setComposite(compClear);  
        myGraphics.fillOval(x-5,y-5,x+5,y+5);
        setImage(dark);
    }
Nothing
Builderboy2005 Builderboy2005

2012/5/18

#
Lol well you are using setImage() on the image 'dark' instead of the image 'myImage'. Since myGraphics controls myBuffer controls myImage, when you draw to myGraphics you will only be modifying myImage. Either setImage(myImage), or create a buffer from the image you actually want to modify.
danpost danpost

2012/5/18

#
Yeah, none of those methods in Greenfoot will work for what you want. But you could brut force it with the following method, if you already set the image to a completely black one.
public void createPortal(int x, int y, int r) // x and y are the center and r  is the radius
{
    GreenfootImage img = getImage();
    Color color = Color.white;
    Color trans = new Color(0, 0, 0, 0);
    img.setColor(color);
    img.fillOval(x - r, y - r, r * 2, r * 2);
    for (int i = 0; i < r * 2; i++)
    {
        for (int j = 0; j < r * 2; j++)
        {
            Color here = img.getColorAt(x - r + i, y - r + j);
            if (color.equals(here)) img.setColorAt(x - r + i, y - r + j, trans);
        }
    }
    setImage(img);
}
RM6442 RM6442

2012/5/18

#
Well setImage(myImage) doesn't work. How do you do that other thing?
RM6442 RM6442

2012/5/18

#
danpost's works great. Can it work with multiple objects?
There are more replies on the next page.
1
2
3