So I have written a Java application, that provides a transparent Heads up display at the top of the screen, it works perfectly on windows, but on my kubuntu 16.04 machine it does not clear the old label when you change the labels text, you end up with a ton of overlapping mess.
because a picture is worth a thousand words, the top is how it looks in windows, the bottom is how it looks under kubuntu:
https://s23.postimg.org/yra0vvlvf/rawr.png
here is the code:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.net.URL;
import javax.swing.*;
import java.io.*;
public class spob extends JFrame implements WindowFocusListener
{
public spob()
{
if (!SystemTray.isSupported()) {
System.out.println("SystemTray is not supported");
return;
}
final TrayIcon trayIcon = new TrayIcon((new ImageIcon("icon.png", "trayicon")).getImage());
final SystemTray tray = SystemTray.getSystemTray();
trayIcon.setImageAutoSize(true);
trayIcon.setToolTip("spO2 pr monitor");
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.out.println("TrayIcon could not be added.");
return;
}
setType(javax.swing.JFrame.Type.UTILITY);
setUndecorated(true);
getContentPane().setBackground(new Color(1.0f,1.0f,1.0f,0.0f));
setBackground(new Color(1.0f,1.0f,1.0f,0.0f));
setSize(400, 35);
JLabel label = new JLabel("Loading...");
label.setFont(new Font("Tahoma", Font.BOLD, 28));
label.setForeground(Color.GREEN);
add(label);
setLocation(800, 0);
addWindowFocusListener(this);
setAlwaysOnTop( true );
this.setFocusable(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
URL url = null;
BufferedReader in = null;
String[] anArray = new String[10];
anArray[0] = "<html><font color=green>- spO2:91 pr:65</font></html>";
anArray[1] = "<html><font color=red>+ spO2:85 pr:77</font></html>";
anArray[2] = "<html><font color=green>- spO2:90 pr:68</font></html>";
anArray[3] = "<html><font color=orange>+ spO2:89 pr:76</font></html>";
anArray[4] = "<html><font color=orange>- spO2:89 pr:72</font></html>";
anArray[5] = "<html><font color=orange>+ spO2:88 pr:73</font></html>";
anArray[6] = "<html><font color=red>- spO2:87 pr:78</font></html>";
anArray[7] = "<html><font color=red>+ spO2:86 pr:73</font></html>";
anArray[8] = "<html><font color=green>- spO2:92 pr:74</font></html>";
anArray[9] = "<html><font color=green>+ spO2:90 pr:71</font></html>";
while (true){
try {
Thread.sleep(200);
//url = new URL("http://192.168.1.153/stat.php");
//in = new BufferedReader(new InputStreamReader(url.openStream()));
//label.setText(in.readLine().toString());
Random randomno = new Random();
label.setText(anArray[randomno.nextInt(9 - 1) + 1]);
} catch (Exception ex) {
} finally {
//try {
// in.close();
//} catch (IOException e) {
//}
}
}
}
public void windowGainedFocus(WindowEvent e){}
public void windowLostFocus(WindowEvent e)
{
if(e.getNewState()!=e.WINDOW_CLOSED){
setAlwaysOnTop(false);
setAlwaysOnTop(true);
}
}
public static void main(String[] args)
{
new spob();
}
}