Windows上のJavaで半透明のローディングアニメーションを作成しようとしています。
JlabelのsetIconにImageIconを、順番に指定することでダミー画像をアニメーションさせることは成功しているのですが、半透明のpngファイルを使用しているためか、指定するたびに以下のように画像が合成されてしまいます。
そこで、以下のソースのように、setIconにnullを指定して画像を消した後に、次の画像を表示しようとしたのですが、setIconにnullを指定しても画像がクリアされません。
以下のクラスを、ダミーのmainから呼び出しています。
package customwaitanimation; import java.awt.Color; import java.awt.Container; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class CustomWaitAnimation extends Thread { private boolean running; private JFrame frame; private JPanel panel; private JLabel label1; private Container contentPane; private static final int ANIMATION_FRAMES = 10; // コンストラクタ public CustomWaitAnimation() { // this.running = true; // this.frame = new JFrame(); this.frame.setSize(110, 110); this.frame.setAlwaysOnTop(true); this.frame.setLocationRelativeTo(null); this.frame.setUndecorated(true); this.frame.setBackground(new Color(0, 0, 0, 0)); this.panel = new JPanel(); this.panel.setBackground(new Color(0, 0, 0, 0)); ImageIcon icon = new ImageIcon("loading01.png"); this.label1 = new JLabel(); this.label1.setIcon(icon); this.panel.add(this.label1); this.contentPane = this.frame.getContentPane(); this.contentPane.add(this.panel); } @Override public void run() { this.frame.setVisible(true); int index = 1; while(this.running) { //画像を消したあと次の画像をセットする this.label1.setIcon(null); String filename = "loading" + String.format("%02d", index) + ".png"; ImageIcon icon = new ImageIcon(filename); this.label1.setIcon(icon); //ファイル名のインデックスをインクリメントorリセット if(index >= ANIMATION_FRAMES) { index = 1; } else { index ++; } //0.1秒更新 try { sleep(100); } catch (InterruptedException ex) { Logger.getLogger(CustomWaitAnimation.class.getName()).log(Level.SEVERE, null, ex); } } this.frame.setVisible(false); } public void stopRunning() { // this.running = false; } }
ダミーのmain
import customwaitanimation.CustomWaitAnimation; public class TestMain { public static void main(String[] args) { CustomWaitAnimation w = new CustomWaitAnimation(); w.start(); try{ Thread.sleep(10000); }catch(InterruptedException e){} w.stopRunning(); } }
nullを指定すると、画像がクリアされるという認識だったのですが、間違っているでしょうか?
また、このような状況になった方や、回避策をご存知の方はいらっしゃらないでしょうか?
使用しているツールのバージョンは、
・Windows10 pro
・NetBeans 8.1
・Java 1.7.0
です。
回答3件
あなたの回答
tips
プレビュー