当 window 背景具有非零不透明度时,JComponent repaint() 失败

JComponent repaint() fails when window background has non-zero Opacity

我已经卡在这个问题上一天了。我正在尝试动态更新我在透明无边框 window 上绘制的 BufferedImage。但是,一旦 window 背景 alpha 值设置为 full (1.0f);

以外的任何值,repaint() 就会停止工作

我已尝试 image_canvas.setOpaque(false),但未能解决问题。

如果有人能找到允许组件 repaint() 在透明背景上工作的解决方法,请告诉我。

重现问题的代码如下:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Window;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;

public class Dynamic_Buffered_Image2 {

    static Window win;
    static JComponent image_canvas;
    static BufferedImage my_image;

public static void main(String[] args) throws InterruptedException {
    // Create a test BufferedImage:
    my_image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    for (int i = 0; i < 100; i++)
        my_image.setRGB(i, i, Color.BLUE.getRGB());

    // Create display window:
    create_win();

    // Modify the BufferedImage dynamically:
    for (int i = 0; i < my_image.getWidth(); i++) {
        for (int j = 0; j < my_image.getHeight(); j++) {
            my_image.setRGB(i, j, Color.red.getRGB());
            Thread.sleep(10);
            win.repaint();
        }
    }
}

private static void create_win() {
    win = new Window(null);
    win.setSize(150, 150);
    image_canvas = new JComponent() {
        private static final long serialVersionUID = 1L;

        protected void paintComponent(Graphics g) {
            g.drawImage(my_image, 0, 0, null);
        }
    };
    win.add(image_canvas);
    win.setBackground(new Color(0, 0, 0, 1.0f)); // Any opacity other than 1.0f breaks repaint()??? (no red lines)
    win.setVisible(true);
}

}

我做了两处修改:

//win.repaint();
image_canvas.repaint();

和:

image_canvas.setOpaque(true); // added
win.add(image_canvas);