Java communicates with VRML using EAI mode

Last week several students came to ask me how to use java to  control VRML?This question remembered me something:the same question was asked 2 years ago by one friend of mine,and I even forgot to tell him how.That's really too bad.He is now in Copenhagen,Denmark. I e-mailed him to ask this,he just totally  forgot  it. Well,things in the world are usually out of the control of  your own hand,perhaps at that time I thought I had told him how.But now,I decide to write down the answer,though an old tech,it is useful for the beginners to understand something.

This article assumes you are familiar with Java,VRML and HTML(know about is OK).

To run the example below rightly you should eatablish its running envrionment.This is not our main point to discuss in this article,I'll just give u a clue and the environment information.The running environment is necessary to learn the java EAI mode communicating with VRML.

1.the "vrml97.jar" should be copied to the dir "jre/lib/etc".

2.one VRML plugin is necessary, you may choose"Cortona","Blaxxun" or "BS" as u like.

3.the classpath should be like "C:/jdk1.3.1_06/jre/lib/rt.jar;C:/jdk1.3.1_06/jre/lib/i18n.jar;C:/jdk1.3.1_06/lib/dt.jar;C:/jdk1.3.1_06/lib/tools.jar;C:/Program Files/Common Files/ParallelGraphics/Cortona/classes.zip;C:/Program Files/Common Files/ParallelGraphics/Cortona/corteai.zip
",here one thing I should mention is the jdk version,the 1.3 series is recommended.

4.your Microsoft VM should be reset to "Java Console Enabled (yes)
Java compiler (no)
Java Logging (yes) "

The following is the example,it includes three files:the create.wrl,the index.htm and the createbox.class,which was complied from the createbox.java.We use the java applet embedded in the html to control the wrl object embedded in the html.That's the main idea of EAI.

the index.htm(I use the cortona plugin):

<html>
<head><title>Create Sphere</title></head>
<body>

<object name="cortona" id="cortona" 

codebase
="http://www.parallelgraphics.com/bin/cortvrml.cab#Version=4,0,0,76"

classid
="CLSID:86A88967-7A20-11d2-8EDA-00600818EDB1" width="52%" height="58%"
type
="x-world/x-vrml" standby="Loading...">
  
<param name="src" value="create.wrl">
  
<param name="BackColor" value="#FFFFFF">
  
<param name="VRML_DASHBOARD" value="FALSE">
  
<param name="WaitForAllResources" value="TRUE">
  
<param name="VRML_SPLASHSCREEN" value="FALSE">
  
<embed name="cortona" id="cortona" 

pluginspage
="http://www.parallelgraphics.com/products/cortona/download/"

 type
="x-world/x-vrml" width="52%" height="58%" src="create.wrl"
 vrml_background_color
="#FFFFFF" vrml_dashboard="FALSE" waitforallresources="TRUE" 

vrml_splashscreen
="FALSE">
 
  
</embed>
 
</object>


<applet code="createbox.class" mayscript height="30" width="80"></applet>

</body>
</html>

the create.wrl:(a very simple one)

 

 

#VRML V2.0 utf8

DEF NewNode Group {}
Shape {
   appearance Appearance {
      material DEF BoxMaterial Material {
     diffuseColor 1 0 0
      }
   }
   geometry Box { size 2 2 1 }
}

 

the createbox.java:

 

import java.applet.*;
import vrml.eai.field.*;
import vrml.eai.Node;
import vrml.eai.BrowserFactory;
import vrml.eai.Browser;

public class createbox extends Applet {

  
public void init() {
    
super.init();
  }


  
public void start() {
    Browser       browser  
= null;
    Node          rootnode 
= null;
    EventInMFNode children 
= null;

    
// Get the VRML Browser - try 10 times
    for (int count = 0; count < 10; count++{
      browser 
= BrowserFactory.getBrowser(this);
      
if (browser != nullbreak;
      
try { Thread.sleep(200); } catch (InterruptedException e) {}
    }

    
    
// Get the set_children eventIn of the DEF'd Group node
    try {
      rootnode 
= browser.getNode("NewNode");
      children 
= (EventInMFNode) rootnode.getEventIn("set_children");
    }
 catch (vrml.eai.InvalidNodeException ignored) {}

    
// Create a new blue Sphere Shape node
    String newvrml =
      
"#VRML V2.0 utf8 Shape { appearance Appearance { material " +
      
"Material { diffuseColor 0 0 1 } } geometry Sphere { radius 1 } }";
    
    
// Create and add the new node to the scene graph
    children.setValue( browser.createVrmlFromString( newvrml ) );
  }


}

This example was tested to run well in my computer.

Nasky

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章