在java3d中画线

时间:2011-02-01 15:36:59

标签: java 3d

我想在java 3d中指定点之间画一条线。

我该怎么做?

例如,我们为彩色立方体绘制立方体。

请帮帮我。

4 个答案:

答案 0 :(得分:4)

使用LineArray课程。使用两个顶点(您希望它们在哪里)为它创建一个对象,并将其添加到Shape3D对象.i.e。 new Shape3D(lineArr)。 对于行数组:

LineArray lineArr=new LineArray(2,LineArray.COORDINATES);

然后:

lineArr.setCoordinate(0,new Point3f());...

对另一个顶点做同样的事。

然后将shape3D对象添加到场景图或Branchgroup

这应该可以解决问题。

答案 1 :(得分:2)

这篇文章可能会对您有所帮助:How to draw lines with Java3D [java-tips.org]

答案 2 :(得分:1)

这对我有用,它绘制了x轴:

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));

可以像这样绘制彩色线

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);

答案 3 :(得分:0)

下面的Java代码可以在3D屏幕上生成线条:

首先,建立一个主类(即:tuval1),然后建立一个公共类(即tuval7),如下所示。

另见此链接: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();
    }

}