Swing/DisableAnimatedGif のバックアップの現在との差分(No.5)
- category: swing folder: DisableAnimatedGif title: JLabelに表示したAnimated Gifのアニメーションを停止する tags: [JLabel, Animation, ImageIcon] tags: [JLabel, Animation, AnimatedGif, ImageIcon] author: aterai pubdate: 2013-02-25T00:50:43+09:00 description: JLabelに表示したAnimated GIFのアニメーションを停止します。 image:
概要
JLabel
に表示したAnimated GIF
のアニメーションを停止します。
Screenshot
Advertisement
サンプルコード
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解説
上記のサンプルでは、JLabel
がEnabled
でない場合にアニメーションを停止するよう設定しています。
上記のサンプルでは、JLabel#isEnabled()
がfalse
の場合はsetIcon()
メソッドで設定したAnimated Gif
のアニメーションを停止するなどのテストを行っています。
Default
- デフォルトの
JLabel
では、JLabel#setEnabled(false);
としてもアニメーションは停止しない - デフォルトの
JLabel
ではJLabel#setEnabled(false)
を設定してもアニメーションは停止しない
- デフォルトの
Override imageUpdate(...)
JLabel#imageUpdate(...)
のinfoflags
からFRAMEBITS
フラグを落とすことでアニメーションを停止-
JLabel
がリサイズされると?、コマが進んでしまう -
JLabel
がリサイズされると?コマが進んでしまう
setDisabledIcon
- 別途用意した静止画像を使って、
JLabel#setDisabledIcon(...)
を設定 - 別途用意した静止画像を使って
JLabel#setDisabledIcon(...)
を設定 GrayFilter.createDisabledImage(Image)
でアイコンをグレースケール化
- 別途用意した静止画像を使って、
参考リンク
- Duke Images: iconSized
- ImageObserver (Java Platform SE 6)
- ImageObserver (Java Platform SE 8)
- ColorConvertOpで画像をグレースケールに変換
- Animated Gifからフレーム画像を抽出する