Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Paint in Thread or Runnable

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Paint in Thread or Runnable

    import java.awt.Graphics;
     
    import javax.swing.JFrame;
     
    class node{
    	node(int a,node next){
    		this.a=a;this.next=next;
    	}
    	int a;
    	node next;
    }
    public class df extends JFrame implements Runnable{
    	Thread t;
    	node p,q;
    	node First;
    	public df(){
    		t=new Thread(this);
    		p=new node(1,null);
    		First=p;
    		q=p;
    		p=new node(2,null);
    		q.next=p;
    		q=p;
    		p=new node(3,null);
    		q.next=p;
    		q=p;
    		p=new node(4,null);
    		q.next=p;
    		q=p;
    		p=new node(5,null);
    		q.next=p;
    		q=p;
    		t.start();
    	}
    	public void run() {
    		p = First;
    		while (p != null) {
    			try {
    				repaint();
    				t.sleep(1000);
    			} catch (Exception e) {
    			}
    			p=p.next;
    		}
    	}
     
    	public void paint(Graphics g){
    		System.out.println(p.a);
     
    	}
     
    	public static void main(String[]as)	{
    		new df();
    	}
     
     
     
    }



    The code was ran not right, I want it's result is: 1 2 3 4 5 and delay(1000) every time that it prints a number
    exemple: 1 (delay(1000)) 2 (delay(1000)) ....
    and the paint must do that
    please help me!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Paint in Thread or Runnable

    Use a Swing Timer. Oracle has a fine tutorial on using a Swing Timer.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    KevinWorkman (June 26th, 2014)

Similar Threads

  1. How to use data from obtainMessage in Runnable?
    By iceman999 in forum Android Development
    Replies: 0
    Last Post: February 8th, 2013, 05:23 AM
  2. Thread vs Runnable interface
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: August 31st, 2012, 01:38 AM
  3. Replies: 1
    Last Post: August 4th, 2012, 10:02 AM
  4. creating runnable JAR file
    By olimpicco in forum Java IDEs
    Replies: 25
    Last Post: January 11th, 2012, 09:15 AM
  5. Runnable to WebService
    By fran_jo in forum Threads
    Replies: 1
    Last Post: April 12th, 2011, 04:12 PM

Tags for this Thread