TITLE:JLabelに表示したAnimated Gifのアニメーションを停止する

Posted by at 2013-02-25

JLabelに表示したAnimated Gifのアニメーションを停止する

`JLabelに表示したAnimated Gif`のアニメーションを停止します。

  • &jnlp;
  • &jar;
  • &zip;
DisableAnimatedGif.png

サンプルコード

JLabel label2 = new JLabel() {
  @Override public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) {
    if(!isEnabled()) {
      infoflags &= ~FRAMEBITS;
    }
    return super.imageUpdate(img, infoflags, x, y, w, h);
  }
};
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、`JLabelEnabled`でない場合にアニメーションを停止するよう設定しています。

  • `Default`
    • デフォルトの`JLabelでは、JLabel#setEnabled(false);`としてもアニメーションは停止しない
  • `Override imageUpdate(...)`
    • `JLabel#imageUpdate(...)infoflagsからFRAMEBITS`フラグを落とすことでアニメーションを停止
    • `JLabel`がリサイズされると?、コマが進んでしまう
  • `setDisabledIcon`
    • 別途用意した静止画像を使って、JLabel#setDisabledIcon(...)を設定
    • `GrayFilter.createDisabledImage(Image)`でアイコンをグレースケール化

コメント