×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Nov 2009
    Posts
    63

    Using Thread.sleep()

    Hello,
    I wanted to add a start up screen showing the app-icon at the start of the app, like janole did this in his Gravity-app.
    But I'm not able to get the app to stop to show the screen for 2/3 seconds and then go on. I tried it with several positions of Thread.sleep();

    Here's the code I used, I hope you can help me.

    Main:
    Code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.lcdui.Graphics.*;
    
    public class WahlZufall extends MIDlet {
        boolean start;
        public WahlZufall() {
            start = false;
        }
    
        protected void startApp() {
            if (start = false) {
                Display.getDisplay(this).setCurrent(new StartUp(this));
                try {
                    Thread.sleep(5000);
                } catch (Exception e) {
                }
                start = true;
            }
    	Display.getDisplay(this).setCurrent(new MyCanvas(this));
        }
    
        protected void pauseApp() {}
        protected void destroyApp(boolean unconditional) {}
    }
    The class with the icon:
    Code:
    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.lcdui.Image;
    
    public class StartUp extends Canvas {
        WahlZufall midlet;
        Image icon;
        //Die Größe des Bildschirms
        int width;
        int height;
    
        public StartUp (WahlZufall _midlet) {
            try {
                icon = Image.createImage("/icon.png");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        protected void paint (Graphics g) {
            g.setColor(0x00f49f60);
            g.fillRect(0, 0, sW(360), sH(640));
            g.drawImage(icon, sW(180), sH(320), Graphics.HCENTER|Graphics.VCENTER);
        }
    
        //Veränderung der Höhe bei Drehen des Gerätes
        protected void sizeChanged(int w, int h) {
            (...)
        }
    
        public int sW(int z) {
            (...)
        }
        public int sH(int z) {
            (...)
        }
    }

  2. #2
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Using Thread.sleep()

    Use synchronised() and wait().

    Try this code instead that displays a splash screen (/splash.png image should be placed in your resources) for 3 seconds and then resumes the midlet's operation from the splashScreenDone() method:

    SplashScreenMidlet
    Code:
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.Image;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
    import javax.microedition.io.Connector;
    
    public class SplashScreenMidlet extends MIDlet {
    
    	protected void destroyApp(boolean unconditional)
    			throws MIDletStateChangeException {
    	}
    	protected void pauseApp() {
    	}
    
    	protected void startApp() throws MIDletStateChangeException {
    
                Display.getDisplay(this).setCurrent(new SplashScreen(this));
    	}
    
    
    	void splashScreenDone()
    	{
    		//Resume Code from here		
    	}
    	
    	static Image createImage(String filename)
    	{
    		Image image = null;
    	    try
    	    {
    	    	image = Image.createImage(filename);
    	    }
    	    catch (java.io.IOException ex)
    	    {
    	    	System.out.println(ex.getMessage());ex.printStackTrace();
    	    }
    	    return image;
    	}
    
    }
    SplashScreen
    Code:
    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.lcdui.Image;
    
    public class SplashScreen extends Canvas implements Runnable
    {
        private final SplashScreenMidlet midlet;
        private Image splashImage;
        private volatile boolean dismissed = false;
        
        SplashScreen(SplashScreenMidlet midlet)
        {
            this.midlet = midlet;
            setFullScreenMode(true);
            splashImage = SplashScreenMidlet.createImage("/splash.png");
            new Thread(this).start();
        }
        
        public void run()
        {
            synchronized(this)
            {
                try
                {
                    wait(3000L);   // 3 seconds
                }
                catch (InterruptedException e)
                {
                    // can't happen in MIDP: no Thread.interrupt method
                }
                dismiss();
            }
        }
        public void paint(Graphics g)
        {
            int width = getWidth();
            int height = getHeight();
            g.setColor(0x00FFFFFF);  // white
            g.fillRect(0, 0, width, height);
            g.setColor(0x00FF0000);  // red
            g.drawRect(1, 1, width-3, height-3);  // red border one pixel from edge
            if (splashImage != null)
            {
                g.drawImage(splashImage,
                            width/2,
                            height/2,
                            Graphics.VCENTER | Graphics.HCENTER);
                splashImage = null;
            }
        }
    
        public synchronized void keyPressed(int keyCode)
        {
            dismiss();
        }
    
        private void dismiss()
        {
            if (!dismissed)
            {
                dismissed = true;
                midlet.splashScreenDone();
            }
        }
    
    }

  3. #3
    Regular Contributor
    Join Date
    Nov 2009
    Posts
    63

    Re: Using Thread.sleep()

    wow, thanks for answering!

    the code works awesome, thank you

    now I need to understand what it means...
    will ask if I have a question anywhere

  4. #4
    Nokia Developer Champion
    Join Date
    Apr 2007
    Posts
    2,708

    Re: Using Thread.sleep()

    also just to have an idea : there should be an example of a splash screen in the Wiki, thats what you are looking for and how such a screen is called.
    Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them.

Similar Threads

  1. Thread.sleep in while loop
    By oaieuy in forum Mobile Java General
    Replies: 2
    Last Post: 2010-02-24, 16:22
  2. Thread.Sleep
    By ResHAcK in forum Mobile Java General
    Replies: 2
    Last Post: 2008-09-25, 12:28
  3. Sleep and Resume thread
    By felixksp in forum Mobile Java General
    Replies: 3
    Last Post: 2007-05-23, 10:42
  4. thread sleep
    By nodevelop in forum Mobile Java General
    Replies: 3
    Last Post: 2003-09-14, 19:56
  5. Problems with Thread.sleep()
    By tjessy in forum Mobile Java Media (Graphics & Sounds)
    Replies: 3
    Last Post: 2003-08-31, 23:06

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •