2013年6月29日 星期六

[ Processing for Android ] A Simple Thread example

package processing.test.threadexample;

import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.*;

public class ThreadExample extends PApplet {



// A Simple Thread example


CountdownThread a_thread;
CountdownThread b_thread;
CountdownThread c_thread;

public void setup() {
  orientation(PORTRAIT);
 
  textSize(35);
  a_thread = new CountdownThread(1000,10);
  a_thread.start();
  
  b_thread = new CountdownThread(1000,100);
  b_thread.start();
  
  c_thread = new CountdownThread(1000,1000);
  c_thread.start();

}

public void draw() {
  
  background(255);
  fill(0);
  
  int a = a_thread.getCount();
  text("Count down : "+a,10,50);
  
  int b = b_thread.getCount();
  text("Count down : "+b,10,200);
  
  int c = c_thread.getCount();
  text("Count down : "+c,10,350);  
}

class CountdownThread extends Thread {
  
    int wait;   // How many milliseconds
    int count;  // counter
    
    CountdownThread (int w, int c) {
    wait = w;
    count = c;
  }
  
    public int getCount() {
    return count;
  }
  
    // Overriding "start()"
    public void start () {
    super.start();
  }
  
    public void run () {
    while (count > 0) {
      count--;
      try {
        sleep((long)(wait));
      } catch (Exception e) {
      }
    }
    quit();
  }
  
  // the method that quits the thread
  public void quit() {
    interrupt();
  }
}

  public int sketchWidth() { return 500; }
  public int sketchHeight() { return 500; }
}

沒有留言:

張貼留言