📄 figure.java
字号:
package Mover3D;
// Figure.java
// Thana Konglikhit, October 2003, s4310170@maliwan.psu.ac.th
// Andrew Davison, April 2005, ad@fivedots.coe.psu.ac.th
/* The Figure class carries out three main tasks:
1. constructs the figure by connecting Limb objects;
2. processes limb commands by passing them
onto the relavant Limb objects
3. processes figure commands by moving or rotating
the TransformGroup for the figure (figureTG).
The Limb hierarchy:
Limb
|---------------------
| |
MoveableLimb EllipticLimb
|
|
MoveableEllipticLimb
Limb defines the appearance of a limb (using a Lathe shape),
and how it is connected to other limbs via joints.
The limb's initial orientation is set. Limb and EllipticLimb
cannot be moved, and do not use limb names.
The MoveableLimb and MoveableEllipticLimb classes are moveable.
They have limb names, and x-, y-, z- axis ranges in which they
can be rotated. If a range is not specified, then it is assumed
to be 0 (i.e. rotation is not possible around that axis).
The figure's component limb objects are stored in the limbs
ArrayList. A limb object is stored in the ArrayList at the
position specified by its limb number.
A limbNames hashmap stores (limb name, no.) pairs.
It is used to determine a limb's number when a limb name is
supplied by the user in a limb command.
*/
import javax.media.j3d.*;
import javax.vecmath.*;
import java.util.*;
public class Figure
{
// figure movement constants
private final static double MOVERATE = 0.3;
private final static double ROTATE_AMT = Math.PI / 16.0; // 11.25 degrees
private final static int FWD = 0;
private final static int BACK = 1;
private final static int LEFT = 2;
private final static int RIGHT = 3;
private final static int UP = 4;
private final static int DOWN = 5;
private final static int CLOCK = 0; // clockwise turn
private final static int CCLOCK = 1; // counter clockwise
// axis constants
private final static int X_AXIS = 0;
private final static int Y_AXIS = 1;
private final static int Z_AXIS = 2;
// step to move in each direction
private static final Vector3d fwdVec = new Vector3d(0, 0, MOVERATE);
private static final Vector3d backVec = new Vector3d(0, 0, -MOVERATE);
private static final Vector3d leftVec = new Vector3d(-MOVERATE, 0, 0);
private static final Vector3d rightVec = new Vector3d(MOVERATE, 0, 0);
private static final Vector3d upVec = new Vector3d(0, MOVERATE, 0);
private static final Vector3d downVec = new Vector3d(0, -MOVERATE, 0);
private ArrayList limbs;
// Arraylist of Limb objects, indexed by limb number
private HashMap limbNames;
// holds (name, limbNo) pairs of visible limbs
// for moving the figure as a whole
private TransformGroup figureTG;
private Transform3D t3d, toMove, toRot; // for TG operations
private int yCount; // used to position the figure along the y-axis
public Figure()
{
yCount = 0; // the figure is on the floor initially
t3d = new Transform3D();
toMove = new Transform3D();
toRot = new Transform3D();
limbs = new ArrayList();
limbNames = new HashMap();
// construct the figure in parts (connected Limb objects)
buildTorso();
buildHead();
buildRightArm();
buildLeftArm();
buildRightLeg();
buildLeftLeg();
printLimbsInfo();
buildFigureGraph(); // convert the figure into a Java 3D graph
} // end of Figure()
private void buildTorso()
/* A limb requires a limb number, its start and end joints,
an axis of orientation and angle to that axis, and lathe
shape coordinates and texture.
If the limb will be moveable (i.e. a MoveableLimb or
MoveableEllipticLimb object), then it also requires a
name and x-, y-, z- ranges to restrict its movements.
*/
{
// the figure's bottom
double xsIn1[] = {0, -0.1, 0.22, -0.2, 0.001};
double ysIn1[] = {0, 0.03, 0.08, 0.25, 0.25};
EllipticLimb limb1 = new EllipticLimb(
1, "j0", "j1", Z_AXIS, 0, xsIn1, ysIn1, "denim.jpg");
// no movement, so no name or ranges
// the figure's chest: moveable so has a name ("chest") and rotation ranges
double xsIn2[] = {-0.001, -0.2, 0.36, 0.001};
double ysIn2[] = {0, 0, 0.50, 0.68};
MoveableEllipticLimb limb2 = new MoveableEllipticLimb("chest",
2, "j1", "j2", Z_AXIS, 0, xsIn2, ysIn2, "camoflage.jpg");
limb2.setRanges(0, 120, -60, 60, -40, 40);
// x range: 0 to 120; y range: -60 to 60; z range: -40 to 40
limbs.add(limb1);
limbs.add(limb2);
limbNames.put("chest", new Integer(2));
// store (name,number) pairs in limbNames
} // end of buildTorso()
private void buildHead()
{
// figure's neck
double xsIn3[] = {-0.001, -0.09, -0.09, 0.001};
double ysIn3[] = {0, 0, 0.1, 0.1};
Limb limb3 = new Limb(
3, "j2", "j3", Z_AXIS, 0, xsIn3, ysIn3, "skin.jpg");
// no movement
// figure's head
double xsIn4[] = {-0.001, 0.09, 0.17, 0};
double ysIn4[] = {0, 0, 0.2, 0.4};
MoveableLimb limb4 = new MoveableLimb("head",
4, "j3", "j4", Z_AXIS, 0, xsIn4, ysIn4, "head.jpg");
limb4.setRanges(-40, 40, -40, 40, -30, 30);
limbs.add(limb3);
limbs.add(limb4);
limbNames.put("head", new Integer(4));
} // end of buildHead()
private void buildRightArm()
{
/* An invisible limb connecting the neck and upper right arm.
It is invisible since no lathe shape coords or texture
are supplied. However, it does have a length (0.35). */
Limb limb5 = new Limb(5, "j2", "j5", Z_AXIS, 95, 0.35);
// upper right arm
double xsIn6[] = {0, 0.1, 0.08, 0};
double ysIn6[] = {0, 0.08, 0.45, 0.55};
MoveableLimb limb6 = new MoveableLimb("urArm",
6, "j5", "j6", Z_AXIS, 80, xsIn6, ysIn6, "rightarm.jpg");
limb6.setRanges(-60, 180, -90, 90, -90, 30);
// lower right arm
double xsIn7[] = {0, 0.08, 0.055, 0};
double ysIn7[] = {0, 0.08, 0.38, 0.43};
MoveableLimb limb7 = new MoveableLimb("lrArm",
7, "j6", "j7", Z_AXIS, 5, xsIn7, ysIn7, "skin.jpg");
limb7.setRanges(0, 150, -90, 90, -90, 90);
// right hand
double xsIn8[] = {0, 0.06, 0.04, 0};
double ysIn8[] = {0, 0.07, 0.16, 0.2};
MoveableEllipticLimb limb8 = new MoveableEllipticLimb("rHand",
8, "j7", "j8", Z_AXIS, 0, xsIn8, ysIn8, "skin.jpg");
limb8.setRanges(-50, 50, -40, 90, -40, 40);
limbs.add(limb5);
limbs.add(limb6);
limbs.add(limb7);
limbs.add(limb8);
limbNames.put("urArm", new Integer(6));
limbNames.put("lrArm", new Integer(7));
limbNames.put("rHand", new Integer(8));
} // end of buildRightArm()
private void buildLeftArm()
// very similar to buildRightArm()
{
// invisible limb connecting the neck and upper left arm
Limb limb9 = new Limb(9, "j2", "j9", Z_AXIS, -95, 0.35);
// upper left arm
double xsIn10[] = {0, 0.1, 0.08, 0};
double ysIn10[] = {0, 0.08, 0.45, 0.55};
MoveableLimb limb10 = new MoveableLimb("ulArm",
10, "j9", "j10", Z_AXIS, -80, xsIn10, ysIn10, "leftarm.jpg");
limb10.setRanges(-60, 180, -90, 90, -30, 90);
// lower left arm
double xsIn11[] = {0, 0.08, 0.055, 0};
double ysIn11[] = {0, 0.08, 0.38, 0.43};
MoveableLimb limb11 = new MoveableLimb("llArm",
11, "j10", "j11", Z_AXIS, -5, xsIn11, ysIn11, "skin.jpg");
limb11.setRanges(0, 150, -90, 90, -90, 90);
// left hand
double xsIn12[] = {0, 0.06, 0.04, 0};
double ysIn12[] = {0, 0.07, 0.16, 0.2};
MoveableEllipticLimb limb12 = new MoveableEllipticLimb("lHand",
12, "j11", "j12", Z_AXIS, 0, xsIn12, ysIn12, "skin.jpg");
limb12.setRanges(-50, 50, -90, 40, -40, 40);
limbs.add(limb9);
limbs.add(limb10);
limbs.add(limb11);
limbs.add(limb12);
limbNames.put("ulArm", new Integer(10));
limbNames.put("llArm", new Integer(11));
limbNames.put("lHand", new Integer(12));
} // end of buildLeftArm()
private void buildRightLeg()
{
// invisible limb connecting the bottom and upper right leg
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -