<HTML>
<HEAD>
<TITLE>DirectX Media SDK DirectAnimation Tutorial</TITLE>
<LINK REL=STYLESHEET HREF="../../../../../webcd/da/tutorial.css">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=iso8859-1">
<META NAME="MS.LOCALE" CONTENT="EN-US">
<META NAME="ROBOTS" CONTENT="noindex">
</HEAD>
<BODY BGCOLOR="WHITE" LINK=#0033CC TOPMARGIN=0 LEFTMARGIN=20>
<!--TOOLBAR_START-->
<!--TOOLBAR_EXEMPT-->
<!--TOOLBAR_END-->

<DIV CLASS="flush">
<TABLE WIDTH=100% BORDER=0 BGCOLOR=#000000>
<TR ALIGN=RIGHT><TD VALIGN=TOP><FONT CLASS=desc1 SIZE=2>
<IMG SRC="../../../../../media/image/d_prev.gif"
 WIDTH=71 HEIGHT=16 BORDER=0 ALT="Previous Topic">
<A HREF="../../../../../webcd/da/tutorial_main.htm" TARGET="_top">
<IMG SRC="../../../../../media/image/d_home.gif" WIDTH=48 HEIGHT=16 BORDER=0 ALT="Tutorial Home Page"></A>
<IMG SRC="../../../../../media/image/d_next.gif" WIDTH=48 HEIGHT=16 BORDER=0 ALT="Next Topic">
</FONT></TD><TD WIDTH=30></TD></TR></DIV></TABLE>
<FONT CLASS=title><CENTER>Extended Applet</CENTER></FONT>
<BR>
<FONT CLASS=desc1>
This applet illustrates the construction of a DxM model including: <BR>
1) importing an image and a sound <BR>
2) using the height and width of the applet for scaling the image to fit
   properly <BR>
3) animating the image and looping the sound <BR>
4) setting the resulting image and sound to be presented <BR>
5) constructing a DxM applet including the proper steps for initialization
<BR>
</FONT><BR>

<TABLE WIDTH=100%><TR><TD WIDTH=5%><TD><FONT CLASS=code>

import com.ms.dxmedia.*;        </FONT><FONT CLASS=desc2> direct animation libraries</FONT><FONT CLASS=code><BR>
import java.awt.*;              </FONT><FONT CLASS=desc2> for getting the applet dimension</FONT><FONT CLASS=code><BR>
import java.net.*;              </FONT><FONT CLASS=desc2> for URLs</FONT><FONT CLASS=code><BR>
</FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> This class extends the DXMApplet class.  The model you set in this class,
 by calling the setModel() method, is the model that will be displayed.</TD></TR></FONT></TABLE>
<FONT CLASS=code>public class ExtendedApplet extends DXMApplet {<BR>
</FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> The start() and stop() methods of DXMApplet restart and stop the
 DirectX Media animation loop already, so you don't need to implement
 these methods if you don't have other activities you need to stop and
 restart.</TD></TR></FONT></TABLE>
<FONT CLASS=code></FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> Set the model in the init() method.</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.125in">public void init() {<BR>
</DIV></FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> Always call the super classes init first to ensure codeBase is set.</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.25in">super.init() ;<BR>
</DIV></FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> Now set the model.</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.25in">setModel(new ExtendedModel(this));<BR>
</DIV><DIV STYLE="margin-left: 0.125in">}<BR>
</DIV>}<BR>
</FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> This class extends the Model class.  The createModel method in this class
 is where you construct your animation.  This example will make use of
 two of the primary media types supported by DirectAnimation: image and sound.</TD></TR></FONT></TABLE>
