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

  1. // <Tutorial Section=1.0 Title="Using DirectAnimation to Jump to another Website">
  2. //
  3. // There are two main pieces to any DirectAnimation applet, the DXMApplet 
  4. // class and the Model class. To use the DirectAnimation classes, import 
  5. // the media libraries like this:
  6. import com.ms.dxmedia.*;
  7. import java.awt.*;
  8. import java.net.*;     // gets URL.
  9. import java.applet.*;  // gets getAppletContext.
  10.  
  11. public class JumpURL extends DXMApplet  {
  12.  
  13.   public void init()  {
  14.     super.init() ;
  15.     JumpURLModel newJump = new JumpURLModel();
  16.     setModel(newJump);
  17.         // Call AppletContext() and set _getAppContext to its return.  
  18.         // _getAppContext will be used to call showDocument().
  19.     newJump.appLink(getAppletContext());
  20.   }
  21. }
  22.  
  23. // Then create a class that extends the Model class.  In this class the 
  24. // createModel method is where you construct your animation.  This example 
  25. // will make use of two of the primary media types supported by 
  26. // DirectAnimation: images and sound.
  27. class JumpURLModel extends Model  {
  28.  
  29.   public void createModel(BvrsToRun blist)  {
  30.     // Create bases for importing images and sounds.
  31.     URL mediaBase = getImportBase();
  32.     URL imgBase = buildURL(mediaBase,"image/");
  33.     URL sndBase = buildURL(mediaBase,"sound/");
  34.  
  35.     // Import seattle.jpg.
  36.     ImageBvr seattleImg = importImage(buildURL(imgBase,"seattle.jpg"));
  37.  
  38.     // Import the sound that will be played when the mouse moves over 
  39.     // seattleImg.
  40.     SoundBvr pickSnd = importSound(buildURL(sndBase,
  41.       "etherial.mp2"),null).loop();
  42.   
  43.     // Make seattleImg pickable.
  44.     PickableImage pickPImg = new PickableImage(seattleImg,true);
  45.  
  46.     // Create an event that will trigger when the mouse moves over detectImg.
  47.     DXMEvent pickEv = pickPImg.getPickEvent();
  48.  
  49.     // Get the image part of pickPImg.
  50.     ImageBvr pickImg = pickPImg.getImageBvr();
  51.  
  52.     // Create a string that contains the URL to Seattle Sidewalk.
  53.     StringBvr urlStr = toBvr("http://seattle.sidewalk.com");
  54.  
  55.     // Create the tip box (tipImg), which consists of a black 
  56.     // backgroud (tipBackIm), and white text (tipTxtIm).
  57.     FontStyleBvr font = font("Arial",12,white).bold();
  58.     ImageBvr tipTxtImg = stringImage(
  59.       "Click the red dot to go to Seattle Sidewalk", font);
  60.  
  61.     Bbox2Bvr txtBB = tipTxtImg.boundingBox();
  62.  
  63.     ImageBvr tipBackImg = solidColorImage(black)
  64.       .crop(txtBB.getMin(),txtBB.getMax());
  65.         
  66.     ImageBvr tipImg = overlay(tipTxtImg,tipBackImg);
  67.  
  68.     // Create a detectable image (jumpImg)and crop it to 6 by 6 mm.
  69.     // This image will be made pickable and used to jump to Seattle Sidewalk.
  70.     ImageBvr jumpImg = solidColorImage(red).crop(point2(-0.003,-0.003),
  71.       point2(0.003,0.003));
  72.  
  73.         jumpImg = jumpImg.transform(translate(toBvr(0),
  74.             mul(toBvr(-50),pixelBvr)));
  75.  
  76.         // Make detectImg pickable.
  77.     PickableImage jumpPImg = new PickableImage(jumpImg,true);
  78.  
  79.     // Create an event that will trigger when the mouse moves over jumpImg.
  80.     DXMEvent jumpEv = jumpPImg.getPickEvent();
  81.  
  82.     // Create an event that will trigger when both the mouse moves over 
  83.     // jumpImg, and the left mouse button is clicked.
  84.     DXMEvent jumpClickEv = andEvent(leftButtonDown, jumpEv);
  85.  
  86.     // Get the image part of jumpPImg.
  87.     ImageBvr jumpPointImg = jumpPImg.getImageBvr();
  88.  
  89.     // Combine the pickable images with the tip box and pair them
  90.     // with sound.
  91.     TupleBvr compTup1 = pairBvr(pickPImg.getImageBvr(),silence);
  92.     ImageBvr finalImg = overlay(jumpPointImg,overlay(tipImg,pickImg));
  93.     TupleBvr compTup2 = pairBvr(finalImg,pickSnd);
  94.  
  95.     // Create a behavior (pickTup) that start out as a detectable empty
  96.     // image with no sound.  When the mouse moves over it, the sound
  97.     // changes to pickSnd, and the tip box is displayed.  When the mouse is
  98.     // no longer over it, it returns to the original behavior.
  99.     TupleBvr pickedTup = TupleBvr.newUninitBvr(pairBvr(emptyImage,silence));
  100.     pickedTup.init(until(compTup1,pickEv,until(compTup2,
  101.       notEvent(pickEv),pickedTup)));
  102.  
  103.     // Create a behavior that starts out as pickedTup, and that jumps to the
  104.     // Seattle Sidewalk site when the user clicks on pickPImg using the left
  105.     // mouse button.
  106.     TupleBvr finalTup = (TupleBvr)untilNotify(pickedTup,
  107.       jumpClickEv,new JumpURLLink(urlStr));
  108.  
  109.     // Display the final image.
  110.     setImage(overlay((ImageBvr)finalTup.nth(0),
  111.       overlay(seattleImg,solidColorImage(black))));
  112.  
  113.     // Start the sound.
  114.     setSound(mix((SoundBvr)finalTup.nth(1),silence));  
  115.   }
  116.  
  117.   public void appLink(AppletContext appGet)  {
  118.     _getAppContext = appGet;
  119.   }
  120.   public static AppletContext _getAppContext;
  121. }
  122.  
  123. class JumpURLLink extends Statics implements UntilNotifier  {
  124.  
  125.   public JumpURLLink(StringBvr url)  {
  126.         // Convert urlStr into an URL.
  127.     try  {
  128.       _clickDest = new URL((String) url.extract());
  129.     }
  130.     catch(MalformedURLException mal)  {
  131.       System.out.println("Malformed URL: Check Applet Tag.");
  132.     }
  133.   }
  134.  
  135.   public Behavior notify(Object eventData,
  136.     Behavior currentRunningBvr, BvrsToRun btr)  { 
  137.     // Use the Java showDocument method to load the Seattle Sidewalk page
  138.     // in the parent frame.
  139.     JumpURLModel._getAppContext.showDocument(_clickDest,"_parent");
  140.  
  141.     // Return currentRunningBvr to keep the UntilNotify happy.
  142.     return currentRunningBvr;
  143.   }
  144.   URL _clickDest;
  145. }
  146. // Thats all there is to it.  Happy animating...
  147. // </Tutorial>