简体   繁体   中英

Java Timer not working

I have an Image named worldImageToUse and I have a Timer that is supposed to toggle worldImageToUse between two images every 1 second. But it does not seem to work. Help Please?

public void startWorldImageFlash() {
        worldImageFlashTimer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                if(worldImageToUse == worldImage) setWorldImageBW();
                if(worldImageToUse == worldImageBW) setWorldImageColor();
            }
        };
        worldImageFlashTimer.scheduleAtFixedRate(task, 0, 1000);
    }


public void stopWorldImageFlash() {
        worldImageFlashTimer.cancel();
        setWorldImageColor();
    }

Checked twice, change the second if with "else if", that will solve the problem. Also, you should consider debugging in such cases :)

It looks like your code says if color set to black and white. Then says if black and white set to color. Wouldn't you end up with the same image every time. Your second if needs to be an else if.

是否在设置图像后repaint()组件?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM