⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 figure.java

📁 JAVA的一个程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    Limb limb13 = new Limb(13, "j0", "j13", Z_AXIS, 50, 0.20);

    // upper right leg
    double xsIn14[] = {0, 0.12, 0.1, 0};
    double ysIn14[] = {0, 0.1, 0.6, 0.7};
    MoveableLimb limb14 = new MoveableLimb("urLeg",
         14, "j13", "j14", Z_AXIS, 130, xsIn14, ysIn14, "denim.jpg");
    limb14.setRanges(-45, 80, -20, 20, -45, 30);

    // lower right leg
    double xsIn15[] = {0, 0.10, 0.06, 0};
    double ysIn15[] = {0, 0.15, 0.62, 0.7};
    MoveableLimb limb15 = new MoveableLimb("lrLeg",
         15, "j14", "j15", Z_AXIS, 0, xsIn15, ysIn15, "lowDenim.jpg");
    limb15.setRange(X_AXIS, -120, 0);

    // invisible limb connecting lower right leg and right foot
    Limb limb16 = new Limb(16, "j15", "j16", Z_AXIS, 0, 0.07);
    
    // right foot
    double xsIn17[] = {0, 0.08, 0.06, 0};
    double ysIn17[] = {0, 0.07, 0.21, 0.25};
    MoveableEllipticLimb limb17 = new MoveableEllipticLimb("rFoot",
         17, "j16", "j17", X_AXIS, 90, xsIn17, ysIn17, "shoes.jpg");
    limb17.setRanges(-90, 0, 0, 0, -30, 30);

    limbs.add(limb13);
    limbs.add(limb14);
    limbs.add(limb15);
    limbs.add(limb16);   
    limbs.add(limb17);

    limbNames.put("urLeg", new Integer(14));
    limbNames.put("lrLeg", new Integer(15));
    limbNames.put("rFoot", new Integer(17));
  }  // end of buildRightLeg()



  private void buildLeftLeg()
  // very similar to buildRightLeg()
  {
    // invisible limb connecting the bottom and upper left leg
    Limb limb18 = new Limb(18, "j0", "j18", Z_AXIS, -50, 0.20);

    // upper left leg
    double xsIn19[] = {0, 0.12, 0.1, 0};
    double ysIn19[] = {0, 0.1, 0.6, 0.7};
    MoveableLimb limb19 = new MoveableLimb("ulLeg",
         19, "j18", "j19", Z_AXIS, -130, xsIn19, ysIn19, "denim.jpg");
    limb19.setRanges(-45, 80, -20, 20, -30, 45);

    // lower left leg
    double xsIn20[] = {0, 0.10, 0.06, 0};
    double ysIn20[] = {0, 0.15, 0.62, 0.7};
    MoveableLimb limb20 = new MoveableLimb("llLeg",
         20, "j19", "j20", Z_AXIS, 0, xsIn20, ysIn20, "lowDenim.jpg");
    limb20.setRange(X_AXIS, -120, 0);

    // invisible limb connecting between lower left leg and left foot
    Limb limb21 = new Limb(21, "j20", "j21", Z_AXIS, 0, 0.07);

    // left foot
    double xsIn22[] = {0, 0.08, 0.06, 0};
    double ysIn22[] = {0, 0.07, 0.21, 0.25};
    MoveableEllipticLimb limb22 = new MoveableEllipticLimb("lFoot",
       22, "j21", "j22", X_AXIS, 90, xsIn22, ysIn22, "shoes.jpg"); 
    limb22.setRanges(-90, 0, 0, 0, -30, 30);

    limbs.add(limb18);
    limbs.add(limb19);
    limbs.add(limb20);
    limbs.add(limb21);
    limbs.add(limb22);

    limbNames.put("ulLeg", new Integer(19));
    limbNames.put("llLeg", new Integer(20));
    limbNames.put("lFoot", new Integer(22));
  }  // end of buildLeftLeg()



  private void printLimbsInfo()
  /* Used for debugging.
     Print limb info for each Limb object, and the
     name->number limb mapping.  */
  {
   /*
    for(int i = 0; i < limbs.size(); i++)   
      ((Limb)limbs.get(i)).printLimb();
    */

    int i = 0;
    Iterator it = limbNames.keySet().iterator();
    while( it.hasNext() ){
      String lNm = (String) it.next();
      System.out.print( "(" + lNm + " = " + limbNames.get(lNm) + ") " );
      i++;
      if (i == 5) {    // max number of elements on a line
        System.out.println();
        i = 0;
      }
    }
    System.out.println();
  }  // end of printLimbsInfo()


  private void buildFigureGraph()
  /* Convert the figure into a Java 3D subgraph, stored in figureTG.

     offsetTG is connected to figureTG; it specifies the distance
     from the floor to the first joint and limb.

     The figure's graph will hang below offsetTG.  */
  { 
    HashMap joints = new HashMap();  
    /* joints will contain (jointName, TG) pairs. Each TG is the
       position of the joint in the scene.
       
       A limb connected to a joint is placed in the scene by 
       using the TG associated with that joint.  */

    figureTG = new TransformGroup();
    figureTG.setCapability( TransformGroup.ALLOW_TRANSFORM_READ);
    figureTG.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE);  // can alter

    TransformGroup offsetTG = new TransformGroup();
    Transform3D trans = new Transform3D();
    trans.setTranslation( new Vector3d(0, 1.24, 0));  
          // an offset from the ground to the first joint
    offsetTG.setTransform( trans );  

    joints.put( "j0", offsetTG);   // store starting joint (assume it is j0)

    /* Grow the subgraph for each limb object, attaching it
       to the figure's subgraph below offsetTG. */
    Limb li;
    for (int i = 0; i < limbs.size(); i++) {   
      li = (Limb)limbs.get(i);
      li.growLimb(joints);
    }

    figureTG.addChild(offsetTG);
  }  // end of buildFigureGraph()


  public TransformGroup getFigureTG()
  {  return figureTG;  }


  // ----------------- limb-related operations ---------------------


  public int checkLimbNo(int no)
  // Is no a limb number?
  {
    if (limbNames.containsValue( new Integer(no)))
      return no;
    else 
      return -1;    // no not found in limbNames
  }  // end of checkLimbNo()



  public int findLimbNo(String name)
  // Find the limb number for this name
  {
    Integer iNo = (Integer) limbNames.get(name);
    if (iNo == null)   // no limb of that name
      return -1;
    else 
      return iNo.intValue();
  }  // end of findLimbNo()


  public void updateLimb(int limbNo, int axis, double angle)
  /* Apply the limb operation to the limb with limbNo.
     The operation is a rotation of angle degress around axis.
  */
  { 
    Limb li = (Limb) limbs.get(limbNo-1);  // assume limbNo input is correct
    li.updateLimb(axis, angle);  // send axis and angle
  }  // end of updateLimb()


  public void reset()
  // restore each limb to its original position in space
  { Limb li;
    for (int i = 0; i < limbs.size(); i++) {
      li = (Limb)limbs.get(i);
      li.reset();
    }
  }  // end of reset()



  // ------------------- figure movement operations ------------

  public void doMove(int dir)
  /* Move the figure forwards, backwards, left, right, up, or down. 
     doMove() can be called from LocationBeh or CommandsPanel.  */
  {
    if (dir == FWD)
      doMoveVec(fwdVec);
    else if (dir == BACK)
      doMoveVec(backVec);
    else if (dir == LEFT)
      doMoveVec(leftVec);
    else if (dir == RIGHT)
      doMoveVec(rightVec);
    else if (dir == UP) {
      doMoveVec(upVec);
      yCount++;
    }
    else if (dir == DOWN) {
      if (yCount > 0) {
        doMoveVec(downVec);
        yCount--;
      }
    }
    else
      System.out.println("Unknown doMove() call");
  }  // end of doMove()


  private void doMoveVec(Vector3d theMove)
  // Move the figure by the amount in theMove
  {
    figureTG.getTransform( t3d );
    toMove.setTranslation(theMove);    // overwrite previous trans
    t3d.mul(toMove);
    figureTG.setTransform(t3d);
  }  // end of doMoveVec()



  public void doRotateY(int rotDir)
  /* Rotate the figure clockwise ot counter-clockwise around 
     the y-axis.
     doRotateY() can be called from LocationBeh or CommandsPanel.
  */
  { if (rotDir == CLOCK)
     doRotateYRad(-ROTATE_AMT);    // clockwise
    else if(rotDir == CCLOCK)
      doRotateYRad(ROTATE_AMT);    // counter-clockwise
    else
      System.out.println("Unknown doRotateY() call");
  }  // end of doRotateY()


  private void doRotateYRad(double radians)
  // Rotate the figure by radians amount around its y-axis
  {
    figureTG.getTransform( t3d );
    toRot.rotY(radians);   // overwrite previous rotation
    t3d.mul(toRot);
    figureTG.setTransform(t3d);
  } // end of doRotateYRad()


} // end of Figure class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -