关于vrml语言,用applet,实现添加与删除节点,运行不了
运行vrml与java交互,需要配置环境,还要相应的包,这里有没会的人,帮小弟看一下,非常谢谢。
vrml代码:
#VRML V2.0 utf8
Viewpoint
{
position 0 0 15
orientation 0 0 1 0
fieldOfView 0.8
}
Background
{
skyColor [ 0 0 0.2,
0.1 0.1 0.8,
0.7 0.8 0.9
]
skyAngle [1 1.57 ]
groundColor [ 0 0 0,
0.5 0.4 0.3,
0.3 0.3 0.3
]
groundAngle [1 1.57]
}
DEF Base Transform {
translation -5 0 0
}
}
DEF Base1 Transform {
translation -2 0 0
}
DEF Base2 Transform {
translation 1 0 0
}
DEF Base3 Transform {
translation 4 0 0
}
java代码:
import java.awt.*;
import java.applet.*;
import java.lang.*;
import vrml.external.*;
import vrml.external.field.*;
public class AddRemove extends Applet
{
Browser browser = null;
Node theBase = null;
Node theBase1 = null;
Node theBase2 = null;
Node theBase3 = null;
Node[] shape;
Node[] cone;
Node[] box;
Node[] cylinder;
EventInMFNode addChildren;
EventInMFNode addChildren1;
EventInMFNode addChildren2;
EventInMFNode addChildren3;
EventInMFNode removeChildren;
EventInMFNode removeChildren1;
EventInMFNode removeChildren2;
EventInMFNode removeChildren3;
public void init()
{
add(new Button("AddSphere"));
add(new Button("AddCylinder"));
add(new Button("AddBox"));
add(new Button("Addcone"));
add(new Button("DelSphere"));
add(new Button("DelCylinder"));
add(new Button("DelBox"));
add(new Button("Delcone"));
}
public void start()
{
browser = Browser.getBrowser(this);
theBase = browser.getNode("Base");
theBase1 = browser.getNode("Base1");
theBase2 = browser.getNode("Base2");
theBase3 = browser.getNode("Base3");
addChildren = (EventInMFNode)theBase.getEventIn("addChildren");
addChildren1 = (EventInMFNode)theBase1.getEventIn("addChildren");
addChildren2 = (EventInMFNode)theBase2.getEventIn("addChildren");
addChildren3 = (EventInMFNode)theBase3.getEventIn("addChildren");
removeChildren = (EventInMFNode)theBase.getEventIn("removeChildren");
removeChildren1 = (EventInMFNode)theBase1.getEventIn("removeChildren");
removeChildren2 = (EventInMFNode)theBase2.getEventIn("removeChildren");
removeChildren3 = (EventInMFNode)theBase3.getEventIn("removeChildren");
shape = browser.createVrmlFromString("Shape{\n" +
" appearance Appearance {\n" +
" material Material {\n" +
" diffuseColor 0.2 0.2 0.8\n" +
" }\n" +
" }\n" +
" geometry Sphere {}\n" +
"}\n");
cone = browser.createVrmlFromString("Shape{\n" +
" appearance Appearance {\n" +
" material Material {\n" +
" diffuseColor 0.2 0.2 0.8\n" +
" }\n" +
" }\n" +
" geometry Cone {}\n" +
"}\n");
box = browser.createVrmlFromString("Shape{\n" +
" appearance Appearance {\n" +
" material Material {\n" +
" diffuseColor 0.2 0.2 0.8\n" +
" }\n" +
" }\n" +
" geometry Box {}\n" +
"}\n");
cylinder = browser.createVrmlFromString("Shape{\n" +
" appearance Appearance {\n" +
" material Material {\n" +
" diffuseColor 0.2 0.2 0.8\n" +
" }\n" +
" }\n" +
" geometry Cone {}\n" +
"}\n");
}
public boolean action(Event event, Object what)
{
if (event.target instanceof Boolean) // instanceof 用于检测对象的类型
{
Button b = (Button)event.target;
if (b.getLabel() == "AddSphere")
{
addChildren.setValue(shape);
}
else if(b.getLabel() == "AddCylinder")
{
addChildren1.setValue(cylinder);
}
else if (b.getLabel() == "AddBox")
{
addChildren2.setValue(box);
}
else if (b.getLabel() == "AddCone")
{
addChildren3.setValue(cone);
}
else if (b.getLabel() == "DelSphere")
{
removeChildren.setValue(shape);
}
else if (b.getLabel() == "DelCylinder")
{
removeChildren1.setValue(cylinder);
}
else if (b.getLabel() == "DelBox")
{
removeChildren2.setValue(box);
}
else if (b.getLabel() == "DelCone")
{
removeChildren3.setValue(cone);
}
}
return true;
}
public static void main(String[] args)
{
}
}
编译,运行了。就是点击按钮没反应,找了挺长时间的,不知错在哪里,大家帮忙看下
------解决方案--------------------
同问,纠结ing