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

  1. // <Tutorial Section=1.0 Title="Draggable Image">
  2. //
  3. /** This applet shows a red square 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 square. Dragging happens when the mouse moves within the applet. During<BR>
  7. this process, the color of the square changes to blue.  Releasing happens when<BR>
  8. the left mouse button is released, and the square 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 square image draggable.<BR>
  13. - getting events that trigger when the image is grabbed and released.<BR>
  14. - changing the color of the square when it is being dragged.<BR>
  15. - returning the square to its original color when it is released.<BR>
  16. **/
  17.  
  18.  
  19. import com.ms.dxmedia.*;    // All DirectAnimation classes
  20. import java_utility.*;           // the DraggableImage class
  21.  
  22. public class ImageDrag extends DXMApplet  {
  23.   public void init() {
  24.     super.init() ;
  25.     setModel (new ImagePickTestModel());
  26.   }
  27. }
  28.  
  29. // In the ImagePickTestModel class the createModel method is where you construct 
  30. // your animation.
  31. class ImagePickTestModel extends Model  {
  32.   public void createModel(BvrsToRun listBvrs)  {
  33.  
  34.     // Create an uninitialized ColorBvr (clr).
  35.     ColorBvr clr = ColorBvr.newUninitBvr();
  36.  
  37.     // Create cropped square, and apply clr's color behavior to it. 
  38.     ImageBvr blockImg = solidColorImage(clr).
  39.       crop(point2(toBvr(0), toBvr(0)),
  40.         point2(toBvr(0.005), toBvr(0.005)));
  41.  
  42.     // Make blockImg draggable by creating a DraggableImage class
  43.     // object (grabImg). 
  44.     DraggableImage grabImg = new DraggableImage(blockImg, origin2);
  45.     
  46.     // Initialize clr.  Let it start out as red, change it to blue,
  47.     // when the square is grabbed, and return to red when the square
  48.     // is released.  The grab and release events are obtained from the
  49.         // getGrabEvent() and getReleaseEvent() methods of the DraggableImage
  50.         // class respectively.
  51.     clr.init(until(red, grabImg.getGrabEvent(), 
  52.       until(blue, grabImg.getReleaseEvent(), clr)));
  53.  
  54.     // Get the ImageBvr part of grabImg, by calling the getImageBvr() method
  55.     // of DraggableImage.
  56.     ImageBvr pickableBlockImg = grabImg.getImageBvr();
  57.  
  58.     // overlay pickableBlockImg on a black background.
  59.     setImage(overlay(pickableBlockImg, solidColorImage(black)));
  60.   }
  61. }
  62.  
  63. // That's all there is to it.  Happy animating...
  64. // </Tutorial>
  65.