Redraw argb BufferedImage

In my applet, I use different BufferedImage and use them as part of the screen. Each part of the screen will be repainted only when the content should 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 does not work, because the buffer is also transparent. It will work when I change the BufferedImage buffer type from ARGB to RGB, but it will also display a black background. So my question is: how can I correctly redraw this BufferedImage using a buffer?

+4
source share
1 answer

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); } 

It does not use another BufferedImage .

0
source

Source: https://habr.com/ru/post/1435751/


All Articles