TITLE:TrayIconのアニメーション

Posted by at 2007-02-05

TrayIconのアニメーション

SystemTrayに追加したアイコン(JDK 6以上)をアニメーションさせます。

AnimatedTrayIcon.png

サンプルコード

final TrayIcon icon = new TrayIcon(imglist[0], "TRAY", popup);
animator = new javax.swing.Timer(100, new ActionListener() {
  private int idx = 0;
  @Override public void actionPerformed(ActionEvent e) {
    icon.setImage(imglist[idx]);
    idx = (idx<imglist.length-1)?idx+1:0;
  }
});
View in GitHub: Java, Kotlin

解説

16*16の画像を3パターン用意し、これをJDK 6で追加されたTrayIcon#setImage(Image)メソッドを使って切り替えることでアニメーションしています。

参考リンク

コメント

  • animated gifを使うほうが簡単だと思うのですが、作ったGifが悪いのか、環境のせいなのか、残像がでてしまいます。 -- aterai
    • メモ: Bug ID: 6453582 Animation gif too fast -- aterai
    • 上のバグ?でウェイトが効かずに残像が残っていたのではなく、前のコマがそのまま残して透過色(背景色)でクリアしないタイプ?のアニメGIFになっていたようです。Giamを使って、全コマの消去方法を「背景色で塗りつぶす」に変更したファイルを使用すると、正常に描画されるようになりました。 -- aterai