Graphics2D does not clear transparent image

I have the following code snippet, which should increment the counter on a transparent background on the first monitor. When the image shows 0, it displays fine, but after that (as soon as 1 hits) the window is redrawn with an opaque background.

The stupid practical example that I know just broke the real use case to a simpler part of the code.

It seems that the key could be in the paintComponent TestCanvas method:

g.setColor(new Color(0, 0, 0, 0));
g.clearRect(0, 0, getWidth(), getHeight());

From what I can fix, these two lines should set the drawing color to completely transparent, and then clear this area with this color - but this does not seem to be kept outside the first redraw.

EDIT: using fillRect instead of clearRect does not work, because it just draws a transparent rectangle on top of the existing image, so it never gets cleared. 1 is superimposed on 0, then 2 is superimposed on 1, etc.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;

public class LyricWindow extends JWindow {

    private final TestCanvas canvas;

    public LyricWindow(Rectangle area, boolean stageView) {
        setBackground(new Color(0, 0, 0, 0));
        setArea(area);
        canvas = new TestCanvas();
        canvas.setPreferredSize(new Dimension((int) (area.getMaxX() - area.getMinX()), (int) (area.getMaxY() - area.getMinY())));
        add(canvas);
        new Thread() {

            public void run() {
                for(int i = 0; true; i++) {
                    final int ii = i;
                    SwingUtilities.invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            canvas.setText(Integer.toString(ii));
                        }
                    });
                    try {
                        Thread.currentThread().sleep(200);
                    }
                    catch(InterruptedException ex) {}
                    System.out.println(ii);
                }
            }
        }.start();
    }

    public final void setArea(final Rectangle area) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                if(canvas != null) {
                    canvas.setPreferredSize(new Dimension((int) (area.getMaxX() - area.getMinX()), (int) (area.getMaxY() - area.getMinY())));
                }
                setSize((int) (area.getMaxX() - area.getMinX()), (int) (area.getMaxY() - area.getMinY()));
                setLocation((int) area.getMinX(), (int) area.getMinY());
            }
        });
    }

    public static void main(String[] args) {
        LyricWindow w = new LyricWindow(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getConfigurations()[0].getBounds(), false);
        w.setVisible(true);
    }
}

class TestCanvas extends JPanel {

    private String text;

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(new Color(0, 0, 0, 0));
        g.clearRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.RED);
        g.drawString(text, 100, 100);
    }

    public void setText(String s) {
        text = s;
        repaint();
    }
}
+3
source share
4 answers

It turned out that before drawing I needed to set the correct composite value. Adding

((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));

before the method paintComponent(), then using fillRect(), did the trick!

+2
source

Try g.fillRect (...) instead of g.clearRect (...). I think I once encountered a similar problem, and this may have solved this for me.

I found my original solution to the problem to use with BufferedImages. Not sure why White can work and black won't, but try:

g.setBackground(new Color(255, 255, 255, 0));
g.clearRect(0, 0, width, height);

Color Javadoc:

1,0 255 , - 0 0,0 , .

+2

, -?

- 255 0. , "" (0, 0, 0), , "" clearRect, fillRect.

0

, , JPanel, , ?

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(new Color(0, 0, 0, 0));
    g.clearRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.RED);
    g.drawString(text, 100, 100);
}

.

0

All Articles