Results 1 to 5 of 5
  1. #1
    raptor is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Unhappy while running multiple thread repaint isnt being called....

    Java Code:
    import java.applet.*;
    import java.awt.*; 
    
    public class TrafficControl extends Applet implements Runnable{
    	Thread t;
    	Color c1,c2,c3;
    	public void init() 
    	{ 	  
    		for(int a=1;;a++)
    		{
     			if(a%3==1)starter("RED");
    			else if(a%3==2)starter("YELLOW");
    			else starter("GREEN");
    			
    			try{
    				t.join();
    				Thread.sleep(1000);
    			   }   catch(Exception e){System.out.println(e);}
    		}
     			 
    	} 
    	public void starter(String s)
    	{
    		t=new Thread(this,s);
    	    t.start(); 
    	}
    	 public void run()
    	{
    		System.out.println(t); 
    		String pro=t.getName();
    		c1=c2=c3=Color.WHITE;
     		if(pro.equals("RED"))
            	c1=Color.RED;
            else if(pro.equals("YELLOW"))
                c2=Color.YELLOW;
            else
                c3=Color.GREEN;  		
     		repaint();      //this repaint is nvr called
    	}
            
    	 public void paint(Graphics g)
         { 
          System.out.println("In paint"); //thread nvr enters paint???? y ??
    	                                   //it'll only enter ven infinite loop will finish   
        	 g.drawRect(0, 0, 30, 80);
        	 g.setColor(c1);
        	 g.fillOval(5, 5, 20, 20);
        	 g.setColor(c2);
        	 g.fillOval(5, 30, 20, 20);
        	 g.setColor(c3);
        	 g.fillOval(5, 55, 20, 20);    	 
         }
    }
    Last edited by Fubarable; 08-02-2010 at 01:37 PM. Reason: moderator edit: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You appear to be calling Thread.sleep on the event thread, and thereby put your applet to sleep.

    Also, please look at my link about using code tags. i will try to add them to your post above.

  3. #3
    raptor is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default

    hw can i mend it..using above code...?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Put the loop in init() into its own thread.

  5. #5
    shuks is offline Member
    Join Date
    Oct 2009
    Posts
    18
    Rep Power
    0

    Default

    I dont think you be calling the sleep() method from the init() method. You should be calling the sleep() method in the run() method...

    public void run()
    {
    System.out.println(t);
    String pro=t.getName();
    c1=c2=c3=Color.WHITE;
    if(pro.equals("RED"))
    c1=Color.RED;
    else if(pro.equals("YELLOW"))
    c2=Color.YELLOW;
    else
    c3=Color.GREEN;
    repaint(); //this repaint is nvr called

    try{
    Thread.sleep(1000);
    } catch(Exception e){System.out.println(e);}


    }

Similar Threads

  1. Running multiple threads on multiple CPU cores?
    By Dosta in forum Threads and Synchronization
    Replies: 2
    Last Post: 09-19-2010, 03:48 PM
  2. Question about running Multiple Threads
    By ferdzz in forum New To Java
    Replies: 2
    Last Post: 06-29-2010, 01:19 PM
  3. Running thread a second time
    By Fleur in forum New To Java
    Replies: 18
    Last Post: 11-17-2009, 12:57 PM
  4. [SOLVED] Method from one thread called on another thread
    By Ypsilon IV in forum Threads and Synchronization
    Replies: 7
    Last Post: 04-24-2009, 02:07 PM
  5. running multiple server
    By amitnayak1 in forum Advanced Java
    Replies: 3
    Last Post: 06-05-2008, 04:14 AM

Posting Permissions

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