I want draw a line between to specify point in java 3d.
how can i do it?
for example for draw a cube we write colorcube开发者_StackOverflow.
please help me.
Use the LineArray
class. Create an object for it with two vertices( wherever you want them to be) and add this to a Shape3D object.i.e. new Shape3D(lineArr)
.
For line array:
LineArray lineArr=new LineArray(2,LineArray.COORDINATES);
then:
lineArr.setCoordinate(0,new Point3f());...
do the same for the other vertex.
Then add the shape3D
object to a scene graph or Branchgroup
.
That should do the trick.
This post might help you: How to draw lines with Java3D [java-tips.org]
The Java code below can generate lines on your 3D screen:
First, build a main class (i.e :tuval1) and second a public class (i.e tuval7) as below.
Also see this link: http://www.itk.ilstu.edu/faculty/javila/ITk356/Java3D/geometry.htm#3.4.2 Point* Classes:
import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.LineStripArray;
import javax.media.j3d.Shape3D;
import javax.vecmath.Point3d;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class tuval7 {
public tuval7(){
SimpleUniverse u=new SimpleUniverse();
BranchGroup group=new BranchGroup();
Point3d coords[] = new Point3d[4];
Appearance app=new Appearance();
coords[0] = new Point3d(-0.5d, -0.2d, 0.1d);
coords[1] = new Point3d(-0.2d, 0.1d, 0.0d);
coords[2] = new Point3d(0.2d, -0.3d, 0.1d);
coords[3] = new Point3d(0.3d, 0.5d, 0.10d);
int vertexCounts[] = {4};
LineStripArray lines = new LineStripArray(4,
GeometryArray.COORDINATES, vertexCounts);
lines.setCoordinates(0, coords);
Shape3D shape=new Shape3D(lines , app);
group.addChild(shape);
u.addBranchGraph(group);
u.getViewingPlatform().setNominalViewingTransform();
}
}
public class tuval1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
new tuval7();
}
}
This worked for me it draws the x-axis:
LineArray lineX = new LineArray(2, LineArray.COORDINATES);
lineX.setCoordinate(0, new Point3f(-100.0f, 0.0f, 0.0f));
lineX.setCoordinate(1, new Point3f(100.0f, 0.0f, 0.0f));
scene.addChild(new Shape3D(lineX));
a colored line can be drawn like this
Appearance appearanceGreen = new Appearance();
ColoringAttributes coloringAttributesGreen = new ColoringAttributes();
coloringAttributesGreen.setColor(new Color3f(Color.green));
appearanceGreen.setColoringAttributes(coloringAttributesGreen);
Shape3D shapeLine = new Shape3D(lineX, appearanceGreen);
scene.addChild(shapeLine);
精彩评论