0

I am currently making a 2D game, and I am in the process of making shadows. I have a buffered image background which is just a black rectangle to put over the screen, and a transparent triangle buffered image that will be used for the players viewed distance. What I want to do, is over my game draw the black background buffered image, and then where the player is, draw the transparent triangle and the black background does not show up inside this transparent triangle.

This might give you more insight on what I want to do: Image of what I want

Here is my shadow class:

private BufferedImage diamond;
private BufferedImage background;

public Shaders(World world) {
    try {
        diamond = ImageIO.read(getClass().getResourceAsStream("/GUI/DIAMOND.png"));
        background = ImageIO.read(getClass().getResourceAsStream("/GUI/BACKGROUND.png"));
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    this.world = world;
}

public void draw(Graphics g2) {
}
Magnilex
  • 11,584
  • 9
  • 62
  • 84
Avery246813579
  • 199
  • 1
  • 11
  • possible duplicate of [How to create a circular bufferedimage rather than creating a rectangular one in Java using Graphics](http://stackoverflow.com/questions/23669333/how-to-create-a-circular-bufferedimage-rather-than-creating-a-rectangular-one-in) – Harald K Feb 10 '15 at 09:29
  • looks like your image is **not** transparent!? maybe you must make it transparent first... – Martin Frank Feb 10 '15 at 09:54
  • if you want to know how to make a certain color transparent, then look at http://stackoverflow.com/questions/3101446/how-to-convert-an-image-into-a-transparent-image-in-java – Martin Frank Feb 10 '15 at 09:57

1 Answers1

0

If I understand your problem correctly, the visual effect you are trying to create is to overlay your game scene with a black-out except for a "triangle" (actually a rotated square) through which a portion of your game scene is visible.

I have a couple of suggestions (of which the second would be my recommendation):

  1. Create your background graphic with a transparent hole. Scale the entire graphic so the hole is the size you want, then draw it over your scene. The background graphic has to be large enough that it extends to the borders of the scene even at the smallest size of the hole.

  2. Rather than using a graphic image for the black-out, simply draw a filled polygon that contains the hole. You can use a GeneralPath to define a complex polygon or (my preference) chop your black-out in half (say, horizontally) and draw each half separately.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521