Loading...
Logo
Processing Forum
Hi Everyone help me with this. 
It should splash color on the PGraphics but it not happening. It is continuously throwing Null point exception error... it uses NITE click gesture to throw color ...please help


Copy code
  1. import SimpleOpenNI.*;
    SimpleOpenNI context;

    PGraphics topLayer; 
    boolean      handsTrackFlag = false;
    PVector      handVec = new PVector();
    PVector      handVec2D  = new PVector();//just for drawing
    String       lastGesture = "";
    float        lastZ = 0;
    boolean      isPushing,wasPushing;
    float        yourClickThreshold = 20;//set this up as you see fit for your interaction

    void setup(){
      size(640,480);  
      context = new SimpleOpenNI(this);
      context.enableDepth();
      // enable hands + gesture generation
      context.enableGesture();
      context.enableHands();
      // add focus gestures  / here i do have some problems on the mac, i only recognize raiseHand ? Maybe cpu performance ?
      context.addGesture("Wave");
      context.addGesture("Click");
      context.addGesture("RaiseHand");
      topLayer = createGraphics(640, 480, g.getClass().getName());
    }

    void draw()
    {
      context.update();
      image(context.depthImage(),0,0);
      // draw the tracked hand
      if(handsTrackFlag){
        context.convertRealWorldToProjective(handVec,handVec2D);
        float diff = (handVec.z-lastZ);
        isPushing = diff < 0;
        if(diff > yourClickThreshold){
          if(!wasPushing && isPushing) fill(255,0,0);
          if(wasPushing && !isPushing) fill(0,255,0);
         splash();

        }else fill(255);
        lastZ = handVec.z;
        wasPushing = isPushing;
       ellipse(handVec2D.x,handVec2D.y,10,10);
      }
    image(topLayer,0,0);
    }


    // -----------------------------------------------------------------
    // hand events

    void onCreateHands(int handId,PVector pos,float time){
      println("onCreateHands - handId: " + handId + ", pos: " + pos + ", time:" + time);

      handsTrackFlag = true;
      handVec = pos;
    }

    void onUpdateHands(int handId,PVector pos,float time){
      //println("onUpdateHandsCb - handId: " + handId + ", pos: " + pos + ", time:" + time);
      handVec = pos;
    }

    void onDestroyHands(int handId,float time){
      println("onDestroyHandsCb - handId: " + handId + ", time:" + time);
      handsTrackFlag = false;
      context.addGesture(lastGesture);
    }

    // -----------------------------------------------------------------
    // gesture events

    void onRecognizeGesture(String strGesture, PVector idPosition, PVector endPosition){
      if(strGesture == "Click") println("onRecognizeGesture - strGesture: " + strGesture + ", idPosition: " + idPosition + ", endPosition:" + endPosition);

      lastGesture = strGesture;
      context.removeGesture(strGesture); 
      context.startTrackingHands(endPosition);
    }

    void onProgressGesture(String strGesture, PVector position,float progress){
      //println("onProgressGesture - strGesture: " + strGesture + ", position: " + position + ", progress:" + progress);
    }

    //---------------------------------------------------------------------------
    void splash(){
      stroke(255, 0, 0);
      fill(random(0,255),random(0,255),random(0,255));
      star((int)random(2, 8), mouseX-random(100), mouseY-random(100), random(100), random(100), -PI / 2.0, random(0, 1.5));
      star((int)random(2, 8), mouseX+random(200), mouseY+random(140), random(100), random(100), -PI / 2.0, random(0, 3.0));
      star((int)random(2, 8), mouseX+random(50), mouseY-random(78), random(100), random(100), -PI / 2.0, random(0, 2));
      star((int)random(2, 8), mouseX-random(120), mouseY+random(100), random(100), random(100), -PI / 2.0, random(0, 0.50));
    //  ellipse(mouseX + 50, mouseY + 50,random(40),random(40));

    }
     
    void star(int n, float cx, float cy, float r, float proportion)
    {
      star(n, cx, cy, 2.0 * r, 2.0 * r, 0.0, proportion);
     
    }
     
    void star(int n, float cx, float cy, float w, float h,  float startAngle, float proportion)
    {
      if (n > 2)
      {
        float angle = TWO_PI/ (random(2,6) *n);
        float dw; // draw width
        float dh; // draw height
         
        w = w / 2.0;
        h = h / 2.0;
         
        topLayer.beginShape();
        for (int i = 0; i < random(4, 12) * n; i++)
        {
          dw = w;
          dh = h;
          if (i % 2 == 1) // for odd vertices, use short radius
          {
            dw = w * proportion;
            dh = h * proportion;
          }
         topLayer.curveVertex(cx + dw * cos(startAngle + angle * i) + random(10),
            cy + dh * sin(startAngle + angle * i) + random(10));
        }
        topLayer.endShape(CLOSE);
         
      }

    }
     
    void deleteDrawing(){
      if(keyPressed && key == 'd'){
        setup();
      }
    }

Replies(4)

Moved to Contributed Library section, because your code requires a contributed library.

You should look at the reference for PGraphics. There are several errors in your code when it comes to PGraphics.

First, why use g.getClass().getName()? Just use:
Copy code
  1.   topLayer = createGraphics(640, 480, JAVA2D);

Second and most importantly: all calls to a PGraphics must be between beginDraw() and endDraw() calls. There are no such calls in your sketch.

Third, PGraphics that aren't drawn to (as is the case here) may give a NPE (depending on Processing version and renderer). Before you use the PGraphics in an image() calls there must in those cases be at least a call to beginDraw-endDraw() once to initialize the PGraphics and prevent NPE's.

Restored. Please don't delete threads with answers.




I AM REALLY SORRY .... .ONCE I DID THE SAME MISTAKE
thanks amnon.owed .... I have solved the problem