ということで、透過ウィンドウは諦めて、ここにあるコードを使ってやってみました。
これで実質成功に見えるのですが、他のウィンドウを動かして、マスコットの下にやると、コンポーネントの再描画が行われず以下の様になります。
タイマーで無理やり再描画させても全く変わりません。
どうすればいいのでしょう。
Sceon.java
import java.io.*;
import java.util.Properties;
import javax.swing.*;
class Sceon{
//setting variant
private static int WinX;
private static int WinY;
private static int adelay;
private static int msgd;
//component variant
private static SceonWindow mwin;
//images
private static ImageIcon imgs[];
private static int modeimg;
private static Properties readini(String filename){
Properties conf = new Properties();
try {
conf.load(new FileInputStream(filename));
} catch (IOException e) {
System.err.println("Cannot open " + filename + ".");
e.printStackTrace();
System.exit(-1); // プログラム終了
}
return conf;
}
public static void refSetting(){
Properties sysconf = readini("sysconf.ini");
WinX = Integer.parseInt(sysconf.getProperty("WinX"));
WinY = Integer.parseInt(sysconf.getProperty("WinY"));
imgs[0] = new ImageIcon(sysconf.getProperty("Normal"));
imgs[1] = new ImageIcon(sysconf.getProperty("Trouble"));
imgs[2] = new ImageIcon(sysconf.getProperty("Smile"));
imgs[3] = new ImageIcon(sysconf.getProperty("Angry"));
modeimg = Integer.parseInt(sysconf.getProperty("DefaultMode"));
adelay = Integer.parseInt(sysconf.getProperty("ActionDelay"));
msgd = Integer.parseInt(sysconf.getProperty("MessageDelay"));
}
public static void main(String[] args){
//Load Setting
imgs = new ImageIcon[4];
refSetting();
//Initialize MainWindow
mwin = new SceonWindow(WinX,WinY,adelay,msgd);
//Visible Image
mwin.SetPicture(imgs[0]);
}
}
import javax.swing.*;
import java.awt.*;
public class TransparentBackground extends JComponent {
private JFrame frame;
private Image background;
public TransparentBackground(JFrame frame) {
this.frame = frame;
updateBackground( );
}
public void updateBackground( ) {
try {
Robot rbt = new Robot( );
Toolkit tk = Toolkit.getDefaultToolkit( );
Dimension dim = tk.getScreenSize( );
background = rbt.createScreenCapture(
new Rectangle(0,0,(int)dim.getWidth( ),
(int)dim.getHeight( )));
} catch (Exception ex) {
//p(ex.toString( ));何かエラー出るから消しておく
ex.printStackTrace( );
}
}
public void paintComponent(Graphics g) {
Point pos = this.getLocationOnScreen( );
Point offset = new Point(-pos.x,-pos.y);
g.drawImage(background,offset.x,offset.y,null);
}
}
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
class SceonWindow extends JFrame implements ActionListener{
private TransparentBackground bg;
private int WinX,WinY;
private JLabel pic;
private Timer timer;
private JLabel msg;
private int vt;
private int vtd;
public SceonWindow(int winw,int winh,int ad,int vt_){
//Variant Initialize
WinX = winw;
WinY = winh;
vtd = vt_;
//JFrame Initialize
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(screen.width - WinX,screen.height - WinY);
setSize(WinX,WinY);
setAlwaysOnTop(true);//toBack();
setUndecorated(true);
//setBackground(new Color(0, 0, 0, 0));
setDefaultCloseOperation(3);
//bg component
bg = new TransparentBackground(this);
bg.setLayout(new BorderLayout( ));
//JLabel Initialize
pic = new JLabel();
pic.setOpaque(false);
bg.add(pic);//add(pic);
//Msg Initialize
msg = new JLabel("あいうえお",SwingConstants.CENTER);
vt = 0;
msg.setLayout(new BorderLayout());
msg.setHorizontalAlignment(JLabel.CENTER);
msg.setFont(new Font(Font.DIALOG,Font.PLAIN, 16));
msg.setBorder(new LineBorder(Color.black,2,true));
bg.add(msg,BorderLayout.PAGE_START);//add(msg,BorderLayout.PAGE_START);
//Timer Initialize
timer = new Timer(ad,this);
timer.setActionCommand("update");
Timer ptime = new Timer(10,this);
ptime.setActionCommand("paint");
//Visualize
getContentPane().add(bg,BorderLayout.CENTER);
setVisible(true);
timer.start();
ptime.start();
}
public void SetMsg(String setm){
msg.setText(setm);
vt = vtd;
}
public void SetPicture(ImageIcon img){
pic.setIcon(img);
pic.repaint();
}
public void actionPerformed(ActionEvent e){
bg.repaint();
if(e.getActionCommand()=="paint")return;
System.out.println("Event start...");
//Form Update
if(vt>0){
vt--;
msg.setVisible(true);
}else msg.setVisible(false);
msg.repaint();
}
}
環境はLinux beanです。
Javaバージョン