4

In my applet I make use of different BufferedImages and use them as screen parts. Each screen part will only be repainted when the content needs to change.

This is the abstract ScreenPart class:

public abstract class ScreenPart extends BufferedImage{
    Graphics2D g;

    private BufferedImage buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);

    public ScreenPart(int width, int height) {
        super(width, height, BufferedImage.TYPE_INT_ARGB);
        g = createGraphics();
        repaint();
    }

    public abstract void paint(Graphics2D g);

    public void repaint(){
        g.drawImage(buffer, 0, 0, null);
        paint(g);
    }
}

But the buffer doesn't work because the buffer is also transparent. It will work when I change the BufferedImage type of the buffer from ARGB to RGB but this displays also a black background. So my question is: how can I correctly repaint this BufferedImage with a buffer?

eboix
  • 5,113
  • 1
  • 27
  • 38
Jochem Gruter
  • 2,813
  • 5
  • 21
  • 43

1 Answers1

0

Already found a solution:

public void repaint() {
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR));
    g.fillRect(0,0, getWidth(), getHeight());
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
    paint(g);
}

This doesn't make use of another BufferedImage.

eboix
  • 5,113
  • 1
  • 27
  • 38
Jochem Gruter
  • 2,813
  • 5
  • 21
  • 43