home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / dxma.exe / DXMA05.cab / samples / da / java / exercises / Switch / Switch.java < prev   
Encoding:
Java Source  |  1997-11-13  |  3.6 KB  |  123 lines

  1.  
  2. // Copyright (c) 1997 Microsoft Corporation
  3.  
  4. import java.awt.*;
  5. import java.applet.*;
  6. import java.net.*;
  7. import com.ms.dxmedia.*;
  8.  
  9. // Constructs a model where the image and the sound are switchers,
  10. // the model provides set methods that switches its constituent
  11. // sound and image into new values, upon external events.
  12. class SwitchModel extends Model {
  13.   public void createModel(BvrsToRun blst) {
  14.     // Build up a URL to import relative to.
  15.     URL mediaBase = getImportBase();
  16.     URL imgBase = buildURL(mediaBase, "image/");    
  17.     URL sndBase = buildURL(mediaBase, "sound/");    
  18.  
  19.     // Import the image and sound values.
  20.     _canyonImg = importImage(buildURL(imgBase, "foliageUtah.jpg"));
  21.     _kidsImg = importImage(buildURL(imgBase, "kidsUtah.jpg"));
  22.     _oceanImg = importImage(buildURL(imgBase, "hiddenBeachSG.jpg"));
  23.     _canyonSnd = importSound(buildURL(sndBase, "earth.mp2"), null).loop();
  24.     _kidsSnd = importSound(buildURL(sndBase, "torain.mp2"), null).loop();
  25.     _oceanSnd = importSound(buildURL(sndBase, "wave.mp2"), null).loop();
  26.  
  27.     // initialize the switchers, and extract their behaviors
  28.     _imgSw = new ModifiableBehavior(_canyonImg);
  29.     ImageBvr _img = (ImageBvr)_imgSw.getBvr();
  30.     _sndSw = new ModifiableBehavior(_canyonSnd);
  31.     SoundBvr _snd = (SoundBvr)_sndSw.getBvr();
  32.       
  33.     // set the image and sounds of the model to the switch behaviors
  34.     setImage(overlay(_img, solidColorImage(blue)));
  35.     setSound(_snd.gain(0.4));
  36.   }
  37.  
  38.   // these methods switch the behaviors to new values.
  39.   // the new values are started at the time of switching.
  40.   public void setCanyon() {
  41.     _imgSw.switchTo(_canyonImg);
  42.     _sndSw.switchTo(_canyonSnd);
  43.   }
  44.  
  45.   public void setKids() {
  46.     _imgSw.switchTo(_kidsImg);
  47.     _sndSw.switchTo(_kidsSnd);
  48.   }
  49.  
  50.   public void setOcean() {
  51.     _imgSw.switchTo(_oceanImg);
  52.     _sndSw.switchTo(_oceanSnd);
  53.   }
  54.  
  55.   // the switchers
  56.   private ModifiableBehavior _imgSw;
  57.   private ModifiableBehavior _sndSw;
  58.  
  59.   // the media values
  60.   private ImageBvr _canyonImg;
  61.   private ImageBvr _kidsImg;
  62.   private ImageBvr _oceanImg;
  63.   private SoundBvr _canyonSnd;
  64.   private SoundBvr _kidsSnd;
  65.   private SoundBvr _oceanSnd;
  66. }
  67.  
  68. // the applet is a DA canvas and a panel of buttons
  69. public class Switch extends Applet {
  70.   public void init() {
  71.     setLayout(new BorderLayout());
  72.     DaSwitch DaCv = new DaSwitch();
  73.     Panel ctrls = new AwtControls(DaCv._model);
  74.     add("Center", DaCv);
  75.     add("North", ctrls);
  76.   }
  77. }
  78.  
  79. // the DA canvas
  80. class DaSwitch extends DXMCanvas {
  81.   DaSwitch() {
  82.     _model = new SwitchModel();
  83.     setModel(_model);
  84.   }
  85.   SwitchModel _model;
  86. }
  87.  
  88. // a panel with three buttons,
  89. // it calls the model's corresponding set methods 
  90. // upon button events.
  91. class AwtControls extends Panel {
  92.  
  93.   // Place the scrollbar at the center, then two buttons on either side.
  94.   AwtControls(SwitchModel switchModel) {
  95.     _switchModel = switchModel;
  96.     setLayout(new FlowLayout());
  97.     Panel buttons = new Panel();
  98.     buttons.add(new Button("Canyon"));
  99.     buttons.add(new Button("Kids"));
  100.     buttons.add(new Button("Ocean"));
  101.     add(buttons);
  102.   }
  103.  
  104.   // The event handler when any buttons are pressed.
  105.   public boolean handleEvent(Event ev) {
  106.     switch (ev.id) {
  107.       case Event.ACTION_EVENT:
  108.         if (ev.arg == "Canyon") {
  109.           _switchModel.setCanyon();
  110.           return true;
  111.         } else if (ev.arg == "Kids") {
  112.           _switchModel.setKids();
  113.           return true;
  114.         } else if (ev.arg == "Ocean") {
  115.           _switchModel.setOcean();
  116.           return true;
  117.         }
  118.     }
  119.     return false;
  120.   }
  121.   SwitchModel _switchModel;
  122. }
  123.