www.pudn.com > vrmlExample.rar > Create.java


 
import java.util.*; 
import vrml.*; 
import vrml.node.*; 
import vrml.field.*; 
 
public class Create extends Script 
{ 
    MFNode addSphere; 
    float x = 0.0f; 
     
    public void initialize() 
    { 
        addSphere = (MFNode)getEventOut("addSphere"); 
    } 
     
    public void processEvent(Event e) 
    { 
        if(e.getName().equals("touched") == true) 
        { 
            String part1 = 
                "Transform {" + 
                "    translation "; 
            String part2 = 
                "        0.0 0.0 " + 
                "    children[" + 
                "    Shape{geometry Sphere{}" + 
                "          appearance Appearance{" + 
                "              material Material{diffuseColor 1 0 0"+ 
                "              }" + 
                "          }" + 
                "    }" + 
                "    ]" + 
                "}"; 
 
            Browser browser = getBrowser(); 
            BaseNode baseNodes[]; 
 
            x += 3.0f; 
 
            try{ 
                baseNodes = browser.createVrmlFromString(part1 + x +part2); 
                if(null != baseNodes)  
                { 
                    addSphere.setValue(baseNodes); 
                } 
            } catch (Exception exp) { 
                exp.printStackTrace() ; 
            } 
        } 
    } 
}