2

It seems like clearRect doesn't set my BufferedImage to transparent but to black. Creating a new BufferedImage works, but I don't want to produce too much garbage. I have this code for my chess programm:

//graphics
private Graphics2D g2d;

private Graphics2D gLayer1;
private Graphics2D gLayer2;
private Graphics2D gLayer3;

public BufferedImage imgLayer1;
private BufferedImage imgLayer2;
private BufferedImage imgLayer3;

private Game() throws IOException {

    //initialize...

    imgLayer1 = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
    gLayer1 = imgLayer1.createGraphics();

    imgLayer2 = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
    gLayer2 = imgLayer2.createGraphics();

    imgLayer3 = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
    gLayer3 = imgLayer3.createGraphics();
}

//main
public static void main(String[] args) throws IOException {

    //initialize frame
}

public void run() {

    while (isRunning) {

        updateGraphicsLayer1();
        updateGraphicsLayer2();
        updateGraphicsLayer3();

        repaint();
    }
}

protected void paintComponent(Graphics g) {
    g2d = (Graphics2D) g;

    g.drawImage(imgLayer1, 0, 0, null);
    g.drawImage(imgLayer2, 0, 0, null);
    g.drawImage(imgLayer3, 0, 0, null);

    g2d.dispose();
}

public void updateGraphicsLayer1() {
    if (redrawLayer1) {

        gLayer1.clearRect(0, 0, WIDTH, HEIGHT);

        //draw this layer
    }
}

public void updateGraphicsLayer2() {
    if (redrawLayer2) {

        gLayer2.clearRect(0, 0, WIDTH, HEIGHT);

        //draw this layer
    }
}

public void updateGraphicsLayer3() {
    if (redrawLayer3) {

        gLayer3.clearRect(0, 0, WIDTH, HEIGHT);

        //draw this layer
    }
}

this is what I get:

enter image description here

what have I got to do? How can I clear my layers, so they are transparent?

Duke
  • 386
  • 2
  • 13
  • A general rule of thumb when overriding the `paintComponent` method is to always call `super.paintComponent(g)` before anything else. – CraigR8806 May 11 '17 at 17:38
  • If this solves your problem, let me know. Your code is too limited to be able to determine what the issue is with any certainty. For example, you have a `paintComponent()` method, but it is not overridden. Also, your class doesn't extend any `swing` components, but you call `repaint()` in context of your class not showing a `repaint()` method – CraigR8806 May 11 '17 at 17:41
  • Unfortunately didn't solve the problem – Duke May 11 '17 at 17:42
  • Okay, I would leave that line in there though because otherwise if your Panel gets large enough you will have unpredictable artifacts later on. Do you set the graphics color anywhere in your code? – CraigR8806 May 11 '17 at 17:44
  • Yes I do. Multiple times. Does this affect the clearRect method? – Duke May 11 '17 at 17:45
  • Not sure, let me do some research – CraigR8806 May 11 '17 at 17:46
  • Check out: http://stackoverflow.com/questions/43882050/bufferedimage-fill-rectangle-with-transparent-pixels/43891658#43891658 – camickr May 11 '17 at 17:53
  • this worked but somehow changed the color of the tiles :/ Does this affect the Graphic2D's colors? – Duke May 11 '17 at 18:05

2 Answers2

1

So, here's what I have done in the past... When I am painting a BufferedImage to a Component with swing, I will set the pixel color values individually.

This same concept can be applied to clearing a BufferedImage. You can set the pixels int[] by calling bi.setRGB(). There are quite a few parameters that you need to provide to set the array, but don't worry, I'll go over each one.

bi.setRGB(int startingX, int startingY, int width, int height, int[] sourceArray, int offset, int scanSize);

startingX and startingY should be 0 in this instance.

width and height are what you would expect them to be (bi.getWidth(), bi.getHeight())

sourceArray will be the int[] that you fill with 0s

offset should be 0 in this instance

scanSize will just be bi.getWidth()

So, lets look at some code to clear your BufferedImage

int[] pixels = new int[bi.getWidth()*bi.getHeight()];

Arrays.fill(pixels, 0);
bi.setRGB(0, 0, bi.getWidth(), bi.getHeight(), pixels, 0, bi.getWidth());

If this doesn't clear your BufferedImage, then either you're not creating the BufferedImage using BufferedImage.TYPE_INT_ARGB or you're reading the image in and it is only a 24-bit color image that lacks an Alpha channel

CraigR8806
  • 1,584
  • 13
  • 21
1

I also want to mention, that camickr's solution worked too:

link

g2d.setComposite(AlphaComposite.Clear);
g2d.fillRect(0, 0, (width), (height));
g2d.setComposite(AlphaComposite.SrcOver);
Community
  • 1
  • 1
Duke
  • 386
  • 2
  • 13