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

  1. // <Tutorial Section=1.0 Title="Extended Applet">
  2. /**
  3. This applet illustrates the construction of a DxM model including: <BR>
  4. 1) importing an image and a sound <BR>
  5. 2) using the height and width of the applet for scaling the image to fit
  6.    properly <BR>
  7. 3) animating the image and looping the sound <BR>
  8. 4) setting the resulting image and sound to be presented <BR>
  9. 5) constructing a DxM applet including the proper steps for initialization
  10. <BR>
  11. **/
  12. // </Tutorial>
  13.  
  14.  
  15. // <Tutorial Section=1.1>
  16. import com.ms.dxmedia.*;        // direct animation libraries
  17. import java.awt.*;              // for getting the applet dimension
  18. import java.net.*;              // for URLs
  19.  
  20. // This class extends the DXMApplet class.  The model you set in this class,
  21. // by calling the setModel() method, is the model that will be displayed.
  22. public class ExtendedApplet extends DXMApplet {
  23.  
  24.   // The start() and stop() methods of DXMApplet restart and stop the
  25.   // DirectX Media animation loop already, so you don't need to implement
  26.   // these methods if you don't have other activities you need to stop and
  27.   // restart.
  28.  
  29.   // Set the model in the init() method.
  30.   public void init() {
  31.  
  32.     // Always call the super classes init first to ensure codeBase is set.
  33.     super.init() ;
  34.  
  35.     // Now set the model.
  36.     setModel(new ExtendedModel(this));
  37.   }
  38. }
  39.  
  40. // This class extends the Model class.  The createModel method in this class
  41. // is where you construct your animation.  This example will make use of
  42. // two of the primary media types supported by DirectAnimation: image and sound.
  43. class ExtendedModel extends Model {
  44.  
  45.   // Get the size of the applet in the constructor, and store it in the
  46.   // member variables.
  47.   ExtendedModel(DXMApplet dxma) {
  48.  
  49.     // Get the size of the applet.
  50.    Dimension dim = dxma.getSize();
  51.  
  52.     // The base unit of measure for DirectAnimation is the meter.
  53.     // Convert the size into meters by multiplying it with the pixelBvr.
  54.     _halfWidthNum = mul(toBvr(dim.width*0.5),pixelBvr);
  55.     _halfHeightNum = mul(toBvr(dim.height*0.5),pixelBvr);
  56.  
  57.     // Notice the use of the toBvr() call. This helper function is used
  58.     // extensively in DirectAnimation to convert Java primitive data types to 
  59.     // DxM behaviors. As you learn more about the system you will begin to appreciate the
  60.     // appreciate the power of behaviors, which can be time-varying values.
  61.   }
  62.  
  63.   // Create the animation in the createModel method.
  64.   public void createModel(BvrsToRun blist)
  65.   {
  66.     // Build up a URL to import relative to.
  67.     URL mediaBase = getImportBase();
  68.     URL imgBase = buildURL(mediaBase, "image/");
  69.     URL sndBase = buildURL(mediaBase, "sound/");
  70.  
  71.     // Create an image behavior by importing a bitmap.
  72.     ImageBvr img = importImage(buildURL(imgBase, "phantom.jpg"));
  73.  
  74.     // Create a sound behavior by importing a wave file.
  75.     SoundBvr snd = importSound(buildURL(sndBase, "earth.mp2"), null);
  76.  
  77.     // Find the length of the diagonal of the imported image, we'll use
  78.     // it later to scale the image to fit in the viewport.
  79.     // halfDiagonalNum = sqrt(xNum * xNum + yNum * yNum);
  80.     Point2Bvr maxPt2 = img.boundingBox().getMax();
  81.     NumberBvr xNum = maxPt2.getX();
  82.     NumberBvr yNum = maxPt2.getY();
  83.     NumberBvr halfDiagonalNum = sqrt(add(mul(xNum, xNum), mul(yNum, yNum)));
  84.  
  85.     // Find the smaller of the width and height.
  86.     NumberBvr minHalfExtNum = (NumberBvr)cond(lt(_halfWidthNum, _halfHeightNum),
  87.                                               _halfWidthNum, _halfHeightNum);
  88.  
  89.     // Create an image that fits in the viewport.
  90.     ImageBvr scaledImg = img.transform(scale2(div(minHalfExtNum,
  91.                                                   halfDiagonalNum)));
  92.  
  93.     // Create a rotating image
  94.     ImageBvr rotatingImg = scaledImg.transform(rotate(localTime));
  95.  
  96.     // Set the image that actually gets displayed using setImage()
  97.     setImage(overlay(rotatingImg, solidColorImage(black)));
  98.  
  99.     // Create a sound that loops continuously.
  100.     SoundBvr loopingSnd = snd.loop();
  101.  
  102.     // And set the sound that gets played using setSound()
  103.     setSound(loopingSnd);
  104.   }
  105.  
  106.   private NumberBvr _halfWidthNum, _halfHeightNum;
  107. }
  108.  
  109. // </Tutorial>
  110.