博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

点、线、面的生成(三)——线的介绍

Posted on 2009-06-27 11:16  Anna Yang  阅读(528)  评论(0编辑  收藏  举报

Java3D提供的API中,生成的直线对象有:

LineArray         IndexedLineArray               LineStripArray             IndexedLineStripArray          //后两个对象是与折线相关的

在介绍直线之前,先介绍一下直线属性的设置对象:LineAttributes对象

其构造方法为LineAttributes()或LineAttributes(float lineWidth, int linePattern , boolean lineAntialiasing)

linePattern 包括:pattern_solid ,  pattern_dash,        pattern_dot,   pattern_dash_dot

lineAntialiasing 为true时,显示的直线是经过修饰的,有光滑的边缘,而当lineAntialiasing=false或默认时,显示的是长方体的直线,即有很明显的边缘。

LineAttributes有6个flags,分别是lineWidth,linePattern ,lineAntialiasing的读和写,可参照PointAttribute,起作用也与PointAttribute相同

LineAttributes的主要方法:setLineWidth(float lineWidth),     setLinePattern(int linePattern),   setLineAntialiasingEnable(boolean state)

LineArray对象

LineArray的构造方法:LineArray(int vertexCount,  int vertexFormat) 

要求:1.用于生成多条直线,按下标顺序每两个顶点定义一条直线,点无重叠,除非在实际定义点的坐标时将同一个点定义多次。

        2.顶点及颜色的个数必须相等且必须为偶数,(这一点在PointArray中没有要求,当实际颜色的个数多于实际顶点的个数时也能正常显示)

        3.vertexCount不能小于实际提供的顶点个数,这一点和PointArray中相同

示例如下:

模块:将共同需要用到的方法放入一个类中,将Shape3D图形生成的方法放入另一个类中,避免重复的工作

共同类:

import java.applet.Applet;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class Line1 extends Applet{
private BranchGroup createSceneGraph()
{
 BranchGroup objRoot=new BranchGroup();
    BoundingSphere bounds=new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0); 
 Color3f bgColor=new Color3f(1.0f,1.0f,1.0f);
 Background bg=new Background(bgColor);
 bg.setApplicationBounds(bounds);
 objRoot.addChild(bg);
 
 Shape3D shape=new lineShape();
 objRoot.addChild(shape);
 objRoot.compile();
 return objRoot;
}
public Line1()
{
 setLayout(new BorderLayout());
 GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration();
 Canvas3D c=new Canvas3D(config);
 add("Center",c);
 BranchGroup scene=createSceneGraph();
 SimpleUniverse u=new SimpleUniverse(c);
 u.getViewingPlatform().setNominalViewingTransform();
 u.addBranchGraph(scene);
}
public static void main(String[] args)
{
 new MainFrame(new wind(),600,600);
}
}

调用lineShape类生成LineArray的Shape3D图形:

import javax.media.j3d.*;
public class lineShape extends Shape3D{
private float vert[]=
{
 -0.6f,0.4f,0.0f,       -0.6f,-0.4f,0.0f,
 -0.2f,0.4f,0.0f,       -0.2f,-0.4f,0.0f,
 0.2f,0.4f,0.0f,         0.2f,-0.4f,0.0f,
 0.6f,0.4f,0.0f,         0.6f,-0.4f,0.0f,
};
private float color[]=
{
 0.0f,0.5f,1.0f,   0.5f,0.0f,1.0f,
 0.0f,0.8f,2.0f,   1.0f,0.0f,0.3f,
 0.0f,1.0f,0.3f,   0.3f,0.8f,0.0f,
 0.5f,0.3f,0.3f,   0.0f,0.4f,0.1f,
};
public lineShape()
{
 
LineArray line=new LineArray(8,LineArray.COORDINATES|LineArray.COLOR_3);
 line.setCoordinates(0, vert);
 line.setColors(0, color);
 LineAttributes la=new LineAttributes();
 la.setLineWidth(5.0f);
 la.setLineAntialiasingEnable(true);
 Appearance ap=new Appearance();
 ap.setLineAttributes(la);
 this.setGeometry(line);
 this.setAppearance(ap); 
}
}

显示效果如下:                                         若将vert[]数组中的坐标( -0.2f,0.4f,0.0f, )改为(-0.6f,-0.4f,0.0f,)则会共用第二个点,效果如下:

                   

LineStripArray对象

构造函数:LineStripArray(int vertexCount,int vertFormat, int[] StripVertexCounts)  //用于生成多条折线,每条折线有2个或多个点构成

StripVertexCounts为一个数组,数组里的每一个值表示每条折线所拥有的顶点数目

将上述lineShape()函数中蓝色的部分代码改为如下:

int sCount[]=new int[2];
 sCount[0]=3;
 sCount[1]=3;
 LineStripArray line=new LineStripArray(8,LineStripArray.COORDINATES|LineStripArray.COLOR_3,sCount);

则显示的效果如下(按点的顺序显示,不重叠,除非重复定义某个点,和上面的理论一样)

IndexedLineArray对象

构造方法为:IndexedLineArray(int vertextCount, int vertexFormat, int indexCount)

作用:用于从多个点中选择特定的点生成多条直线,所挑选的坐标下标要放在一个数组中

indexCount:表示选用的顶点个数,若一个点用了多次,则要按多次计算

若要从上述lineShape类中挑选4各点来显示:则蓝色部分代码替换如下:

 int indexCount=4;    //所选下标个数,包括重复的选择
 int[] index={0,1,1,4};  //存放所挑选的下标值
 IndexedLineArray line=new IndexedLineArray(8,IndexedLineArray.COORDINATES|IndexedLineArray.COLOR_3,indexCount);
 line.setCoordinateIndices(0, index);//定义要显示的点的坐标
 line.setColorIndices(0, index);//定义要显示的点的颜色

显示效果如下:

 

 IndexedLineStripArray对象

构造函数如下:IndexedLineStripArray(int vertexCount, int vertexFormat, int indexCount, int stripIndexCounts[])

作用:从顶点坐标数组中选择部分顶点,将其分成几组,每个组的各个顶点链接起来,生成相应的折线,程序中需要定义一个数组来存放做选顶点的下标

stripIndexCount为一数组,数组里每一个值表示每条折线所拥有的顶点数

将上述lineShape中蓝色部分该为如下代码:

int indexCount=7;
 int[] sCount=new int[2];
 int[] index={0,2,1,6,4,1,3};
 sCount[0]=3;
 sCount[1]=4;
 IndexedLineStripArray line=new IndexedLineStripArray(8,IndexedLineStripArray.COORDINATES|IndexedLineStripArray.COLOR_3,indexCount,sCount);
 line.setCoordinateIndices(0, index);//定义要显示的点的坐标
 line.setColorIndices(0, index);//定义要显示的点的颜色

显示效果如下:

注:除非重复定义了某个点,否则每个点只被使用一次,即直线与直线之间,或折线与折线之间不会相连接。若重复定义了n此,则点被重复使用n此