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; }
}

2013年6月22日 星期六

[ Processing for Android ] fisica 2D 物理系統

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

import fisica.*; 

import java.util.*; 
import java.io.*;  

public class Pipe extends PApplet {

FWorld world;

public void setup() {
 
  smooth();

  Fisica.init(this);

  world = new FWorld();
  world.setGravity(0, -300);


  FPoly l = new FPoly();
  l.vertex(width/4, 0);
  l.vertex(0, 0);
  l.vertex(0, height);

  l.vertex(width/4, height);
  l.vertex(width/4, 7*height/10);
  l.vertex(width/2, 7*height/10);
  l.vertex(width/2, 3*height/10);
  l.vertex(width/4, 3*height/10);
  l.setStatic(true);
  l.setFill(0);
  l.setFriction(0);
  world.add(l);

  FPoly r = new FPoly();
  r.vertex(width/4+60, 0);
  r.vertex(width, 0);
  r.vertex(width, height);

  r.vertex(width/4+60, height);
  r.vertex(width/4+60, 7*height/10+60);
  r.vertex(width/2+60, 7*height/10+60);
  r.vertex(width/2+60, 3*height/10-60);
  r.vertex(width/4+60, 3*height/10-60);
  r.setStatic(true);
  r.setFill(0);
  r.setFriction(0);
  world.add(r);
}

public void draw() {
  background(80, 120, 200);

  if ((frameCount % 40) == 1) {
    FBlob b = new FBlob();
    float s = random(30, 40);
    b.setAsCircle(width/4+20, height-random(100), s, 20);
    
    b.setStroke(0);
    b.setStrokeWeight(2);
    b.setFill(255);
    b.setFriction(0);
    world.add(b);
  }

  world.step();
  world.draw();
}

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

2013年6月9日 星期日

第一支Processing for Android 程式

package processing.test.radiation;

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

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 

public class Radiation extends PApplet {

String lines[]; 
String [][] radiation_csv;
int csvWidth=0;

public void setup() {
lines = loadStrings("http://www.aec.gov.tw/open/gammamonitor.csv");
background(255);
textSize(16);

for (int i=0; i < lines.length; i++) {
  String [] chars=split(lines[i],',');
  if (chars.length>csvWidth){
    csvWidth=chars.length;
  }
}


radiation_csv = new String [lines.length][csvWidth];


for (int i=0; i < lines.length; i++) {
  String [] temp = new String [lines.length];
  temp= split(lines[i], ',');
  for (int j=0; j < temp.length; j++){
      radiation_csv[i][j]=temp[j];
  }
}

}

public void draw() {
for (int i=1; i < lines.length; i++) {
   fill(0, 102, 153, 204);
   text(radiation_csv[i][1]+" "+radiation_csv[i][2]+" (mSv/hr)",10,20+i*15);
   stroke(255, 0, 0);
   line(200, 20+i*15, 200+PApplet.parseFloat(radiation_csv[i][2])*2000, 20+i*15);

  }

}

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