Chitika

Threads

Example Program :
The following program will create two Thread objects and display two messages interchangeably

public class ThreadDemo{
public static void main(String[] arg){
           Message m1 = new Message("SriLanka win");
           Thread t1 = new Thread(m1);
           Message m2 = new Message("Australia win");
           Thread t2 = new Thread(m2);
           t1.start();
           t2.start();
           }
}

class Message implements Runnable{
          public String msg;
          public Message(String s){
                  msg=s;
          }
public void run(){
       for(int i=0;i<100;i++)
                 System.out.println(msg);
       }
 }


 The following program draw Archs in an applet and uses a thread to refresh the browser

import java.applet.*;
import java.awt.*;
import java.util.*;
public class ArcDemo extends Applet implements Runnable {
 int k=0, x=0;
 Thread t;
 Color col[] = new Color[50];
public void init() {
 float h = 0.0f;
 for(int i=0; i<col.length;i++) {
  col[i] = Color.getHSBColor(h,1.0f,1.0f);
  h+=0.02;
 }
}
public void start() {
 if (t==null) {
 t = new Thread (this);
 t.start();
        }
}
public void run() {
 while (true) {
  repaint();
  setForeground(col[k]);
  k++;
  if(k==col.length) k=0;
  x+=1; if(x==360) x=0;
  try {
   Thread.sleep(10);
  } catch (InterruptedException e) {}
 }
}
public void stop() {
 if (t!=null) {
  t = null;
 }
}
public void paint (Graphics g) {
 g.drawArc(100,100,400,400,0,x);
 g.drawArc(125,125,350,350,0,x);
 g.drawArc(150,150,300,300,0,x);
 g.drawArc(175,175,250,250,0,x);
 g.drawArc(200,200,200,200,0,x);
 g.drawArc(225,225,150,150,0,x);
 g.drawArc(250,250,100,100,0,x);
 g.drawArc(275,275,50,50,0,x);
 g.drawArc(298,298,5,5,0,x);
}
}

HTML code to display the above applet

<html>
<body>
<applet code="ArcDemo.class" width=600 height=600>
</applet>
</body>
</html>

Example 02:

import java.applet.*;
import java.awt.*;
import java.util.*;
public class ArcDemo2 extends Applet implements Runnable {
 int k=0, x=0;
 Thread t;
 Color col[] = new Color[50];
public void init() {
 float h = 0.0f;
 for(int i=0; i<col.length;i++) {
  col[i] = Color.getHSBColor(h,1.0f,1.0f);
  h+=0.02;
 }
}
public void start() {
 if (t==null) {
 t = new Thread (this);
 t.start();
        }
}
public void run() {
 while (true) {
  repaint();
  setForeground(col[k]);
  k++;
  if(k==col.length) k=0;
  x+=1; if(x==360) x=0;
  try {
   Thread.sleep(10);
  } catch (InterruptedException e) {}
 }
}
public void stop() {
 if (t!=null) {
  t = null;
 }
}
public void paint (Graphics g) {
 g.fillArc(100,100,400,400,0,x);
}
}

Analog Clock:
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.awt.Graphics.*;
public class ClockDemo extends Applet implements Runnable {
 int k=0, x=90, y=90, z=90;
 Thread t;
 Color col[] = new Color[50];
public void init() {
 float h = 0.0f;
 for(int i=0; i<col.length;i++) {
  col[i] = Color.getHSBColor(h,1.0f,1.0f);
  h+=0.02;
 }
}
public void start() {
 if (t==null) {
 t = new Thread (this);
 t.start();
        }
}
public void run() {
 while (true) {
  repaint();
  x-=5; if(x==0) x=360;
  if(x==90) {
   y-=5; if(y==0) y=360;
          if(y==90) {
     z-=5; if(z==0) z=360;
   }
  }     
  try {
   Thread.sleep(1000);
  } catch (InterruptedException e) {}
 }
}
public void stop() {
 if (t!=null) {
  t = null;
 }
}
public void paint (Graphics g) {
 g.drawArc(100,100,400,400,0,390);
 Font f = new Font("Times New Roman",Font.BOLD,30);
 g.setFont(f);

 g.setColor(Color.RED);
 g.fillArc(105,105,390,390,x,2);

 g.setColor(Color.BLACK);
 g.fillArc(105,105,390,390,y,2);
 g.fillArc(125,125,350,350,z,2);
 g.fillArc(295,295,10,10,0,360);
 g.drawString("12",287,130);
 g.drawString("6",293,485);
 g.drawString("9",120,310);
 g.drawString("3",475,310);
}
}

HTML Code to display the above Applet

<html>
<body>
<applet code="ClockDemo.class" width=600 height=600>
</applet>
</body>
</html>
A Ball bumping up and down

Images used in the program
Ball2
Ball3

Ball1

Ball4









import java.applet.*;
import java.awt.*;
import java.util.*;
import java.awt.Graphics.*;
public class Ball extends Applet implements Runnable {
 int k=0, y=0, x=0;
 Thread t;
 Image img[] = new Image[4];
public void init() {
 for(int i=0; i<4; i++)
  img[i] = this.getImage(this.getDocumentBase(),"Ball"+(i+1)+".GIF");

}
public void start() {
 if (t==null) {
 t = new Thread (this);
 t.start();
 }
}
public void run() {
 while (true) {
  repaint();
  if((x<250 && x>230) || (x<280 && x>260)) {
   k++; if(k==img.length) k=0;
  }
  //k++; if(k==img.length) k=0;
  y+=2; x+=10;
  if(x==500) {
   while(x!=0) {
    repaint();
    if((x<250 && x>230) || (x<280 && x>260)) {
     k++; if(k==img.length) k=0;
    }
     y+=2; x-=10;
    try {
     Thread.sleep(40);
    } catch (InterruptedException e) {}
   }
  }
  if(y==800) {
   while(y!=0) {
    repaint();
    if((x<250 && x>230) || (x<280 && x>260)) {
     k++; if(k==img.length) k=0;
    }
     y-=2; x+=10;
    if(x==500) {
     while(x!=0) {
      repaint();
      if((x<250 && x>230) || (x<280 && x>260)) {
       k++; if(k==img.length) k=0;
      }
       y-=2; x-=10;
      try {
       Thread.sleep(40);
      } catch (InterruptedException e) {}
     }
    }
    try {
     Thread.sleep(40);
    } catch (InterruptedException e) {}
   }
  }
  try {
   Thread.sleep(40);
  } catch (InterruptedException e) {}
 }
}
public void stop() {
 if (t!=null) {
  t = null;
 }
}
public void paint (Graphics g) {
 g.drawImage(img[k],y,x,this);

}
}