HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Java & VRML"
Vol. 2, Issue 11, p. 26

	

Listing 1. 

#VRML V2.0 utf8 
DEF Main Transform { 

      translation 0 0 0  
      rotation 5 3 2 1  
      scale 1 1 1 
       
      children [       
       
            Shape {          
               appearance Appearance { 
                  texture ImageTexture { 
                     url "avi.jpg" 
                     repeatS FALSE 
                     repeatT TRUE 
                                 } 
               material Material {    
                     diffuseColor 0.8 0.2 0.2 
                     shininess .99    
                                } 
                                } 
               geometry Box { 
                          size 2 2 2 
                            } 
            }, 
      DEF TOUCH TouchSensor {      } 
      ] 
      } 

Listing 2. 

 public class AVIAplt extends Applet implements EventOutObserver { 
  TextArea output = null; 
  Browser browser = null; 
           
  Node Main = null; 
  EventInSFRotation BoxRotation = null; 
  EventOutSFRotation outputRotation = null; 
  EventInSFVec3f ScaleIn = null; 
  EventOutSFVec3f ScaleOut = null; 
  EventOutSFTime touchTime = null; 
  boolean error = false; 
  java.awt.Scrollbar horizontalScrollbar1; 
  java.awt.CheckboxGroup checkboxGroup1; 
  java.awt.Label label1; 
  java.awt.Label label2; 
  

Listing 3. 

      // Get the Touch Sensor 
      Node sensor = browser.getNode("TOUCH"); 
      // Get its touchTime EventOut 
      touchTime = (EventOutSFTime) sensor.getEventOut("touchTime"); 
      // Set up the callback 
      touchTime.advise(this, new Integer(1)); 
      // Get its BoxRotation EventOut 
outputRotation = (EventOutSFRotation) Main.getEventOut("rotation"); 
      // Set up its callback 
      outputRotation.advise(this, new Integer(2)); 
    } 

  
public void init() { 

Listing 4. 

setLayout(new GridLayout(7,1)); 
addNotify(); 
     
//The Scrollbar 
horizontalScrollbar1 = new     
java.awt.Scrollbar(Scrollbar.HORIZONTAL,5,1,1,10); 
label1 = new java.awt.Label("Rotation Angle",Label.CENTER); 
label1.setFont(new Font("TimesRoman", Font.BOLD,14));  
add(label1); 
add(horizontalScrollbar1); 

//The CheckBoxes 
label2 = new java.awt.Label("This changes the Scale",Label.CENTER); 
label2.setFont(new Font("TimesRoman", Font.BOLD,14)); 
add(label2); 
checkboxGroup1 = new java.awt.CheckboxGroup(); 
add(new Checkbox("Big", checkboxGroup1, false)); 
add(new Checkbox("Medium", checkboxGroup1, true)); 
add(new Checkbox("Small", checkboxGroup1, false)); 

output = new TextArea(5, 40); 
add(output); 

Listing 5. 

// Now we've got the handle to the VRML Browser. 
try { 
  // Get the Main node... 
  Main = browser.getNode("Main"); 
  // Get the BoxRotation EventIn 
  BoxRotation = (EventInSFRotation) Main.getEventIn("set_rotation"); 
  // Get the Touch Sensor 
  Node sensor = browser.getNode("TOUCH"); 
  // Get its touchTime EventOut 
  touchTime = (EventOutSFTime) sensor.getEventOut("touchTime"); 
  // Set up the callback 
  touchTime.advise(this, new Integer(1)); 
  // Get its BoxRotation EventOut 
  outputRotation = (EventOutSFRotation) Main.getEventOut("rotation"); 
  // Set up its callback 
  outputRotation.advise(this, new Integer(2)); 
} 
catch (InvalidNodeException ne) { 
  add(new TextField("Failed to get node:" + ne)); 
  error = true; 
} 
catch (InvalidEventInException ee) { 
  add(new TextField("Failed to get EventIn:" + ee)); 
  error = true; 
} 
catch (InvalidEventOutException ee) { 
  add(new TextField("Failed to get EventOut:" + ee)); 
  error = true; 
}
 
Listing 6. 

public void callback(EventOut who, double when, Object which) { 
    Integer whichNum = (Integer) which; 
    if (whichNum.intValue() == 1) { 
      // Make the timestamp and new time show up in the textarea. 
      double val = touchTime.getValue(); 
      output.appendText("Got time " + val + " at time " + when + "\n"); 
    } 
    if (whichNum.intValue() == 2) { 
      // Make the new rotation of the box and timestamp 
      // show up in the textarea. 
      float[] val = outputRotation.getValue(); 
      output.appendText("Got rotation value " + val[0] + ", " + val[1] 
         + ", " + val[2] + "," +  val[3] + " at time " + when + "\n"); 
    } 

Listing 7. 

public boolean handleEvent(Event event) 
{ 
    if (error) 
      { 
   showStatus("Problems! Had an error during initialization"); 
   return true;  // Uh oh... 
      } 
    if (event.target instanceof Checkbox) 
   { 
         Checkbox b = (Checkbox) event.target; 
         if (b.getLabel() == "Small" && b.getState() == true) 
         {   
            showStatus("Small!"); 
            float[] val = new float[3]; 
            val[0] = .5f; 
            val[1] = .5f; 
            val[2] = .5f; 
            ScaleIn.setValue(val); 
            return true; 
         } 
         if (b.getLabel() == "Big" && b.getState() == true) 
         {   
            showStatus("Big!"); 
            float[] val = new float[3]; 
            val[0] = 3; 
            val[1] = 3; 
            val[2] = 3; 
            ScaleIn.setValue(val); 
            return true;           
         } 
         if (b.getLabel() == "Medium" && b.getState() == true) 
         {   
            showStatus("Medium!"); 
            float[] val = new float[3]; 
            val[0] = 1; 
            val[1] = 1; 
            val[2] = 1; 
            ScaleIn.setValue(val); 
            return true; 
         } 
       } 



 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.