This applet shows a red cube that can be dragged around the applet.
Dragging actually consists of three parts: grabbing, dragging and releasing.
Grabbing occurs when the left mouse button is pressed and held down over the
red cube. Dragging happens when the mouse moves within the applet. During
this process, the color of the cube changes to blue. Releasing happens when
the left mouse button is released, and the cube is dropped in that position,
with its color returning to red.
It illustrates the following:
- making a imported cube draggable.
- getting events that trigger when the cube is grabbed and released.
- changing the color of the cube when it is being dragged.
- returning the cube to its original color when it is released.
import com.ms.dxmedia.*; All DirectAnimation classes
import utility.*; the DraggableImage class
import java.net.*; the Java URL classes
public class GeometryDrag extends DXMApplet {
public void init() {
super.init() ;
setModel (new GeometryPickTestModel());
}
}
In the GeometryPickTestModel class the createModel method is where you construct
your animation. |
class GeometryPickTestModel extends Model {
public void createModel(BvrsToRun listBvrs) {
Import the cube rotate it and scale it down. |
URL mediaBase = getImportBase();
URL geoBase = buildURL(mediaBase,"geometry/");
GeometryBvr geo = importGeometry(buildURL(geoBase,"cube.x"));
geo = geo.transform(compose(
rotate(vector3(1,1,1), localTime),
scale3(1*cm) ));
Make geo draggable by creating a DraggableGeometry class
object (grabGeo). |
DraggableGeometry grabGeo = new DraggableGeometry(geo, origin3);
Initialize clr. Let it start out as red, change it to blue,
when the cube is grabbed, and return to red when the cube
is released. The grab and release events are obtained from the
getGrabEvent() and getReleaseEvent() methods of the DraggableGeometry
class respectively. |
ColorBvr clr = ColorBvr.newUninitBvr();
clr.init(until(red, grabGeo.getGrabEvent(),
until(blue, grabGeo.getReleaseEvent(), clr)));
Get the GeometryBvr part of grabGeo, by calling the getGeometryBvr()
method of DraggableGeometry. |
GeometryBvr pickableGeo = grabGeo.getGeometryBvr();
Apply clr to pickableGeo. |
pickableGeo = pickableGeo.diffuseColor(clr);
pickableGeo = union(pickableGeo, directionalLight);
CameraBvr cam = perspectiveCamera(toBvr(10*cm),toBvr(5*cm));
overlay the rendered on a black background. |
setImage(overlay(pickableGeo.render(cam), solidColorImage(black)));
}
}
That's all there is to it. Happy animating... |