<FONT CLASS=code>class ExtendedModel extends Model {<BR>
</FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> Get the size of the applet in the constructor, and store it in the
 member variables.</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.125in">ExtendedModel(DXMApplet dxma) {<BR>
</DIV></FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> Get the size of the applet.</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.1875in">Dimension dim = dxma.getSize();<BR>
</DIV></FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> The base unit of measure for DirectAnimation is the meter.
 Convert the size into meters by multiplying it with the pixelBvr.</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.25in">_halfWidthNum = mul(toBvr(dim.width*0.5),pixelBvr);<BR>
_halfHeightNum = mul(toBvr(dim.height*0.5),pixelBvr);<BR>
</DIV></FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> Notice the use of the toBvr() call. This helper function is used
 extensively in DirectAnimation to convert Java primitive data types to
 DxM behaviors. As you learn more about the system you will begin to appreciate the
 appreciate the power of behaviors, which can be time-varying values.</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.125in">}<BR>
</DIV></FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> Create the animation in the createModel method.</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.125in">public void createModel(BvrsToRun blist)<BR>
{</FONT>
<BR>
</DIV><TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> Build up a URL to import relative to.</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.25in">URL mediaBase = getImportBase();<BR>
URL imgBase = buildURL(mediaBase, "image/");<BR>
URL sndBase = buildURL(mediaBase, "sound/");<BR>
</DIV></FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> Create an image behavior by importing a bitmap.</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.25in">ImageBvr img = importImage(buildURL(imgBase, "phantom.jpg"));<BR>
</DIV></FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> Create a sound behavior by importing a wave file.</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.25in">SoundBvr snd = importSound(buildURL(sndBase, "earth.mp2"), null);<BR>
</DIV></FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> Find the length of the diagonal of the imported image, we'll use
 it later to scale the image to fit in the viewport.
 halfDiagonalNum = sqrt(xNum * xNum + yNum * yNum);</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.25in">Point2Bvr maxPt2 = img.boundingBox().getMax();<BR>
NumberBvr xNum = maxPt2.getX();<BR>
NumberBvr yNum = maxPt2.getY();<BR>
NumberBvr halfDiagonalNum = sqrt(add(mul(xNum, xNum), mul(yNum, yNum)));<BR>
</DIV></FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> Find the smaller of the width and height.</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.25in">NumberBvr minHalfExtNum = (NumberBvr)cond(lt(_halfWidthNum, _halfHeightNum),<BR>
</DIV><DIV STYLE="margin-left: 2.875in">_halfWidthNum, _halfHeightNum);<BR>
</DIV></FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> Create an image that fits in the viewport.</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.25in">ImageBvr scaledImg = img.transform(scale2(div(minHalfExtNum,<BR>
</DIV><DIV STYLE="margin-left: 3.125in">halfDiagonalNum)));<BR>
</DIV></FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> Create a rotating image</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.25in">ImageBvr rotatingImg = scaledImg.transform(rotate(localTime));<BR>
</DIV></FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> Set the image that actually gets displayed using setImage()</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.25in">setImage(overlay(rotatingImg, solidColorImage(black)));<BR>
</DIV></FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> Create a sound that loops continuously.</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.25in">SoundBvr loopingSnd = snd.loop();<BR>
</DIV></FONT>
<BR>
<TABLE><TR><TD WIDTH=500><FONT CLASS=desc1> And set the sound that gets played using setSound()</TD></TR></FONT></TABLE>
<FONT CLASS=code><DIV STYLE="margin-left: 0.25in">setSound(loopingSnd);<BR>
</DIV><DIV STYLE="margin-left: 0.125in">}<BR>
</DIV><BR>
<DIV STYLE="margin-left: 0.125in">private NumberBvr _halfWidthNum, _halfHeightNum;<BR>
</DIV>}<BR>
<BR>
</TABLE><BR>
</FONT><FONT CLASS=link><A ID=cpslug HREF="../../../../../help/da/dalegal.htm" TARGET="_top">&#169; 1997 Microsoft Corporation. All rights reserved. Terms of Use.</A></FONT>
</TABLE>
<BR>
<BR>
<DIV CLASS="flush">
<TABLE WIDTH=100% BORDER=0 BGCOLOR=#000000>
<TR ALIGN=RIGHT><TD VALIGN=TOP><FONT CLASS=desc1 SIZE=2>
<IMG SRC="../../../../../media/image/d_prev.gif"
 WIDTH=71 HEIGHT=16 BORDER=0 ALT="Previous Topic">
<A HREF="../../../../../webcd/da/tutorial_main.htm" TARGET="_top">
<IMG SRC="../../../../../media/image/d_home.gif" WIDTH=48 HEIGHT=16 BORDER=0 ALT="Tutorial Home Page"></A>
<IMG SRC="../../../../../media/image/d_next.gif" WIDTH=48 HEIGHT=16 BORDER=0 ALT="Next Topic">
</FONT></TD><TD WIDTH=30></TD></TR></DIV>

</BODY>
</HTML>
