×

Discussion Board

Results 1 to 15 of 15
  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    45

    Question introducing delay

    Code:
    if(chanceover==false)
    {
       g.drawRegion(redmap, 0, 153, 60,70, 0, 24,141 , Graphics.TOP|Graphics.LEFT);//3rd Row 1st Col    		       //thread.sleep(DELAY);
    g.drawRegion(bluemap, 0, 230, 65, 90, 0, 62, 182, Graphics.TOP|Graphics.LEFT);//4th Row 1st Col
    }
    the above mention code is written in the paint method. Now i want to introduce the delay between the two drawing regions.
    Now following is the code which i have written for introducing the delay between the two thread.

    Code:
    public void run()
    {
      long endTime = System.currentTimeMillis() + DELAY;
      while (System.currentTimeMillis() < endTime) 
      {
    	try 
    	{
            	Thread.sleep(DELAY);
    	}
    	catch (Exception error) 
    	{
    		// ignore
    	}
      }
     gotoChance();
    }	
    	
    private void gotoChance()
    {
    	if(chance==false)
    	{
    		chanceover=false;
    	}	
    	repaint();
    }
    Now i m able to draw the upper region i.e
    "g.drawRegion(redmap, 0, 153, 60,70, 0, 24,141 , Graphics.TOP|Graphics.LEFT);"
    but actually both the drawing regions are drawn on the same time. I want to draw the regions by introducing the delay between each regions.
    So please tell me how can i draw the two regions after some time between each region.

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

    Re: introducing delay

    Quote Originally Posted by dineshdreamworld View Post
    Code:
    if(chanceover==false)
    {
       g.drawRegion(redmap, 0, 153, 60,70, 0, 24,141 , Graphics.TOP|Graphics.LEFT);//3rd Row 1st Col    		       //thread.sleep(DELAY);
    g.drawRegion(bluemap, 0, 230, 65, 90, 0, 62, 182, Graphics.TOP|Graphics.LEFT);//4th Row 1st Col
    }
    They're both drawn on the same condition, so they'll either both draw, or neither. Try:

    Code:
    if (chanceover == false) {
        g.drawRegion(redmap, 0, 153, 60, 70, 0, 24, 141, Graphics.TOP | Graphics.LEFT); //3rd Row 1st Col
    } else {
        g.drawRegion(bluemap, 0, 230, 65, 90, 0, 62, 182, Graphics.TOP | Graphics.LEFT); //4th Row 1st Col
    }
    Graham.

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

    Re: introducing delay

    Actually either of the region will be drawn as the condition u mentioned. But i want to draw both the region by introducing the delay between the two regions.

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

    Re: introducing delay

    So, do you want this:

    Code:
    g.drawRegion(bluemap, 0, 230, 65, 90, 0, 62, 182, Graphics.TOP | Graphics.LEFT); //4th Row 1st Col
    if (chanceover == false) {
        g.drawRegion(redmap, 0, 153, 60, 70, 0, 24, 141, Graphics.TOP | Graphics.LEFT); //3rd Row 1st Col
    }
    ?

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

    Re: introducing delay

    hello graham actually here is the code i have written for the actual condition to take place.
    Code:
    import javax.microedition.lcdui.*;
    
    
    public class BattlingLordGame extends Canvas implements Runnable
    {
    	private MidletMenu menu;
    	private Image map,mapcontd,greenmap,bluemap,redmap,flashmap;
    	public int first,kingchance=1;
    	public boolean msgprint=false,conquer=false;
    	public boolean chance=false,repeatchance=false,chanceover=true;
    	private Thread thread;
        private final static int DELAY = 10*1000;
    
    		
    	public BattlingLordGame(MidletMenu menu)
    	{
    		this.menu=menu;
    		first=0;
    		//Constantdeclaration.done=false;
    		try
    		{
    			map=Image.createImage("/mappart2.png");
    			mapcontd=Image.createImage("/mappartcontd.png");
    			greenmap=Image.createImage("/greenmap.png");
    			redmap=Image.createImage("/redmap.png");
    			bluemap=Image.createImage("/bluemap.png");
    			flashmap=Image.createImage("/flashmap.png");
    
    			thread = new Thread(this);
    			thread.start();
    		}
    		catch(Exception error)
    		{
    			Alert alert=new Alert("Err...","Cannot Open Image",null,AlertType.ERROR);
    			alert.setTimeout(Alert.FOREVER);
    			MidletMenu.display.setCurrent(alert);
    		}
    	}
    	
    	protected void paint(Graphics g)
    	{
    		g.setColor(255,255,255);
    		g.fillRect(0, 0, getWidth(), getHeight());
    		
    		/*Method1 for Midlet2 and Later*/
    	
    		
    		g.drawRegion(map, 0, 0, 42, 60, 0, 5, 40, Graphics.TOP|Graphics.LEFT); //1st Row 1st Col
    		g.drawRegion(greenmap, 55, 0, 120, 52, 0, 32, 06, Graphics.TOP|Graphics.LEFT);// 1st Row 2nd Col
    		g.drawRegion(map, 180, 0, 60, 60, 0, 130, 06, Graphics.TOP|Graphics.LEFT);//1st Row 3rd Col
    		
    		g.drawRegion(redmap, 0, 65, 72,85, 0, 4,79, Graphics.TOP|Graphics.LEFT);//2nd Row 1st Col
    		g.drawRegion(map, 70, 65, 100, 60, 0, 40, 54, Graphics.TOP|Graphics.LEFT);//2nd Row 2nd Col
    		g.drawRegion(map, 170, 55, 70, 80, 0, 121, 35, Graphics.TOP|Graphics.LEFT);//2nd Row 3rd Col
    		
    		g.drawRegion(map, 0, 153, 60,70, 0, 24,141 , Graphics.TOP|Graphics.LEFT);//3rd Row 1st Col
    		g.drawRegion(map, 55, 153, 120,70, 0, 40,99 , Graphics.TOP|Graphics.LEFT);//3rd Row 2nd Col
    		g.drawRegion(bluemap, 185, 153, 60,70, 0, 129,74 , Graphics.TOP|Graphics.LEFT);//3rd Row 3rd Col
    		
    		g.drawRegion(map, 0, 230, 65, 90, 0, 62, 182, Graphics.TOP|Graphics.LEFT);//4th Row 1st Col
    		g.drawRegion(map, 55, 240, 100, 60, 0, 64, 154, Graphics.TOP|Graphics.LEFT);//4th Row 2nd Col
    		g.drawRegion(map, 170, 230, 100, 60, 0, 157, 105, Graphics.TOP|Graphics.LEFT);//4th Row 3rd Col
    		
    		g.drawRegion(mapcontd, 0, 0, 120, 50, 0, 105, 179, Graphics.TOP|Graphics.LEFT);//1st Row 1st Col
    		g.drawRegion(mapcontd, 120, 0, 80, 40, 0, 170, 158, Graphics.TOP|Graphics.LEFT);//1st Row 2nd Col
    		
    		g.drawRegion(mapcontd, 0, 60, 90, 90, 0, 92, 218, Graphics.TOP|Graphics.LEFT);//2nd Row 1st Col
    		g.drawRegion(mapcontd, 110, 60, 120, 90, 0, 156, 194, Graphics.TOP|Graphics.LEFT);//2nd Row 2nd Col
    		
    		g.drawRegion(mapcontd,20,140,70,100,0,40,209,Graphics.TOP|Graphics.LEFT);//3rd Row 1st Col
    		g.drawRegion(mapcontd,108,140,120,100,0,132,230,Graphics.TOP|Graphics.LEFT);//3rd Row 2nd Col
    		
    		
    		g.setColor(0);
    		g.drawString("Pause",0,getHeight(),Graphics.LEFT|Graphics.BOTTOM);
    		
    		//syntax
    		//g.drawRegion(image,ix(topleftcorner of image),iy(topleft corner of image),
    		//width,height,Sprite.TRANS_NONE,sx(screen position)
    		//,sy(screen poistion),Graphics.TOP|Graphics.LEFT);
    		
    		
    		if(msgprint==false)
    		{
    			g.setColor(0,0,0);
    			g.setFont(Constantdeclaration.largefont);
    			g.drawString("Your Turn", getWidth()/2, getHeight()/2, Graphics.TOP|Graphics.HCENTER);
    		}
    		
    		g.drawString("Pause",0,getHeight(),Graphics.LEFT|Graphics.BOTTOM);
    		
    		if(first==1)
    		{
    			if(conquer==true && chance==true)
    				g.drawRegion(greenmap, 0, 0, 42, 60, 0, 5, 40, Graphics.TOP|Graphics.LEFT); //1st Row 1st Col
    			else
    				g.drawRegion(flashmap, 0, 0, 42, 60, 0, 5, 40, Graphics.TOP|Graphics.LEFT); //1st Row 1st Col
    			chance=false;
    		}	
    		
    		if(first==2)
    		{
    			if(conquer==true && chance==true)
    				g.drawRegion(greenmap, 70, 65, 100, 60, 0, 40, 54, Graphics.TOP|Graphics.LEFT);//2nd Row 2nd Col
    			else 
    				g.drawRegion(flashmap, 70, 65, 100, 60, 0, 40, 54, Graphics.TOP|Graphics.LEFT);//2nd Row 2nd Col
    			chance=false;
    		}
    		
    		if(first==3)
    		{
    			if(conquer==true && chance==true)
    				g.drawRegion(greenmap, 180, 0, 60, 60, 0, 130, 06, Graphics.TOP|Graphics.LEFT);//1st Row 3rd Col
    			else 
    				g.drawRegion(flashmap, 180, 0, 60, 60, 0, 130, 06, Graphics.TOP|Graphics.LEFT);//1st Row 3rd Col
    			chance=false;
    		}
    		
    		if(chanceover==false)
    		{
    			g.drawRegion(redmap, 0, 153, 60,70, 0, 24,141 , Graphics.TOP|Graphics.LEFT);//3rd Row 1st Col
    			//thread.sleep(DELAY);
    			g.drawRegion(bluemap, 0, 230, 65, 90, 0, 62, 182, Graphics.TOP|Graphics.LEFT);//4th Row 1st Col
    		}
    		
    		
    		
    		/* Method2 for Midlet1*/
    		//g.setClip(30, 40, 40, 60);
    		//g.drawImage(map, 30-0, 40-0, Graphics.TOP|Graphics.LEFT);
    		//syntax
    		//sy,sx=screen position where images is pasted
    		//ix,iy=image clipping i.e topleft corner
    		//g.setClip(sx, sy, width, height);
    		//g.drawImage(img, sx - ix, sy - iy, Graphics.TOP | Grapics.LEFT);
    	}
    	
    	protected void keyPressed(int key)
    	{
    		msgprint=true;
    		switch(key)
    		{		
    			case KEY_NUM4:
    				if(repeatchance==false)
    				first=1;
    				break;
    				
    			case KEY_NUM8:
    				if(repeatchance==false)
    				first=2;
    				break;
    				
    			case KEY_NUM6:
    				if(repeatchance==false)
    				first=3;
    				break;
    		
    			case KEY_NUM5:
    				conquer=true;
    				chance=true;
    				repeatchance=true;
    				break;
    		
    				
    			case -21:
    				Constantdeclaration.PAUSE=true;
    				MainMenu displaymain=new MainMenu(menu);
    				MidletMenu.display.setCurrent(displaymain);
    		}
    		repaint();
    	}
    	
    	public void run()
    	{
    		long endTime = System.currentTimeMillis() + DELAY;
    		while (System.currentTimeMillis() < endTime) 
    		{
    			try 
    			{
    				Thread.sleep(DELAY);
    			}
    			catch (Exception error) 
    			{
    				// ignore
    			}
    		}
    		gotoChance();
    	}	
    	
    	private void gotoChance()
    	{
    		if(chance==false)
    		{
    			chanceover=false;
    		}	
    		repaint();
    	}
    }
    Now after running this code i m able to draw the greenmap properly and after some time i wanna draw the remap and after the redmap is drawn after some time the bluemap should be drawn. Now as i have written the same condition for both the region i want to introduce some delay between the redmap and bluemap region so that after redmap is drawn the bluemap should be drawn after some time.

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

    Re: introducing delay

    This is the kind of model I would use:

    Code:
    private static final int STATE_BEFORE_PAUSE = 0;
    private static final int STATE_AFTER_PAUSE = 1;
    
    private int state;
    
    public MyCanvas {
        state = STATE_BEFORE_PAUSE;
        (new Thread(this)).start();
    }
    
    public void run() {
        try {
            Thread.sleep(DELAY);
        } catch (InterruptedException ie) {
            // ignore
        }
        state = STATE_AFTER_PAUSE;
        repaint();
        serviceRepaints();
    }
    
    public void paint(Graphics g) {
        // (paint common background for all states here)
    
        switch (state) {
        case STATE_BEFORE_PAUSE:
            // (paint everything you want only in this state here)
            break;
        case STATE_AFTER_PAUSE:
            // (paint everything you want only in this state here)
            break;
        }
    
        // (paint common foreground for all states here)
    }
    You must paint the entire screen every time paint() is called.

    Does that help?

    Graham.

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

    Re: introducing delay

    Hello Graham as u mentioned the method i tried it but its not working as what i want.

    Following are some points which i want to tell u abt my code.
    1. What i want is that after running the program when the emulator starts when the user presses the KEY_NUM5 then the particular greenmap should be drawn which i m able to achieve in keypressed and paint method.

    2. Now after the greenmap is drawn the redmap should be drawn after the greenmap by introducing some delay bten the two drawing region methods i.e greenmap and redmap. I m able to achieve this thing too which is tken care in gotChance and paint method where in the if loop of chanceover it is taken care of.

    3. Now i m stuck at this point where after the redmap is drawn i want to draw the bluemap by introducing some further delay betn two drawing regions i.e redmap and bluemap. I dont understand where to introduce delay betn this two drawing regions i.e redmap and bluemap. So pls tell me what and where exactly i have to make change in the code.

    Actually i tried the changes u mentioned but the redmap is drawn even before the keypressed event take place.
    So pls tell me what should i do.

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

    Re: introducing delay

    Well, my intent was to illustrate a general technique, with the expectation that you would adapt that technique to your specific needs. It was not my intention to write your game for you. But let's give you another example. This is a bit more complete, and typical of how a game is constructed. It can easily be used to produce something more animated as well as meet your immediate requirement.

    PHP Code:
    class MyCanvas extends Canvas implements Runnable {
        private static final 
    int STATE_WAIT_FOR_KEY 0;
        private static final 
    int STATE_SHOW_GREEN_MAP 1;
        private static final 
    int STATE_SHOW_RED_MAP 2;
        private static final 
    int STATE_SHOW_BLUE_MAP 3;
        private static final 
    int STATE_END 9999;

        private static final 
    int FRAME_TIME 50;  // milliseconds per animation frame
        
    private static final int MINIMUM_SLEEP 10;  // always sleep for at least this time
        
    private static final int MAP_DISPLAY_TIME 4000;  // milliseconds to show each map

        
    private int state;

        private 
    boolean key5pressed;

        public 
    MyCanvas() {
            
    state STATE_WAIT_FOR_KEY;
            (new 
    Thread(this)).start();
        }

        protected 
    void paint(Graphics g) {
            
    int width getWidth();
            
    int height getHeight();
            
            
    // paint background for all states
            
    g.setColor(0xffffff);
            
    g.fillRect(00widthheight);

            switch (
    state) {
            case 
    STATE_WAIT_FOR_KEY:
                
    g.setColor(0x000000);
                
    g.drawString("Press 5"width 2height 2Graphics.HCENTER Graphics.BASELINE);
                break;
            case 
    STATE_SHOW_GREEN_MAP:
                
    // draw green map
                
    break;
            case 
    STATE_SHOW_RED_MAP:
                
    // draw red map
                
    break;
            case 
    STATE_SHOW_BLUE_MAP:
                
    // draw blue map
                
    break;
            default:
                throw new 
    IllegalStateException("unknown state in paint(): " state);
            }
        }

        protected 
    void keyPressed(int key) {
            switch (
    key) {
            case 
    KEY_NUM5:
                
    key5pressed true;
                break;
            }
        }

        public 
    void run() {
            
    int timeInThisState 0;

            while (
    state != STATE_END) {
                
    long startTime System.currentTimeMillis();

                
    // state transitions
                
    int newState state;
                switch (
    state) {
                case 
    STATE_WAIT_FOR_KEY:
                    if (
    key5pressed) {
                        
    key5pressed false;
                        
    newState STATE_SHOW_GREEN_MAP;
                    }
                    break;
                case 
    STATE_SHOW_GREEN_MAP:
                    if (
    timeInThisState >= MAP_DISPLAY_TIME) {
                        
    newState STATE_SHOW_RED_MAP;
                    }
                    break;
                case 
    STATE_SHOW_RED_MAP:
                    if (
    timeInThisState >= MAP_DISPLAY_TIME) {
                        
    newState STATE_SHOW_BLUE_MAP;
                    }
                    break;
                case 
    STATE_SHOW_BLUE_MAP:
                    
    // (some state handling here)
                    
    break;
                default:
                    throw new 
    IllegalStateException("unknown state in run(): " state);
                }

                if (
    newState != state) {
                    
    // state is changing
                    
    timeInThisState 0;
                    
    state newState;
                }

                
    // update the screen
                
    repaint();
                
    serviceRepaints();

                
    // keep animation consistent
                
    int updateTime = (int) (System.currentTimeMillis() - startTime);
                
    int sleepTime FRAME_TIME updateTime;
                if (
    sleepTime MINIMUM_SLEEP) {
                    
    // minimum sleep allows time for other threads to run, ensures key events are delivered
                    
    sleepTime MINIMUM_SLEEP;
                }
                try {
                    
    Thread.sleep(sleepTime);
                } catch (
    InterruptedException ie) {
                    
    // ignore
                
    }
                
    // record how long we've been in this state
                
    timeInThisState += (updateTime sleepTime);
            }
        }

    Graham.

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

    Re: introducing delay

    Hello Graham
    I used all the techniques u mentioned but wen i shown this thing to my college SIR he told us to not to go for the this threading technique. Since here in this project he told us that we have to go for multithreading technique. So he told to go for a timer technique where we can update the screen on the systems time basis. Can u tell me any such thing where we can update the screen when the particular time arrives.

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

    Re: introducing delay

    Hey! You never said I was writing your college assignment for you!

    This is a multithreaded technique, and it does update the screen on the basis of the system timer.

    You can look at the Java Docs for the classes Timer and TimerTask, but I would consider this a very clumsy way to do what you want. Most professional mobile Java developers (especially game developers) avoid using the Timer class, because it's problematic (for example, it can consume huge amounts of CPU time on some devices).

    I guess you could keep the same paint() method, remove the run() method, change the keyPressed() and add a TimerTask inner class as shown below.

    Code:
    private static int[] mapSequence = new int[] {
            STATE_SHOW_GREEN_MAP,
            STATE_SHOW_RED_MAP, 
            STATE_SHOW_BLUE_MAP
        };
    
    private int state;
    
    private static class ScreenSequencer extends TimerTask {
        public static void runSequence(int[] sequence, int delay) {
            new ScreenSequencer(sequence, delay);
        }
    
        private Timer timer;
        private int[] seq;
        private int index;
    
        private ScreenSequencer(int[] sequence, int delay) {
            seq = sequence;
            timer = new Timer();
            timer.schedule(this, 0, delay);
        }
    
        public void run() {
            state = seq[index];
            repaint();
            serviceRepaints();
    
            index++;
            if (index == seq.length) {
                timer.cancel;
            }
        }
    }
    
    protected void keyPressed(int key) {
        if (state == STATE_WAIT_FOR_KEY && key == KEY_NUM5) {
            ScreenSequencer.runSequence(mapSequence, 5000);
        }
    }
    Graham.

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

    Re: introducing delay

    Hello Graham
    Its not a college assignment. I m just developing a game since i have an interest in j2me and i m jst taking an help from my professor. Also thank u for ur help. Wil get back to u if i have some problem.

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

    Re: introducing delay

    Timers and TimerTasks are not the best way to do this. I recommend you avoid them.

    Graham.

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

    Re: introducing delay

    Thank u graham i implemented my game part successfully. As u told me to do in the switch case i did it by making some changes in it and i was able to do it successfully thank u graham. As u told me to avoid the timertask and tried to implement in the way as told me before.

    Once again thank u graham

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

    Question Re: introducing delay

    Code:
    if(first==1 && conquer==true)
            g.drawRegion(greenmap, 0, 0, 42, 60, 0, 5, 40, Graphics.TOP|Graphics.LEFT); //1st Row 1st Col
            		
    if(first==2 && conquer==true)
    	g.drawRegion(greenmap, 70, 65, 100, 60, 0, 40, 54, Graphics.TOP|Graphics.LEFT);//2nd Row 2nd Col
            
    if(first==3 && conquer==true)
    	g.drawRegion(greenmap, 180, 0, 60, 60, 0, 130, 06, Graphics.TOP|Graphics.LEFT);//1st Row 3rd Col
    Now in the above mention condition whichever is true i want to store it somewhere so that by referring it i can use it whenever i need so how can i do it. I want to store the drawing region so how can i do that.

  15. #15
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: introducing delay

    Quote Originally Posted by dineshdreamworld View Post
    Code:
    if(first==1 && conquer==true)
            g.drawRegion(greenmap, 0, 0, 42, 60, 0, 5, 40, Graphics.TOP|Graphics.LEFT); //1st Row 1st Col
            		
    if(first==2 && conquer==true)
    	g.drawRegion(greenmap, 70, 65, 100, 60, 0, 40, 54, Graphics.TOP|Graphics.LEFT);//2nd Row 2nd Col
            
    if(first==3 && conquer==true)
    	g.drawRegion(greenmap, 180, 0, 60, 60, 0, 130, 06, Graphics.TOP|Graphics.LEFT);//1st Row 3rd Col
    Now in the above mention condition whichever is true i want to store it somewhere so that by referring it i can use it whenever i need so how can i do it. I want to store the drawing region so how can i do that.
    Request you to keep the post unique and do not repeat same point.
    http://discussion.forum.nokia.com/fo...d.php?t=195793
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

Similar Threads

  1. E65 constant delay
    By vins4nokia in forum Bluetooth Technology
    Replies: 1
    Last Post: 2008-01-15, 12:02
  2. Replies: 5
    Last Post: 2008-01-09, 10:17
  3. Video with audio recording delay problem on N90
    By vasya0 in forum Symbian Media (Closed)
    Replies: 2
    Last Post: 2007-12-03, 16:44
  4. introducing delay (urgent)
    By harish13_ks in forum Symbian
    Replies: 4
    Last Post: 2006-10-16, 06:34
  5. Replies: 5
    Last Post: 2004-03-04, 04:14

Posting Permissions

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