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

  1. // <Tutorial Section=1.0 Title="Button that Reacts to a Mouse">
  2. //
  3. /** This applet shows a black button that starts to change colors with time<BR>
  4. when the user's mouse moves over it. When the user clicks on the button with<BR>
  5. the left mouse button, the applet takes a snapshot of the time-varying color.<BR>
  6. When the user moves the mouse away from the button, its color returns to black.
  7.  
  8. This applet illustrates the following:<BR><BR>
  9. - creating a solid-colored square.<BR>
  10. - making that square react to mouse over and left mouse button clicks.<BR>
  11. <BR>
  12. **/
  13. import com.ms.dxmedia.*;    // All DirectAnimation classes
  14.  
  15. // ButtonPick is an applet that invokes ButtonPickModel which constructs a
  16. // solid-colored square.
  17. public class ButtonPick extends DXMApplet {
  18.   public void init() {
  19.     super.init() ;
  20.     setModel (new ButtonPickModel());
  21.   }
  22. }
  23.  
  24. // In the ButtonPickModel class the createModel method is where you construct 
  25. // your animation.
  26. class ButtonPickModel extends Model {
  27.  
  28.   public void createModel(BvrsToRun blist)
  29.   {
  30.     // Create a detectable image and crop it to 100 by 100 pixels.
  31.     ImageBvr detectImg = detectableEmptyImage.
  32.       crop(point2(mul(toBvr(-50),pixelBvr), mul(toBvr(-50),pixelBvr)),
  33.         point2(mul(toBvr(50),pixelBvr), mul(toBvr(50),pixelBvr)));
  34.  
  35.     // Make the above created image pickable.
  36.     PickableImage pickPImg = new PickableImage(detectImg);
  37.  
  38.     // Create an event that will trigger when the mouse moves over detectImg.
  39.     DXMEvent pickEv = pickPImg.getPickEvent();
  40.  
  41.     // Create an event that will trigger when both the mouse moves over 
  42.     // detectImg, and the left mouse button is clicked.
  43.     DXMEvent clickEv = andEvent(leftButtonDown, pickEv);
  44.  
  45.     // Create a color behavior that changes its hue with time.
  46.     ColorBvr changeClr = colorHsl(add(mul(sin(
  47.       localTime),toBvr(0.5)),toBvr(0.5)),
  48.         toBvr(0.5),toBvr(0.5));
  49.  
  50.     // Create a color behavior that starts out as black, changes to changeClr 
  51.     // when the mouse moves over detectImg (pickEv), and returns to black 
  52.     // after the mouse is no longer over detectImg (notEvent(pickEv)).
  53.     ColorBvr overClr = ColorBvr.newUninitBvr();
  54.     overClr.init(until(
  55.       black,pickEv,until(changeClr,notEvent(pickEv),overClr)));
  56.  
  57.     // Create an event that takes a snapshot of overClr when detectImg is
  58.     // clicked with the left mouse button.
  59.     DXMEvent snapEv = clickEv.snapshotEvent(overClr);
  60.  
  61.     // Create two color behaviors.  One (snapClr) that starts out as overClr,
  62.     // and changes to whatever color overClr is when detectImg is clicked 
  63.     // with the left mouse button. The second (totalClr) starts out as 
  64.     // snapClr, and changes to black when the mouse is no longer over 
  65.     // detectImg.
  66.     snapClr = (ColorBvr)untilNotify(overClr,snapEv,
  67.       new ButtonPickSnapshot());
  68.  
  69.     ColorBvr totalClr = ColorBvr.newUninitBvr();
  70.     totalClr.init(until(
  71.       snapClr,notEvent(pickEv),totalClr));
  72.  
  73.     // Create a solid color image, crop it to 100 by 100 pixels,
  74.     // and apply the color behavior of totalClr to it.
  75.     ImageBvr initImg = solidColorImage(totalClr).
  76.       crop(point2(mul(toBvr(-50),pixelBvr), mul(toBvr(-50),pixelBvr)),
  77.         point2(mul(toBvr(50),pixelBvr), mul(toBvr(50),pixelBvr)));
  78.  
  79.     // Put the pickable Image on top of initImg.
  80.     ImageBvr finalImg = overlay(pickPImg.getImageBvr(),initImg);
  81.     
  82.     //Display the image on a blue background.
  83.     setImage(overlay(finalImg, solidColorImage(blue)));  
  84.   }
  85.   public void cleanup() {
  86.     super.cleanup();
  87.     snapClr = null;
  88.   }
  89.   ColorBvr snapClr;
  90. }
  91.  
  92. class ButtonPickSnapshot extends Statics implements UntilNotifier {
  93.   
  94.   public Behavior notify(Object eventData,
  95.     Behavior currentRunningBvr, BvrsToRun btr) {   
  96.  
  97.     // Returns the color of overClr when the left mouse button is clicked
  98.     // on detectImg.
  99.     return (ColorBvr)eventData;
  100.   }
  101. }
  102.  
  103. // Thats all there is to it.  Happy animating...
  104. // </Tutorial>
  105.