×

Discussion Board

Results 1 to 9 of 9

Thread: Timer Class

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    45

    Question Timer Class

    Can any body pls give me a small example for using a timer class. Actually i want to display a particular screen for a particular time and that screen should be destroyed after some time and new menu screen must be displayed, so how can i do that with the help of timer class. Can anybody pls give me a small example for using timer class also i dont hav much knowledge about timer class so can anybody pls tell me any book so that i can refer that book and grab the knowledge about timer class.

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Timer Class

    I'd avoid Timer. Try something like this...

    Code:
    public class DisplayTimer implements Runnable {
        private Display display;
        private Displayable next;
        private int delay;
    
        public DisplayTimer(Display d, Displayable first, Displayable second, int milliseonds) {
            d.setCurrent(first);
            display = d;
            next = second;
            delay = milliseconds;
            (new Thread(this)).start();
        }
    
        public void run() {
            try {
                Thread.sleep(delay);
            } catch (InterruptedException ie) {
                // ignore
            }
            display.setCurrent(next);
        }
    }
    Graham.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    45

    Re: Timer Class

    Thnak u for ur reply but following is the code which i have written

    Code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
    public class Menu extends MIDlet{
    	
    	public static Display display;
    	CanvasMenu canvas;
    	SplashScreen ss;
    
    	protected void startApp()
    	{
            display=Display.getDisplay(this);
    		//canvas=new CanvasMenu(this);
    		//display.setCurrent(canvas);
    		ss=new SplashScreen(this);
    		display.setCurrent(ss);
    	}
    	protected void pauseApp()
    	{
    
    	}
    	protected void destroyApp(boolean unconditional)
    	{
    		
    	}
    
    	
    }
    tis is my manin class which extends MIDlet now from this class i call the SplashScreen class which is as follows

    Code:
    import java.util.Timer;
    import java.util.TimerTask;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
    
    public class SplashScreen extends Canvas 
    {
    	public Image img;
    	private Menu menu;
    	
    	public SplashScreen(Menu menu)
    	{
    		this.menu=menu;
    		try
    		{
    			img=Image.createImage("/map.PNG");
    		}
    		catch(Exception error)
    		{
    			Alert alert=new Alert("Err...","Cannot Open Image",null,AlertType.ERROR);
    			alert.setTimeout(Alert.FOREVER);
    			Menu.display.setCurrent(alert);
    		}
    	}
    	
    	public void paint(Graphics g) 
    	{
    		g.setColor(255,255,255);
    		g.fillRect(0, 0, getWidth(), getHeight());
    		g.drawImage(img, 0, 0, Graphics.TOP|Graphics.LEFT);
    	}
    	
    	protected void keyPressed(int key)
    	{
    		key=getGameAction(key);
    		
    		switch(key)
    		{
    		case Canvas.FIRE:
    			CanvasMenu canvas=new CanvasMenu(menu);
    			Menu.display.setCurrent(canvas);
    			break;
    		}
    	}
    	
    }
    Now i have written a keyPressed event here so that after pressing the fire key it will display the menu screen, but i want to display this SplashScreen for say just 10 seconds instead of keyPressed event so what changes should i make it in SplashScreen.

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Timer Class

    Simple enough.

    I've left in the facility to skip the screen with a key press. Note that gotoMenu() is synchronized, for the very limited change of the key being pressed at the same time as the sleep returns.

    Code:
    import javax.microedition.lcdui.*;
    
    public class SplashScreen extends Canvas implements Runnable {
        private final static int DELAY = 10 * 1000;
        
        private Image img;
        private Menu menu;
        private boolean done;
        
        public SplashScreen(Menu menu) {
            this.menu = menu;
            try {
                img = Image.createImage("/map.PNG");
                (new Thread(this)).start();
            } catch (Exception error) {
                Alert alert = new Alert("Err...", "Cannot Open Image", null, AlertType.ERROR);
                alert.setTimeout(Alert.FOREVER);
                Menu.display.setCurrent(alert);
            }
        }
        
        protected void paint(Graphics g) {
            g.setColor(255, 255, 255);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.drawImage(img, 0, 0, Graphics.TOP | Graphics.LEFT);
        }
        
        protected void keyPressed(int key) {
            if (getGameAction(key) == FIRE) {
                gotoMenu();
            }
        }
    
        public void run() {
            try {
                Thread.sleep(DELAY);
            } catch (InterrutedException ie) {
                // ignore
            }
            gotoMenu();
        }
    
        private synchronized void gotoMenu() {
            if (!done) {
                done = true;
                CanvasMenu canvas = new CanvasMenu(menu);
                Menu.display.setCurrent(canvas);
            }
        }
    }

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    45

    Re: Timer Class

    ya it was very simple and also it was very esy to understand. Thank u very much for ur help. Also can u tell me how can i flash the blinking message on splash screen as "Press any key"

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Timer Class

    It would have been easier if you said that in the first place...

    Code:
        private static final ON_TIME = 400;
        private static final OFF_TIME = 200;
    
        private boolean textOn = true;
    
        protected void paint(Graphics g) {
            g.setColor(255, 255, 255);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.drawImage(img, 0, 0, Graphics.TOP | Graphics.LEFT);
            if (textOn) {
                g.setColor(0);
                g.drawString("press fire", getWidth() / 2, getHeight(), Graphics.HCENTER | Graphics.BOTTOM);
            }
        }
    
        public void run() {
            long endTime = System.currentTimeMillis() + DELAY;
            while (System.currentTimeMillis() < endTime) {
                try {
                    Thread.sleep(textOn ? ON_TIME : OFF_TIME);
                } catch (InterrutedException ie) {
                    // ignore
                }
                textOn = !textOn;
                repaint();
                serviceRepaints();
            }
            gotoMenu();
        }
    Graham.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    45

    Re: Timer Class

    got it. But i didn't understand the syntax for thread i.e "Thread.sleep(textOn ? ON_TIME : OFF_TIME);" pls explain me and wat does the "servicerepaint" does here in this code as we are also using repaint in code.
    So this 2 things i m not able to get it pls explain me......

  8. #8
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Timer Class

    Quote Originally Posted by dineshdreamworld View Post
    textOn ? ON_TIME : OFF_TIME
    If you're not familiar with this syntax, you really need to get yourself an introductory guide to Java. I'll leave it for you to find out what this means.

    Quote Originally Posted by dineshdreamworld View Post
    pls explain me and wat does the "servicerepaint" does here in this code as we are also using repaint in code.
    You could find this out by looking at the Java Docs for the Canvas class. repaint() does not cause the screen to repaint immediately. It posts an event in the event queue, which will be processed later. serviceRepaints() causes any pending paint messages in the event queue to be processed immediately. When serviceRepaints() returns, the screen will have been updated, if an update was needed. It is used to make animation happen smoothly, to ensure that the screen updates when you want it to.

    Graham.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    45

    Re: Timer Class

    Ok thank u sir. I will definately go through the java book with syntax mentioned. Thanks for ur help. Pls help me like this in the future too.

Similar Threads

  1. java cimd application
    By vandana14 in forum General Messaging
    Replies: 3
    Last Post: 2011-04-07, 16:04
  2. Inter class problems
    By d-safety in forum Mobile Java General
    Replies: 0
    Last Post: 2009-12-16, 15:08
  3. Java Application running on WTK but not on S60 SDK
    By roman.benz in forum Mobile Java Tools & SDKs
    Replies: 6
    Last Post: 2009-06-09, 14:38
  4. Help needed with +CPMS, +CNMI for Class 0,1,2 SMs
    By joe--joe in forum General Messaging
    Replies: 3
    Last Post: 2009-03-09, 14:55
  5. Container Class needs help
    By vinayashrestha in forum Symbian User Interface
    Replies: 3
    Last Post: 2007-11-14, 12:54

Posting Permissions

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