简体   繁体   English

在java3d中画线

[英]Draw line in java3d

I want draw a line between to specify point in java 3d. 我想画一条线来指定java 3d中的点。

how can i do it? 我该怎么做?

for example for draw a cube we write colorcube. 例如,为了绘制立方体,我们编写了colorcube。

please help me. 请帮我。

Use the LineArray class. 使用LineArray类。 Create an object for it with two vertices( wherever you want them to be) and add this to a Shape3D object.ie new Shape3D(lineArr) . 使用两个顶点(无论您希望它们在哪里)为它创建一个对象,并将其添加到Shape3D对象。 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 . 然后将shape3D对象添加到场景图或Branchgroup

That should do the trick. 这应该够了吧。

这篇文章可能会对你有所帮助: 如何使用Java3D绘制线条[java-tips.org]

This worked for me it draws the x-axis: 这对我有用,它绘制了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));

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

The Java code below can generate lines on your 3D screen: 下面的Java代码可以在3D屏幕上生成线条:

First, build a main class (ie :tuval1) and second a public class (ie tuval7) as below. 首先,构建一个主类(即:tuval1),然后构建一个公共类(即tuval7),如下所示。

Also see this link: http://www.itk.ilstu.edu/faculty/javila/ITk356/Java3D/geometry.htm#3.4.2 Point* Classes: 另请参阅此链接: 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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM