public synchronized void paint(Graphics g) {
    if (mLastFrame == null) {
      return;
    }

    int framePosX = 0;
    int framePosY = 0;

    VideoFrameRef depthFrame = mLastFrame.getDepthFrame();
    if (depthFrame != null) {
      int width = depthFrame.getWidth();
      int height = depthFrame.getHeight();

      // make sure we have enough room
      if (mBufferedImage == null
          || mBufferedImage.getWidth() != width
          || mBufferedImage.getHeight() != height) {
        mBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      }

      mBufferedImage.setRGB(0, 0, width, height, mDepthPixels, 0, width);

      framePosX = (getWidth() - width) / 2;
      framePosY = (getHeight() - height) / 2;

      g.drawImage(mBufferedImage, framePosX, framePosY, null);
    }

    for (UserData user : mLastFrame.getUsers()) {
      if (user.getSkeleton().getState() == SkeletonState.TRACKED) {
        drawSkeleton(g, framePosX, framePosY, user);

        // spatial joint's coordinates
        //				giveMeSpatialCoordinateJoints(user);

        try {
          createSkeletonInstances(user);
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }

      if (user.getSkeleton().getState() == SkeletonState.TRACKED && startedTest) {
        try {
          createSkeletonInstances(user);
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        // System.out.println("send");
      }
    }
  }
  public synchronized void onNewFrame(UserTracker tracker) {
    if (mLastFrame != null) {
      mLastFrame.release();
      mLastFrame = null;
    }

    mLastFrame = mTracker.readFrame();

    // check if any new user detected
    for (UserData user : mLastFrame.getUsers()) {
      if (user.isNew()) {
        // start skeleton tracking
        mTracker.startSkeletonTracking(user.getId());
      }
    }

    VideoFrameRef depthFrame = mLastFrame.getDepthFrame();

    if (depthFrame != null) {
      ByteBuffer frameData = depthFrame.getData().order(ByteOrder.LITTLE_ENDIAN);
      ByteBuffer usersFrame = mLastFrame.getUserMap().getPixels().order(ByteOrder.LITTLE_ENDIAN);

      // make sure we have enough room
      if (mDepthPixels == null
          || mDepthPixels.length < depthFrame.getWidth() * depthFrame.getHeight()) {
        mDepthPixels = new int[depthFrame.getWidth() * depthFrame.getHeight()];
      }

      calcHist(frameData);
      frameData.rewind();

      int pos = 0;
      while (frameData.remaining() > 0) {
        short depth = frameData.getShort();
        short userId = usersFrame.getShort();
        short pixel = (short) mHistogram[depth];
        int color = 0xFFFFFFFF;
        if (userId > 0) {
          color = mColors[userId % mColors.length];
        }

        mDepthPixels[pos] = color & (0xFF000000 | (pixel << 16) | (pixel << 8) | pixel);
        pos++;
      }
    }

    repaint();
  }
  private void drawLimb(Graphics g, int x, int y, UserData user, JointType from, JointType to) {
    com.primesense.nite.SkeletonJoint fromJoint = user.getSkeleton().getJoint(from);
    com.primesense.nite.SkeletonJoint toJoint = user.getSkeleton().getJoint(to);

    if (fromJoint.getPositionConfidence() == 0.0 || toJoint.getPositionConfidence() == 0.0) {
      return;
    }

    com.primesense.nite.Point2D<Float> fromPos =
        mTracker.convertJointCoordinatesToDepth(fromJoint.getPosition());
    com.primesense.nite.Point2D<Float> toPos =
        mTracker.convertJointCoordinatesToDepth(toJoint.getPosition());

    // draw it in another color than the use color
    g.setColor(new Color(mColors[(user.getId() + 1) % mColors.length]));
    g.drawLine(
        x + fromPos.getX().intValue(),
        y + fromPos.getY().intValue(),
        x + toPos.getX().intValue(),
        y + toPos.getY().intValue());
  }
  private void createSkeletonInstances(UserData user) throws Exception {

    skeletonIstances = new ArrayList<Instance>();

    Skeleton skeleton = user.getSkeleton();
    double torso_conf = skeleton.getJoint(JointType.TORSO).getPositionConfidence();

    if (torso_conf >= TORSO_CONF) {
      // se TORSO rilevato correttamente : IMPORTANTE per la correttezza
      // delle distanze dei vari limb

      Point3D<Float> torso_pos = skeleton.getJoint(JointType.TORSO).getPosition();

      // save origin's coordinates
      x_torso = (Float) torso_pos.getX();
      y_torso = (Float) torso_pos.getY();
      z_torso = (Float) torso_pos.getZ();

      createLimbInstance(skeleton, TRACKER_HEAD_LIMB, HEAD_ABOUT, head_att);

      createLimbInstance(skeleton, TRACKER_BODY_UPRIGHT_LIMB, BODY_UPRIGHT_ABOUT, body_upright_att);
      createLimbInstance(
          skeleton,
          TRACKER_BODY_STAN_SLANTING_LIMB,
          BODY_STAN_SLANTING_ABOUT,
          body_stan_slanting_att);
      createLimbInstance(
          skeleton, TRACKER_BODY_SIT_SLANTING_LIMB, BODY_SIT_SLANTING_ABOUT, body_sit_slanting_att);

      createLimbInstance(skeleton, TRACKER_LEFT_ARM_LIMB, LEFT_ARM_ABOUT, left_arm_att);
      createLimbInstance(skeleton, TRACKER_RIGHT_ARM_LIMB, RIGHT_ARM_ABOUT, right_arm_att);
      createLimbInstance(skeleton, TRACKER_LEFT_LEG_LIMB, LEFT_LEG_ABOUT, left_leg_att);
      createLimbInstance(skeleton, TRACKER_RIGHT_LEG_LIMB, RIGHT_LEG_ABOUT, right_leg_att);

      sendInstance();
    }
  }