home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / dxma.exe / DXMA05.cab / samples / da / java / templates / GeometryDrag / GeometryDrag.java next >
Encoding:
Java Source  |  1997-11-13  |  3.0 KB  |  82 lines

  1. // <Tutorial Section=1.0 Title="Draggable Geometry">
  2. //
  3. /** This applet shows a red cube that can be dragged around the applet.<BR>
  4. Dragging actually consists of three parts: grabbing, dragging and releasing.<BR>
  5. Grabbing occurs when the left mouse button is pressed and held down over the<BR>
  6. red cube. Dragging happens when the mouse moves within the applet. During<BR>
  7. this process, the color of the cube changes to blue.  Releasing happens when<BR>
  8. the left mouse button is released, and the cube is dropped in that position,<BR>
  9. with its color returning to red.<BR>
  10.  
  11. It illustrates the following:<BR><BR>
  12. - making a imported cube draggable.<BR>
  13. - getting events that trigger when the cube is grabbed and released.<BR>
  14. - changing the color of the cube when it is being dragged.<BR>
  15. - returning the cube to its original color when it is released.<BR>
  16. **/
  17.  
  18.  
  19.  
  20. import com.ms.dxmedia.*; // All DirectAnimation classes
  21. import java_utility.*;  // the DraggableImage class
  22. import java.net.*; // the Java URL classes
  23.  
  24. public class GeometryDrag extends DXMApplet {
  25.   public void init() {
  26.     super.init() ;
  27.     setModel (new GeometryPickTestModel());
  28.   }
  29. }
  30.  
  31. // In the GeometryPickTestModel class the createModel method is where you construct 
  32. // your animation.
  33. class GeometryPickTestModel extends Model {
  34.   public void createModel(BvrsToRun listBvrs) {
  35.  
  36.         // Import the cube rotate it and scale it down.
  37.     URL mediaBase = getImportBase();
  38.     URL geoBase = buildURL(mediaBase,"geometry/");
  39.     GeometryBvr geo = importGeometry(buildURL(geoBase,"cube.x"));    
  40.     geo = geo.transform(compose(
  41.             rotate(vector3(1,1,1), localTime),
  42.             scale3(1*cm) ));
  43.         
  44.     // Make geo draggable by creating a DraggableGeometry class
  45.     // object (grabGeo). 
  46.     grabGeo = new DraggableGeometry(geo, origin3);
  47.  
  48.     // Initialize clr.  Let it start out as red, change it to blue,
  49.     // when the cube is grabbed, and return to red when the cube
  50.     // is released.  The grab and release events are obtained from the
  51.         // getGrabEvent() and getReleaseEvent() methods of the DraggableGeometry
  52.         // class respectively.
  53.       ColorBvr clr = ColorBvr.newUninitBvr();
  54.       clr.init(until(red, grabGeo.getGrabEvent(), 
  55.             until(blue, grabGeo.getReleaseEvent(), clr)));
  56.  
  57.         // Get the GeometryBvr part of grabGeo, by calling the getGeometryBvr() 
  58.         // method of DraggableGeometry.
  59.     GeometryBvr pickableGeo = grabGeo.getGeometryBvr();  
  60.         
  61.     // Apply clr to pickableGeo.   
  62.     pickableGeo = pickableGeo.diffuseColor(clr);
  63.  
  64.  
  65.     pickableGeo = union(pickableGeo, directionalLight);
  66.         
  67.     CameraBvr cam = perspectiveCamera(toBvr(10*cm),toBvr(5*cm));
  68.         
  69.         // overlay the rendered on a black background.
  70.     setImage(overlay(pickableGeo.render(cam), solidColorImage(black)));                
  71.   }
  72.   public void cleanup() {
  73.     super.cleanup();
  74.     grabGeo = null;
  75.   }
  76.   DraggableGeometry grabGeo;
  77. }
  78.  
  79. // That's all there is to it.  Happy animating...
  80. // </Tutorial>
  81.  
  82.