Hey guys, i have an image on my screen that I want to go on and off every half second, so it has a flashing affect. i am using Netbeans and J2Me and I want to know the best and simplest most efficient way to do this, any help is appreciated!

Thanks!!

Dani AI

Generated

— the GIF not animating is a very common MIDP surprise: the MIDP spec does not guarantee animated‑GIF support, so many phones (and some emulators) will show only the first frame. was right to suggest a thread-based swap — that is the most portable solution. Below is a small, proven pattern that works across MIDP 1/2 phones: run a short animation thread that toggles a boolean and calls repaint(), and start/stop that thread in showNotify/hideNotify so it does not waste CPU or battery.

import javax.microedition.lcdui.*;

public class BlinkCanvas extends Canvas implements Runnable {
    private Image img;
    private boolean visible = true;
    private boolean running;
    private Thread thread;

    public BlinkCanvas() {
        try {
            img = Image.createImage("/pressstart.png");
        } catch (Exception e) { /* handle missing resource */ }
    }

    protected void paint(Graphics g) {
        g.setColor(0); g.fillRect(0,0,getWidth(),getHeight());
        if (visible && img != null) {
            int x = (getWidth() - img.getWidth()) / 2;
            int y = (getHeight() - img.getHeight()) / 2;
            g.drawImage(img, x, y, Graphics.TOP | Graphics.LEFT);
        }
    }

    protected void showNotify() {
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    protected void hideNotify() {
        running = false;
        thread = null;
    }

    public void run() {
        while (running) {
            visible = !visible;
            repaint();
            try { Thread.sleep(500); } catch (InterruptedException ignored) {}
        }
    }
}

Troubleshooting tips: confirm the image is packaged in the JAR (case‑sensitive path, leading slash for resource lookup), test on an actual handset if the emulator differs, and catch any loading exceptions to detect packaging errors. Keep the blink rate moderate (around 500 ms or slower) and offer a way to disable it — rapid flashing can annoy users and can pose health risks for sensitive people.

Recommended Answers

All 2 Replies

1) Use animated GIF
2) Run thread that will switch between 2 different images

PS: Flashing things are irritating, nobody like it in the applications. Remember that!

Thanks for the tips Budo, I am actually just wanting press start to flash on and off on my start screen and trying to figure out how to do it, I could not get a gif to work, can you give any more explanation towards using a gif please?

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.