Java 应用程序的透明 JPanel 在 Kubuntu 上运行时出现乱码

Java 应用程序的透明 JPanel 在 Kubuntu 上运行时出现乱码

我有一个 Java 应用程序,它在 Windows 上运行良好,显然在 Mac 上也运行良好,但我无法让它在 Kubuntu 上运行,而不会出现重叠、乱码的输出。我尝试使用捆绑的 OpenJDK 以及 Oracle 的 Java 8 启动 Java 应用程序。

这是一张图片:

kubuntu java 问题

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.net.URL;
import javax.swing.*;
import javax.swing.Timer;
import java.io.*;

public class spob {

    public static void main(String[] args) {
        new spob();
    }

    private JLabel label;
    private String[] anArray = {
        "<html><font color=green>- spO2:91  pr:65</font></html>",
        "<html><font color=red>+ spO2:85  pr:77</font></html>",
        "<html><font color=green>- spO2:90  pr:68</font></html>",
        "<html><font color=orange>+ spO2:89  pr:76</font></html>",
        "<html><font color=orange>- spO2:89  pr:72</font></html>",
        "<html><font color=orange>+ spO2:88  pr:73</font></html>",
        "<html><font color=red>- spO2:87  pr:78</font></html>",
        "<html><font color=red>+ spO2:86  pr:73</font></html>",
        "<html><font color=green>- spO2:92  pr:74</font></html>",
        "<html><font color=green>+ spO2:90  pr:71</font></html>"
    };
    private Random randomno = new Random();

    public spob() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("spO2 pr");
                frame.setUndecorated(true);
                frame.setAlwaysOnTop(true);
                // Transparent window...
                frame.setBackground(new Color(255, 255, 255, 0));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                BackgroundPane pane = new BackgroundPane();
                // Set this to 0.0f to make it fully transparent
                pane.setAlpha(0.0f);
                //pane.setLayout(new BorderLayout());
                //pane.setBorder(new EmptyBorder(10, 10, 10, 10));
                frame.setContentPane(pane);

                label = new JLabel("-");
                label.setFont(new Font("Tahoma", Font.BOLD, 28));
                label.setForeground(Color.GREEN);
                frame.add(label);

                frame.pack();
                Dimension size = frame.getSize();
                size.width = 400;
                frame.setSize(size);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                Timer timer = new Timer(500, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        label.setText(anArray[randomno.nextInt(9 - 1) + 1]);
                        label.getParent().repaint();
                    }
                });
                timer.start();
            }
        });
    }

    public class BackgroundPane extends JPanel {

        private float alpha;

        public BackgroundPane() {
            setOpaque(false);
        }

        public void setAlpha(float alpha) {
            this.alpha = alpha;
            repaint();
        }

        public float getAlpha() {
            return alpha;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(getBackground());
            g2d.setComposite(AlphaComposite.SrcOver.derive(getAlpha()));
            g2d.fillRect(0, 0, getWidth(), getHeight());
            g2d.dispose();
        }

    }

}

我在 Oracle 网站上找到了测试给定系统支持的透明度的文档,并在 Windows 计算机上运行了测试。所有三个测试都返回 true,但在 Kubuntu 上只有 PerPixel 测试返回 true,均匀半透明测试返回 false。

这是一张图片:

kubuntu 结果

以下是使用的代码:

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import static java.awt.GraphicsDevice.WindowTranslucency.*;

public class test {
  public static void main(String[] args) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();

    boolean isUniform = gd.isWindowTranslucencySupported(TRANSLUCENT);
    boolean isPerPixel = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
    boolean isShaped = gd.isWindowTranslucencySupported(PERPIXEL_TRANSPARENT);

    GraphicsConfiguration[] configurations = gd.getConfigurations();
    System.out.println("Default screen device: " + gd.getIDstring());
    for (int i = 0; i < configurations.length; i++) {
      System.out.println("  Configuration " + (i + 1));
      System.out.println("  " + configurations[i].getColorModel());
    }
    System.out.println("isWindowTranslucencySupported Uniform: " + isUniform);
    System.out.println("isWindowTranslucencySupported PerPixel: " + isPerPixel);
    System.out.println("isWindowTranslucencySupported Shaped: " + isShaped);
  }
}

相关内容