<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wikidot="http://www.wikidot.com/rss-namespace">

	<channel>
		<title>Elisha Peterson&#039;s Programming and Technology Notes</title>
		<link>http://elishapeterson.wikidot.com</link>
		<description></description>
				<copyright></copyright>
		<lastBuildDate></lastBuildDate>
		
					<item>
				<guid>http://elishapeterson.wikidot.com/technotes:dynamic-latex-with-codecogs</guid>
				<title>Dynamic LaTeX with CodeCogs</title>
				<link>http://elishapeterson.wikidot.com/technotes:dynamic-latex-with-codecogs</link>
				<description>

&lt;p&gt;Elisha Peterson, United States Military Academy&lt;/p&gt;
&lt;p&gt;by &lt;span class=&quot;printuser avatarhover&quot;&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;&lt;!--[if gte IE 7]&gt;&lt;!--&gt;&lt;img class=&quot;small&quot; src=&quot;http://www.wikidot.com/common--images/avatars/59/59229/a16.png&quot; alt=&quot;elishapeterson&quot; style=&quot;background-image:url(http://www.wikidot.com/userkarma.php?u=59229)&quot; /&gt;&lt;!--&lt;![endif]--&gt;&lt;!--[if lt IE 7]&gt;&lt;img class=&quot;small&quot; src=&quot;http://www.wikidot.com/common&amp;#45;&amp;#45;images/avatars/59/59229/a16.png&quot; alt=&quot;elishapeterson&quot; style=&quot;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=http://www.wikidot.com/userkarma.php?u=59229,sizingMethod=&#039;scale&#039;)&quot;/&gt;&lt;![endif]--&gt;&lt;/a&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;elishapeterson&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
</description>
				<pubDate>Sun, 22 Nov 2009 12:37:01 +0000</pubDate>
												<content:encoded>
					<![CDATA[
						 <p>Elisha Peterson, United States Military Academy</p> <div class="content-separator" style="display: none:"></div> <p>This note describes how to generate LaTeX images in an html document using a web service… all it takes is inserting the LaTeX into an image link.</p> <div class="content-separator" style="display: none:"></div> <div class="content-separator" style="display: none:"></div> <p>Sometimes when writing your own webpages, it's quite a pain to get LaTeX to show up on your website. There are a number of ways to do this. (On this <em>Wikidot</em> site, the LaTeX is generated from the wiki markup.) Without that option, using the <a href="http://www.codecogs.com">http://www.codecogs.com</a> web service is one of the easiest and quickest techniques I have used.</p> <p><strong>Example:</strong> <img src="http://www.codecogs.com/gif.latex?2^nx^i" alt="gif.latex?2^nx^i" class="image" /> is generated by linking to the image <tt><a href="http://www.codecogs.com/gif.latex?2^nx^i">http://www.codecogs.com/gif.latex?2^nx^i</a></tt>, which in html looks like</p> <div class="code"> <pre> <code>&lt;image src="http://www.codecogs.com/gif.latex?2^nx^i"&gt;&lt;/image&gt;</code> </pre></div> <br /> This also works to generate <tt>.png</tt>, <tt>.swf</tt>, and <tt>.pdf</tt> images. Here is the flash example (<tt>.swf</tt>), which can be resized beautifully: <div class="code"> <pre> <code>&lt;embed width="600" height="100" src="http://www.codecogs.com/swf.latex?1+sin(x)" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" align="top" scale="showall" wmode="window" devicefont="false" bgcolor="#ffffff" menu="true" allowFullScreen="true" type="application/x-shockwave-flash"&gt;&lt;/embed&gt;</code> </pre></div> <br /> <iframe src="http://www.codecogs.com/swf.latex?1+sin(x)" align="" frameborder="" height="" scrolling="" width="" class="" style=""></iframe><br /> See the website (<a href="http://www.codecogs.com">http://www.codecogs.com</a>) for more information. <p>by <span class="printuser avatarhover"><a href="http://www.wikidot.com/user:info/elishapeterson" ><!--[if gte IE 7]><!--><img class="small" src="http://www.wikidot.com/common--images/avatars/59/59229/a16.png" alt="elishapeterson" style="background-image:url(http://www.wikidot.com/userkarma.php?u=59229)" /><!--<![endif]--><!--[if lt IE 7]><img class="small" src="http://www.wikidot.com/common&#45;&#45;images/avatars/59/59229/a16.png" alt="elishapeterson" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=http://www.wikidot.com/userkarma.php?u=59229,sizingMethod='scale')"/><![endif]--></a><a href="http://www.wikidot.com/user:info/elishapeterson" >elishapeterson</a></span></p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://elishapeterson.wikidot.com/technotes:javabeans</guid>
				<title>JavaBeans</title>
				<link>http://elishapeterson.wikidot.com/technotes:javabeans</link>
				<description>

&lt;p&gt;Elisha Peterson&lt;/p&gt;
&lt;p&gt;by &lt;span class=&quot;printuser avatarhover&quot;&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;&lt;!--[if gte IE 7]&gt;&lt;!--&gt;&lt;img class=&quot;small&quot; src=&quot;http://www.wikidot.com/common--images/avatars/59/59229/a16.png&quot; alt=&quot;elishapeterson&quot; style=&quot;background-image:url(http://www.wikidot.com/userkarma.php?u=59229)&quot; /&gt;&lt;!--&lt;![endif]--&gt;&lt;!--[if lt IE 7]&gt;&lt;img class=&quot;small&quot; src=&quot;http://www.wikidot.com/common&amp;#45;&amp;#45;images/avatars/59/59229/a16.png&quot; alt=&quot;elishapeterson&quot; style=&quot;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=http://www.wikidot.com/userkarma.php?u=59229,sizingMethod=&#039;scale&#039;)&quot;/&gt;&lt;![endif]--&gt;&lt;/a&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;elishapeterson&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
</description>
				<pubDate>Wed, 01 Jul 2009 14:30:11 +0000</pubDate>
												<content:encoded>
					<![CDATA[
						 <p>Elisha Peterson</p> <div class="content-separator" style="display: none:"></div> <p>This describes "JavaBeans" and how to use them. A Java "bean" is essentially a class that adopts a kind of contract regarding the names and nature of its methods. This contract allows the class to be easily handled when it comes to adjusting its properties dynamically or through property sheets, serializing (i.e. saving the class to a file and re-loading it), event handling, and many other situations. See also <a href="http://java.sun.com/docs/books/tutorial/javabeans/TOC.html">this official guide</a>.</p> <div class="content-separator" style="display: none:"></div> <table style="margin:0; padding:0"> <tr> <td style="margin:0; padding:0"> <div id="toc"> <div id="toc-action-bar"><a href="javascript:;" >fold</a><a style="display: none" href="javascript:;" >unfold</a></div> <div class="title">Table of Contents</div> <div id="toc-list"> <div style="margin-left: 1em;"><a href="#toc0">Code Patterns</a></div> <div style="margin-left: 2em;"><a href="#toc1">Properties</a></div> <div style="margin-left: 2em;"><a href="#toc2">Events</a></div> <div style="margin-left: 2em;"><a href="#toc3">Bound Properties</a></div> <div style="margin-left: 2em;"><a href="#toc4">Constrained Properties</a></div> <div style="margin-left: 1em;"><a href="#toc5">Persistence</a></div> <div style="margin-left: 1em;"><a href="#toc6">Property Editors</a></div> <div style="margin-left: 2em;"><a href="#toc7">Display and Editing Options</a></div> <div style="margin-left: 2em;"><a href="#toc8">Standard Support Class</a></div> <div style="margin-left: 2em;"><a href="#toc9">Package Implementations</a></div> <div style="margin-left: 2em;"><a href="#toc10">Customizers</a></div> </div> </div> </td> </tr> </table> <div class="content-separator" style="display: none:"></div> <p><strong>Properties</strong> are single data values that have get and set methods. <strong>Beans</strong> contain several properties. The <strong>JavaBeans specification</strong> consists of design patterns that allow beans to be made easily portable. While a single property might use <tt>ChangeEvent</tt>s to notify listeners when a value has changed, a bean may use <tt>PropertyChangeEvent</tt>s to notify listeners of changes to a specific property. This event contains additional information about the old and new values of a property.</p> <p>The basic requirements for a Java "bean" are:</p> <ul> <li>must implement the <tt>Serializable</tt> interface;</li> <li>must have a no-argument constructor.</li> </ul> <p><em>Introspection</em> allows properties and event handling routines that are specified by particular code templates to be "discovered" by external classes. In particular, the Netbeans GUI builder can "detect" the bean properties and allow for customization. For properties of the standard types (<tt>boolean</tt>, <tt>short</tt>, <tt>int</tt>, <tt>long</tt>, <tt>float</tt>, <tt>double</tt>, <tt>String</tt>, <tt>Color</tt>), property editing is automatically supported within the GUI's <em>property sheet</em>. The code generated by the builder first calls the no-argument constructor, and then calls various <tt>set</tt> methods for the properties that have been customized.</p> <h1><span>Code Patterns</span></h1> <p>Besides the no-argument constructor, <em>JavaBeans</em> use several other code patterns.</p> <h2><span>Properties</span></h2> <p>To allow introspection, <strong>properties</strong> should conform to get/set patterns:</p> <div class="code"> <div class="hl-main"> <pre> <span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">C</span><span class="hl-code"> </span><span class="hl-identifier">getXyz</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setXyz</span><span class="hl-brackets">(</span><span class="hl-identifier">C</span><span class="hl-code"> </span><span class="hl-identifier">newValue</span><span class="hl-brackets">)</span><span class="hl-code">;</span> </pre></div> </div> <br /> where <tt>C</tt> is the class type and <tt>Xyz</tt> is the name of the property, e.g. "<tt>Value</tt>". The lowercase version "<tt>value</tt>" is the <em>official</em> property identifier. <strong>Note:</strong> for <tt>boolean</tt> properties, the getter may take the form <tt>public boolean isXyz();</tt>. <p><strong>Indexed properties</strong>, typically arrays, support indexed get/set patterns also:</p> <div class="code"> <div class="hl-main"> <pre> <span class="hl-identifier">C</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">getXyz</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setXyz</span><span class="hl-brackets">(</span><span class="hl-identifier">C</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">array</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-identifier">C</span><span class="hl-code"> </span><span class="hl-identifier">getXyz</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">index</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setXyz</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">index</span><span class="hl-code">, </span><span class="hl-identifier">C</span><span class="hl-code"> </span><span class="hl-identifier">element</span><span class="hl-brackets">)</span> </pre></div> </div> <h2><span>Events</span></h2> <p>Similarly, <strong>event handling</strong> should conform to add/remove patterns:</p> <div class="code"> <div class="hl-main"> <pre> <span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">addXyzListener</span><span class="hl-brackets">(</span><span class="hl-identifier">XyzListener</span><span class="hl-code"> </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">removeXyzListener</span><span class="hl-brackets">(</span><span class="hl-identifier">XyzListener</span><span class="hl-code"> </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span> </pre></div> </div> <br /> where <tt>Xyz</tt> is replaced by the type of listener… e.g. <tt>ChangeListener</tt> or <tt>ActionListener</tt>. <h2><span>Bound Properties</span></h2> <p><strong>Bound properties</strong> fire <tt>PropertyChangeEvent</tt>s when their value changes. This event includes (i) the <tt>String</tt> name of the property, (ii) the old value, and (iii) the new value. Any registered <tt>PropertyChangeListener</tt>s may respond to the change accordingly. The <tt>java.beans.PropertyChangeSupport</tt> class contains the necessary event handling. A typical <tt>set</tt> method differs for bound properties, as in the following example:</p> <div class="code"> <div class="hl-main"> <pre> <span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setTitle</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">title</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code"> ! </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">title</span><span class="hl-code">.</span><span class="hl-identifier">equals</span><span class="hl-brackets">(</span><span class="hl-identifier">title</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">old</span><span class="hl-code"> = </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">title</span><span class="hl-code">; </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">title</span><span class="hl-code"> = </span><span class="hl-identifier">title</span><span class="hl-code">; </span><span class="hl-identifier">firePropertyChange</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">title</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-identifier">old</span><span class="hl-code">, </span><span class="hl-identifier">title</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span> </pre></div> </div> <h2><span>Constrained Properties</span></h2> <p><strong>Constrained properties</strong> fire <tt>VetoableChangeEvent</tt>s when their value changes.</p> <h1><span>Persistence</span></h1> <p><strong>Persistence</strong> refers to the ability to save and reload classes. JavaBean code patterns allow for automated persistence.</p> <h1><span>Property Editors</span></h1> <p><tt><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/PropertyEditor.html">PropertyEditor</a></tt>s are intended to <em>"provide support for GUIs that want to allow users to edit a property value of a given type"</em>. In doing so, they handle <tt>PropertyChangeEvent</tt>s for underlying beans. When the value is changed, the class fires an event notifying any registered listeners of the change.</p> <p>The <tt>PropertyEditor</tt> API encapsulates a single bean, with <tt>getValue</tt> and <tt>setValue</tt> methods, and <tt>PropertyChangeEvent</tt> handling.</p> <p>The API also includes <tt>getJavaInitializationString</tt>, which provides Java constructor code to get the value underlying the component. This can be used by GUI builders to automatically generate the underlying object, or a copy of that object. I'm not entirely sure about its purpose.</p> <h2><span>Display and Editing Options</span></h2> <p>There are three display options:</p> <ol> <li>Textual display, via <tt>getAsText</tt>.</li> <li>Combobox display, via <tt>getAsText</tt> and <tt>String[] getTags</tt>.</li> <li>Custom component display, via <tt>isPaintable</tt> and <tt>paintRectangle</tt>.</li> </ol> <p>Similarly, there are three editing options:</p> <ol> <li>Textual editing, via <tt>setAsText</tt>.</li> <li>Combobox editing, via <tt>setAsText</tt> and <tt>String[] getTags</tt>.</li> <li>Custom component editing, via <tt>supportsCustomEditor</tt> and <tt>Component getCustomEditor</tt>.</li> </ol> <h2><span>Standard Support Class</span></h2> <p>The default implementation is the <tt><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/PropertyEditorSupport.html">PropertyEditorSupport</a></tt> class, which has simple text editing support.</p> <ul> <li>Allows customizing the source of <tt>PropertyChangeEvent</tt>s: this defaults to the class, but also may be set directly via the <em>"source"</em> bean property.</li> <li>Supports <tt>getAsText</tt> and <tt>setAsText</tt>, but an exception is thrown in the <em>set</em> case unless the value is a <tt>String</tt>.</li> <li>Does not support custom editors or tags.</li> </ul> <h2><span>Package Implementations</span></h2> <p>Several editors are already included within the <tt><a href="http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Modules-sun/beans/sun.beans.editors.htm">sun.beans.editors</a></tt> package. Most of these support textual editing only. Exceptions are noted.</p> <ul> <li><tt>BooleanEditor</tt> (tags)</li> <li><tt>ByteEditor</tt></li> <li><tt>ColorEditor</tt> (panel)</li> <li><tt>DoubleEditor</tt></li> <li><tt>EnumEditor</tt> (tags)</li> <li><tt>FloatEditor</tt></li> <li><tt>FontEditor</tt> (panel)</li> <li><tt>IntegerEditor</tt></li> <li><tt>LongEditor</tt></li> <li><tt>NumberEditor</tt></li> <li><tt>ShortEditor</tt></li> <li><tt>StringEditor</tt></li> </ul> <h2><span>Customizers</span></h2> <p>The <tt><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/Customizer.html">Customizer</a></tt> interface has basic <tt>PropertyChangeEvent</tt> support, as well as a <tt>setObject(Object bean)</tt> method that sets the argument based on the value within the customizer. These are intended to provide for more advanced customization capabilities.</p> <p>by <span class="printuser avatarhover"><a href="http://www.wikidot.com/user:info/elishapeterson" ><!--[if gte IE 7]><!--><img class="small" src="http://www.wikidot.com/common--images/avatars/59/59229/a16.png" alt="elishapeterson" style="background-image:url(http://www.wikidot.com/userkarma.php?u=59229)" /><!--<![endif]--><!--[if lt IE 7]><img class="small" src="http://www.wikidot.com/common&#45;&#45;images/avatars/59/59229/a16.png" alt="elishapeterson" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=http://www.wikidot.com/userkarma.php?u=59229,sizingMethod='scale')"/><![endif]--></a><a href="http://www.wikidot.com/user:info/elishapeterson" >elishapeterson</a></span></p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://elishapeterson.wikidot.com/technotes:3d-graphics-blaise</guid>
				<title>3D Graphics (Blaise)</title>
				<link>http://elishapeterson.wikidot.com/technotes:3d-graphics-blaise</link>
				<description>

&lt;p&gt;Elisha Peterson, United States Military Academy&lt;/p&gt;
&lt;p&gt;by &lt;span class=&quot;printuser avatarhover&quot;&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;&lt;!--[if gte IE 7]&gt;&lt;!--&gt;&lt;img class=&quot;small&quot; src=&quot;http://www.wikidot.com/common--images/avatars/59/59229/a16.png&quot; alt=&quot;elishapeterson&quot; style=&quot;background-image:url(http://www.wikidot.com/userkarma.php?u=59229)&quot; /&gt;&lt;!--&lt;![endif]--&gt;&lt;!--[if lt IE 7]&gt;&lt;img class=&quot;small&quot; src=&quot;http://www.wikidot.com/common&amp;#45;&amp;#45;images/avatars/59/59229/a16.png&quot; alt=&quot;elishapeterson&quot; style=&quot;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=http://www.wikidot.com/userkarma.php?u=59229,sizingMethod=&#039;scale&#039;)&quot;/&gt;&lt;![endif]--&gt;&lt;/a&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;elishapeterson&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
</description>
				<pubDate>Wed, 14 Jan 2009 12:26:47 +0000</pubDate>
												<content:encoded>
					<![CDATA[
						 <p>Elisha Peterson, United States Military Academy</p> <div class="content-separator" style="display: none:"></div> <p>This preliminary note will describe how I go about rendering points, curves, and surfaces in three dimensions for <a href="http://www.dean.usma.edu/math/people/peterson/blaise/">blaise</a>. See <a href="http://blaisemath.googlecode.com/">http://blaisemath.googlecode.com/</a> for the complete source code.</p> <div class="content-separator" style="display: none:"></div> <table style="margin:0; padding:0"> <tr> <td style="margin:0; padding:0"> <div id="toc"> <div id="toc-action-bar"><a href="javascript:;" >fold</a><a style="display: none" href="javascript:;" >unfold</a></div> <div class="title">Table of Contents</div> <div id="toc-list"> <div style="margin-left: 1em;"><a href="#toc0">Scene Modeling and Projections</a></div> <div style="margin-left: 2em;"><a href="#toc1">Camera Setup</a></div> <div style="margin-left: 2em;"><a href="#toc2">Projection of an Arbitrary Point</a></div> <div style="margin-left: 2em;"><a href="#toc3">Pixel Conversion</a></div> <div style="margin-left: 2em;"><a href="#toc4">Inverse Transformation</a></div> <div style="margin-left: 1em;"><a href="#toc5">Multiple Viewpoints &amp; Anaglyphs</a></div> <div style="margin-left: 2em;"><a href="#toc6">Color Filtering</a></div> <div style="margin-left: 1em;"><a href="#toc7">Rotations</a></div> <div style="margin-left: 1em;"><a href="#toc8">Source Code</a></div> <div style="margin-left: 1em;"><a href="#toc9">References</a></div> </div> </div> </td> </tr> </table> <div class="content-separator" style="display: none:"></div> <h1><span>Scene Modeling and Projections</span></h1> <p>In what follows, we use lowercase (<em>d</em>) for scalars, uppercase (<em>P</em>) for points in space, lowercase bold (<strong>x</strong>) for arbitrary vectors, and uppercase bold (<strong>T</strong>) for unit vectors.</p> <h2><span>Camera Setup</span></h2> <p>Let <em>C</em> be the camera location, with orientation <strong>T</strong>, <strong>N</strong>, <strong>B</strong>, where <strong>T</strong> is the direction the camera is facing, <strong>N</strong> is the "up" direction, and <strong>B</strong>=<strong>T</strong>x<strong>N</strong> is the perpendicular direction. Assume the viewing screen is located at a distance <em>d</em> from the camera, and is centered relative to the camera. Thus, we assume the the viewscreen is perpendicular to <strong>T</strong> and the center <em>V</em> of the viewscreen is such that <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/3e01addbcc92304aeca93d1cf9c60cc7.png" alt="\overrightarrow{CV} = d \vec{\mathbf{T}}" />.</p> <p>As indicated above, the center of the viewscreen is <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/bd8a0d7d1ea314f1f55f2722fd801908.png" alt="V=C+d\vec{\mathbf{T}}" />. Since the screen is parallel to <strong>B</strong> and <strong>N</strong>, an arbitrary point on the screen may be written as <em>V</em>+<em>x</em><strong>B</strong>+<em>y</em><strong>N</strong>. In this case, we call <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/1edc15a2b7211f53b5f6fc31bd28697f.png" alt="\langle x,y\rangle_\pi" />, the <strong>viewscreen coordinates</strong>. Let <em>w</em> be the width of the viewscreen and <em>h</em> the height; the viewscreen's four corner points are <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/3bc022ef2bf347f1c5f7e301254813d1.png" alt="V\pm\frac{w}{2}\vec{\mathbf{B}}\pm\frac{h}{2}\vec{\mathbf{N}}" />. In general, a vector <strong>y</strong> starting at the camera may be expressed as</p> <span class="equation-number">(1)</span> <div class="math-equation" id="equation-140220-1"><img src="http://elishapeterson.wikidot.com/local--math/eqs/205a55857324eeef3b91071fc330ad53.png" alt="\vec{\mathbf{y}} = (\vec{\mathbf{y}}\cdot\vec{\mathbf{T}})\vec{\mathbf{T}} + (\vec{\mathbf{y}}\cdot\vec{\mathbf{N}})\vec{\mathbf{N}} + (\vec{\mathbf{y}}\cdot\vec{\mathbf{B}})\vec{\mathbf{B}}." /></div> <p>If the vector ends on the viewscreen, its viewscreen coordinates are therefore <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/99ca4235afe0d2ff1f0d8818b0594b03.png" alt="\langle\vec{\mathbf{y}}\cdot\vec{\mathbf{B}}, \vec{\mathbf{y}}\cdot\vec{\mathbf{N}}\rangle_\pi" />.</p> <h2><span>Projection of an Arbitrary Point</span></h2> <p>Let <em>A<sub>π</sub></em> denote the projection of a point <em>A</em> onto the viewscreen; this is where the viewscreen intersects the line between <em>A</em> and <em>C</em>. Let <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/8ca954595f18123fa5e74ef4b5d17283.png" alt="\vec{\mathbf{x}}=\overrightarrow{CA}" /> be the vector pointing from the camera <em>C</em> to the point. We can express this point as <em>A<sub>π</sub></em>=<em>C</em>+λ<strong>x</strong> for some λ. The value λ is easiest to compute by focusing on the <strong>T</strong> direction. Since <em>d</em> is the point from <em>C</em> to the plane, and <strong>x</strong>·<strong>T</strong> is the distance in the same direction from the camera to the point, one has simply <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/b8811ac3b3105d471a8c3e7d9a5475ab.png" alt="\lambda=d/\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}" />. So</p> <span class="equation-number">(2)</span> <div class="math-equation" id="equation-140220-2"><img src="http://elishapeterson.wikidot.com/local--math/eqs/5dcf388a559b906c4fa3f9bfa1153eab.png" alt="A_\pi = C + \left(\frac{d}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}}\right)\overrightarrow{CA}." /></div> <p>In viewscreen coordinates, one considers the vector <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/5bf4f0cb1a4a1a6ee2113b1f354aed53.png" alt="\overrightarrow{CA_\pi}=\left(\frac{d}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}}\right)\overrightarrow{CA}" />, so that the point <em>A</em> has representation in viewscreen coordinates as follows:</p> <span class="equation-number">(3)</span> <div class="math-equation" id="equation-140220-3"><img src="http://elishapeterson.wikidot.com/local--math/eqs/a312201602fae7a8f7f26dfabaf5c1dd.png" alt="A_\pi \quad \longleftrightarrow \quad \frac{d}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}} \left\langle\vec{\mathbf{x}}\cdot\vec{\mathbf{B}}, \vec{\mathbf{x}}\cdot\vec{\mathbf{N}}\right\rangle_\pi = d \left\langle\frac{\vec{\mathbf{x}}\cdot\vec{\mathbf{B}}}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}}, \frac{\vec{\mathbf{x}}\cdot\vec{\mathbf{N}}}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}}\right\rangle_\pi." /></div> <h2><span>Pixel Conversion</span></h2> <p>As a final step, one must convert the viewscreen coordinates <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/1edc15a2b7211f53b5f6fc31bd28697f.png" alt="\langle x,y\rangle_\pi" /> to window coordinates. If the window has pixels ranging from <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/5c16f757233856dcf311176b7410d2d5.png" alt="(0,0)" /> in the upper-lefthand corner to <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/130cc62b1b4d54f8c32b4c58f2c7a571.png" alt="(W,H)" /> in the lower-righthand corner (the usual way of marking window coordinates), then the pixel coordinates of this point are</p> <span class="equation-number">(4)</span> <div class="math-equation" id="equation-140220-4"><img src="http://elishapeterson.wikidot.com/local--math/eqs/4a1fbdd522cb36fd96dd5bf6abc99ad2.png" alt="\langle x,y\rangle_\pi \quad \longleftrightarrow \quad \left(\tfrac{W}{w}(x+\tfrac{w}{2}), -\tfrac{H}{h}(y-\tfrac{h}{2})\right)." /></div> <p>Note that the ratios <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/254c2bc11f73b25912ea3f035232ff12.png" alt="\frac{W}{w}" /> and <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/7b00c4f7f9a3c20884186d9200a5d817.png" alt="\frac{H}{h}" /> will typically be the same and represent <em>the number of pixels per unit length</em>. If this is represented by a constant factor η, the formula becomes <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/4021ba33b89a5e1899bb86323ebe33bc.png" alt="\eta\left(x+\frac{w}{2},-y+\frac{h}{2}\right) = \left(\frac{W}{2} + \eta x, \frac{H}{2}-\eta y\right)" />.</p> <p>Combining the above discussion, if <strong>x</strong> represents the vector from <em>C</em> to <em>A</em>, then the pixel coordinates of <em>A</em>, as it appears on the viewscreen, should be given by the mapping</p> <span class="equation-number">(5)</span> <div class="math-equation" id="equation-140220-5"><img src="http://elishapeterson.wikidot.com/local--math/eqs/36f0f1343cdc79866f72553935c77e6d.png" alt="A \quad \longmapsto \quad \left(\tfrac{W}{2}, \tfrac{H}{2}\right) + \frac{\eta d}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}} \left(\vec{\mathbf{x}}\cdot\vec{\mathbf{B}}, -\vec{\mathbf{x}}\cdot\vec{\mathbf{N}}\right)." /></div> <h2><span>Inverse Transformation</span></h2> <p>A point (<em>x<sub>p</sub></em>,<em>y<sub>p</sub></em>) in pixel coordinates transforms in viewscreen coordinates to <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/a0d3962cffaf859a0f70db613c33522e.png" alt="\left\langle\frac{x_p-W/2}{\eta}, -\frac{y_p-H/2}{\eta}\right\rangle = \left\langle\frac{x_p}{\eta}-\frac{w}{2}, -\frac{y_p}{\eta}+\frac{h}{2}\right\rangle" />. Hence, the corresponding point on the viewscreen is</p> <span class="equation-number">(6)</span> <div class="math-equation" id="equation-140220-6"><img src="http://elishapeterson.wikidot.com/local--math/eqs/fe5302ad9fd23801ae252ba5620d1e6d.png" alt="(x_p,y_p) \quad \longmapsto \quad V + \left(\frac{x_p}{\eta}-\frac{w}{2}\right)\vec{\mathbf{B}} + \left(\frac{y_p}{\eta}+\frac{h}{2}\right)\vec{\mathbf{N}}." /></div> <p>The full line of potential inverse mappings is</p> <span class="equation-number">(7)</span> <div class="math-equation" id="equation-140220-7"><img src="http://elishapeterson.wikidot.com/local--math/eqs/969406c3eb7c220d7bd6668d38819dae.png" alt="C + t \left(d\vec{\mathbf{T}} + \left(\frac{x_p}{\eta}-\frac{w}{2}\right)\vec{\mathbf{B}} + \left(\frac{y_p}{\eta}+\frac{h}{2}\right)\vec{\mathbf{N}}\right)." /></div> <h1><span>Multiple Viewpoints &amp; Anaglyphs</span></h1> <p><a href="http://en.wikipedia.org/wiki/anaglyph_image" >Anaglyphs</a> are images rendered separately for each eye, typically with a different color so they can be seen using 3D glasses. In this case, one must use two different projections to obtain the image of the two cameras.</p> <p>We will look at how the above formulas change for a camera <em>C<sub>ε</sub></em> that is shifted from the original location so that <em>C<sub>ε</sub></em> = <em>C</em> + <strong>ε</strong> = <em>C</em> + <em>ε<sub>x</sub></em><strong>B</strong> + <em>ε<sub>y</sub></em><strong>N</strong> (a translation parallel to the viewscreen). Let <em>A<sub>πε</sub></em> represent the point on the viewscreen as seen by the new camera. Note that <em>V<sub>ε</sub></em> = <em>V</em> + <strong>ε</strong> as well, since the translation is parallel to the viewscreen. One therefore has</p> <span class="equation-number">(8)</span> <div class="math-equation" id="equation-140220-8"><img src="http://elishapeterson.wikidot.com/local--math/eqs/b3167853994b9d860d1162d21d54d62e.png" alt="A_{\pi\epsilon} \quad \longleftrightarrow \quad \frac{d}{\vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{T}}} \left\langle\vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{B}}, \vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{N}}\right\rangle_{\pi\varepsilon} = \frac{d}{\vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{T}}} \left\langle\vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{B}}, \vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{N}}\right\rangle_\pi + \left\langle\varepsilon_x,\varepsilon_y\right\rangle_\pi." /></div> <p>This can be further simplifed. Since <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/d1e8fd2e6dc0a2df7aa893596791acf2.png" alt="\vec{\mathbf{x}}_\varepsilon=\overrightarrow{C_\varepsilon A}=\overrightarrow{CA}-\mathbf{\vec\varepsilon}=\vec{\mathbf{x}}-\mathbf{\vec\varepsilon}" />, we have <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/9998c055af54076ca49ae28fac9f0f46.png" alt="\vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{T}}=\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}" />, <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/f0e114b36951556f4e8b748e2174473a.png" alt="\vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{B}}=\vec{\mathbf{x}}\cdot\vec{\mathbf{B}}-\varepsilon_x" />, and <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/79eade9ea65ef6baf1f7669ff2959c86.png" alt="\vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{N}}=\vec{\mathbf{x}}\cdot\vec{\mathbf{N}}-\varepsilon_y" />. This shows that</p> <span class="equation-number">(9)</span> <div class="math-equation" id="equation-140220-9"><img src="http://elishapeterson.wikidot.com/local--math/eqs/df851ac19169473248f1a82b93b8d159.png" alt="A_{\pi\epsilon} \quad \longleftrightarrow \quad \frac{d}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}} \left\langle\vec{\mathbf{x}}\cdot\vec{\mathbf{B}}-\varepsilon_x, \vec{\mathbf{x}}\cdot\vec{\mathbf{N}}-\varepsilon_y\right\rangle_\pi + \left\langle\varepsilon_x,\varepsilon_y\right\rangle_\pi = \frac{d}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}} \left\langle\vec{\mathbf{x}}\cdot\vec{\mathbf{B}}, \vec{\mathbf{x}}\cdot\vec{\mathbf{N}}\right\rangle_\pi + \left(1-\frac{d}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}}\right)\left\langle\varepsilon_x,\varepsilon_y\rangle_\pi." /></div> <p>Therefore, the parallel camera which is shifted by <strong>ε</strong> will give points that are shifted by (1-λ)<strong>ε</strong> (with the vector <strong>ε</strong> considered in the proper coordinate system.)</p> <p>When reduced to the window's coordinate system, recall that <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/1eef28995b37764ee1ff52e355ada2b8.png" alt="\langle x,y\rangle_\pi \leftrightarrow (\frac{W}{2},\frac{H}{2}) + \eta(x,-y)" />, where <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/14545597962a57095687e6cf637bc52a.png" alt="\eta=\frac{W}{w}=\frac{H}{h}" /> is the number of pixels per unit length. Therefore, one has</p> <span class="equation-number">(10)</span> <div class="math-equation" id="equation-140220-10"><img src="http://elishapeterson.wikidot.com/local--math/eqs/97933333e283f48d593e0f56d36f5a11.png" alt="\langle x+\Delta x,y+\Delta y\rangle \leftrightarrow (\tfrac{W}{2},\tfrac{H}{2}) + \eta(x,-y) + \eta(dx,-dy)." /></div> <p>So the endstate of a camera shift is a change of</p> <span class="equation-number">(11)</span> <div class="math-equation" id="equation-140220-11"><img src="http://elishapeterson.wikidot.com/local--math/eqs/148af65bdcc3f1b14014e1509cc3d2f5.png" alt="\eta(1-\lambda)\langle\varepsilon_x, \varepsilon_y\rangle = \eta\left(1-\frac{d}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}}\right)\vec{\mathbf{\varepsilon}}" /></div> <p>in window coordinates.</p> <p>A typical two-camera construction (e.g. our eyes) will have cameras at <em>C</em> ± <em>ε</em><strong>B</strong> for a scalar <em>ε</em> representing one half the distance between the eyes. So in the viewscreen coordinates, the <em>x</em> values are shifted by ±(1-λ)<em>ε</em>. And in the window coordinates, the values are shifted by ±η(1-λ)<em>ε</em>.</p> <h2><span>Color Filtering</span></h2> <p>Two-color 3d images work by providing two images with different color schemes, that depend upon the color of lenses in a pair of 3d glasses. When looking through a red lens, one cannot see the color red; therefore, the image seen through the red lens should be comprised of colors using only green and blue channels. When looking through a second lens (e.g. cyan), one may use the red channel. For example, if the left lens is red, the image projected by the left camera (left eye) should be colored with only green and blue. Similarly, if the right lens is cyan, the image projected by the right camera (right eye) should be colored by red only.</p> <h1><span>Rotations</span></h1> <p>Rotations are achieved by the following formula [<a href="javascript:;" class="bibcite" id="bibcite-479866-1-37901a" >1</a>]:</p> <span class="equation-number">(12)</span> <div class="math-equation" id="equation-140220-12"><img src="http://elishapeterson.wikidot.com/local--math/eqs/074b2015faa4ce4016f09f0d0c888eff.png" alt="\mathbf{r} \longmapsto \mathbf{r}\cos\Phi + \hat{\mathbf{n}}(\hat{\mathbf{n}}\cdot\mathbf{r})(1-\cos\Phi)+(\mathbf{r}\times\hat{\mathbf{n}})\sin\Phi." /></div> <p>Here, the vector <strong>r</strong> is being rotated by an angle of Φ around the axis <strong>n</strong>.</p> <p>The motion for rotating the figure is "dragging" the screen. The natural interaction is to treat the rendered scene as existing inside a ball, and rotating that ball around its center to see how the image changes. In our case, the center of the ball will be denoted by <em>S</em>=<em>C</em>+Δ<strong>T</strong>, where Δ&gt;<em>d</em> and the radius of that ball will be Δ–<em>d</em>. (Recall that <em>d</em> is the distance to the viewscreen. Think of Δ as the distance to the center of the scene of interest, since it defines the point of rotation.) When the user drags the mouse, points are created for the start and end of that motion, call them <em>R</em><sub>1</sub> and <em>R</em><sub>2</sub>. Let <strong>r</strong><sub>i</sub> be the unit vector <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/79cda8374688b927b6f282459bd954ce.png" alt="\overrightarrow{SR_i}/\overrightarrow{SR_i}" /> formed by normalizing the vector from <em>S</em> to <em>R</em><sub>i</sub>. Then the axis of rotation is the unit vector</p> <span class="equation-number">(13)</span> <div class="math-equation" id="equation-140220-13"><img src="http://elishapeterson.wikidot.com/local--math/eqs/0d7af21873069d9a034cdab5b4b1083f.png" alt="\hat{\mathbf{n}}=\hat{\mathbf{r}}_1\times\hat{\mathbf{r}}_2," /></div> <p>and the angle of rotation is given by the dot product formula</p> <span class="equation-number">(14)</span> <div class="math-equation" id="equation-140220-14"><img src="http://elishapeterson.wikidot.com/local--math/eqs/9acc7a18f9d2d7d2052bc8777ad2ac9b.png" alt="\cos\Phi = \hat{\mathbf{r}}_1\cdot\hat{\mathbf{r}}_2." /></div> <p>The Euler rotation formula can then be used to transform the camera's <strong>T</strong>,<strong>N</strong>,<strong>B</strong> frame, and thereby recenter the camera to the location <em>S</em>–Δ<strong>T</strong> (in the transformed <strong>T</strong>).</p> <p>In some cases, it may be preferable to allow for the user to make an image rotate faster or slower. This is easily accomplished by scaling the rotation angle Φ to some <em>C</em>Φ. One may also animate a rotation, by keeping the axis of rotation fixed, and increasing the rotation by a small angle dΦ at each time step.</p> <h1><span>Source Code</span></h1> <p><em>Coming soon… see page history for old source code.</em></p> <h1><span>References</span></h1> <div class="bibitems"> <div class="bibitem" id="bibitem-479866-1">1. Weisstein, Eric W. "Rotation Formula." From MathWorld—A Wolfram Web Resource. <a href="http://mathworld.wolfram.com/RotationFormula.html">http://mathworld.wolfram.com/RotationFormula.html</a></div> </div> <p>by <span class="printuser avatarhover"><a href="http://www.wikidot.com/user:info/elishapeterson" ><!--[if gte IE 7]><!--><img class="small" src="http://www.wikidot.com/common--images/avatars/59/59229/a16.png" alt="elishapeterson" style="background-image:url(http://www.wikidot.com/userkarma.php?u=59229)" /><!--<![endif]--><!--[if lt IE 7]><img class="small" src="http://www.wikidot.com/common&#45;&#45;images/avatars/59/59229/a16.png" alt="elishapeterson" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=http://www.wikidot.com/userkarma.php?u=59229,sizingMethod='scale')"/><![endif]--></a><a href="http://www.wikidot.com/user:info/elishapeterson" >elishapeterson</a></span></p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://elishapeterson.wikidot.com/technotes:xml-output-with-jaxp</guid>
				<title>XML Output with JAXB 2.0</title>
				<link>http://elishapeterson.wikidot.com/technotes:xml-output-with-jaxp</link>
				<description>

&lt;p&gt;Elisha Peterson&lt;/p&gt;
&lt;p&gt;by &lt;span class=&quot;printuser avatarhover&quot;&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;&lt;!--[if gte IE 7]&gt;&lt;!--&gt;&lt;img class=&quot;small&quot; src=&quot;http://www.wikidot.com/common--images/avatars/59/59229/a16.png&quot; alt=&quot;elishapeterson&quot; style=&quot;background-image:url(http://www.wikidot.com/userkarma.php?u=59229)&quot; /&gt;&lt;!--&lt;![endif]--&gt;&lt;!--[if lt IE 7]&gt;&lt;img class=&quot;small&quot; src=&quot;http://www.wikidot.com/common&amp;#45;&amp;#45;images/avatars/59/59229/a16.png&quot; alt=&quot;elishapeterson&quot; style=&quot;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=http://www.wikidot.com/userkarma.php?u=59229,sizingMethod=&#039;scale&#039;)&quot;/&gt;&lt;![endif]--&gt;&lt;/a&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;elishapeterson&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
</description>
				<pubDate>Thu, 18 Sep 2008 20:33:31 +0000</pubDate>
												<content:encoded>
					<![CDATA[
						 <p>Elisha Peterson</p> <div class="content-separator" style="display: none:"></div> <p>This note covers some basics of JAXB (Java API for XML Binding). See also the <a href="http://en.wikipedia.org/wiki/JAXB" >Wikipedia entry</a>, the <a href="https://jaxb.dev.java.net/">development site</a>, a <a href="http://blogs.sun.com/CoreJavaTechTips/entry/exchanging_data_with_xml_and">tutorial by Sun</a>, and a <a href="http://www.caucho.com/resin/doc/jaxb-annotations.xtp">description of JAXB annotations</a>.</p> <div class="content-separator" style="display: none:"></div> <p><strong>Keywords:</strong> Java, XML, JAXB, JavaBeans, Binding</p> <table style="margin:0; padding:0"> <tr> <td style="margin:0; padding:0"> <div id="toc"> <div id="toc-action-bar"><a href="javascript:;" >fold</a><a style="display: none" href="javascript:;" >unfold</a></div> <div class="title">Table of Contents</div> <div id="toc-list"> <div style="margin-left: 1em;"><a href="#toc0">Marshalling</a></div> <div style="margin-left: 2em;"><a href="#toc1">Automating the Process</a></div> <div style="margin-left: 2em;"><a href="#toc2">Working with Vectors of Data</a></div> <div style="margin-left: 1em;"><a href="#toc3">Unmarshalling</a></div> <div style="margin-left: 2em;"><a href="#toc4">Source Code</a></div> <div style="margin-left: 1em;"><a href="#toc5">Working with Inheritance</a></div> <div style="margin-left: 2em;"><a href="#toc6">First Technique</a></div> <div style="margin-left: 3em;"><a href="#toc7">Collections</a></div> <div style="margin-left: 2em;"><a href="#toc8">Second Technique</a></div> <div style="margin-left: 1em;"><a href="#toc9">Customizing the XML</a></div> </div> </div> </td> </tr> </table> <div class="content-separator" style="display: none:"></div> <h1><span>Marshalling</span></h1> <p>Suppose you want to automatically generate an XML file for your object. In JAXB, this process is called "marshalling". The idea is to annotate the appropriate fields that you want to store in the XML file. It handles the file-generation automatically. Here's a simple example:</p> <div class="code"> <div class="hl-main"> <pre> <span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Main</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlRootElement</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">; @</span><span class="hl-identifier">XmlElement</span><span class="hl-code"> </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">height</span><span class="hl-code">; @</span><span class="hl-identifier">XmlElement</span><span class="hl-code"> </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">width</span><span class="hl-code">; </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(){</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">Spot</span><span class="hl-quotes">"</span><span class="hl-code">;</span><span class="hl-identifier">height</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">;</span><span class="hl-identifier">width</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">throws</span><span class="hl-code"> </span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">JAXBContext</span><span class="hl-code"> </span><span class="hl-identifier">jc</span><span class="hl-code"> = </span><span class="hl-identifier">JAXBContext</span><span class="hl-code">.</span><span class="hl-identifier">newInstance</span><span class="hl-brackets">(</span><span class="hl-identifier">Dog</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">jc</span><span class="hl-code">.</span><span class="hl-identifier">createMarshaller</span><span class="hl-brackets">()</span><span class="hl-code">.</span><span class="hl-identifier">marshal</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">()</span><span class="hl-code">,</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">FileOutputStream</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">dog.xml</span><span class="hl-quotes">"</span><span class="hl-brackets">))</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span> </pre></div> </div> <p>Here <tt>@XmlRootElement</tt> identifies the root element of the XML document tree (DOM), which is required for "well-formed" SML. <tt>@XmlAttribute</tt> specifies an attribute supplied "inline" with the object definition, while <tt>@XmlElement</tt> identifies attributes that are specified separately. Running this file produces the following output:</p> <div class="code"> <div class="hl-main"> <pre> <span class="hl-brackets">&lt;?</span><span class="hl-reserved">xml</span><span class="hl-code"> </span><span class="hl-var">version</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">1.0</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">encoding</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">UTF-8</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">standalone</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">yes</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-brackets">?&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">dog</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">Spot</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">height</span><span class="hl-brackets">&gt;</span><span class="hl-code">10.0</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">height</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">width</span><span class="hl-brackets">&gt;</span><span class="hl-code">10.0</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">width</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">dog</span><span class="hl-brackets">&gt;</span> </pre></div> </div> <h2><span>Automating the Process</span></h2> <p>You can shorten the code a bit by telling the marshaller to automatically put fields into the output file. The command for this is <tt>@XmlAccessorType</tt>, as shown in this abbreviated version:</p> <div class="code"> <div class="hl-main"> <pre> <span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Main</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlRootElement</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAccessorType</span><span class="hl-brackets">(</span><span class="hl-identifier">XmlAccessType</span><span class="hl-code">.</span><span class="hl-identifier">FIELD</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">; </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">height</span><span class="hl-code">; </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">width</span><span class="hl-code">; </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(){</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">Spot</span><span class="hl-quotes">"</span><span class="hl-code">;</span><span class="hl-identifier">height</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">;</span><span class="hl-identifier">width</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">throws</span><span class="hl-code"> </span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">JAXBContext</span><span class="hl-code"> </span><span class="hl-identifier">jc</span><span class="hl-code"> = </span><span class="hl-identifier">JAXBContext</span><span class="hl-code">.</span><span class="hl-identifier">newInstance</span><span class="hl-brackets">(</span><span class="hl-identifier">Dog</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">jc</span><span class="hl-code">.</span><span class="hl-identifier">createMarshaller</span><span class="hl-brackets">()</span><span class="hl-code">.</span><span class="hl-identifier">marshal</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">()</span><span class="hl-code">,</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">FileOutputStream</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">dog.xml</span><span class="hl-quotes">"</span><span class="hl-brackets">))</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span> </pre></div> </div> <h2><span>Working with Vectors of Data</span></h2> <p>We can easily modify the above code to work with deeper nesting structures and vectors of data. Here is the code for marshalling a "pack" of dogs:</p> <div class="code"> <div class="hl-main"> <pre> <span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Main</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlRootElement</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAccessorType</span><span class="hl-brackets">(</span><span class="hl-identifier">XmlAccessType</span><span class="hl-code">.</span><span class="hl-identifier">FIELD</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Pack</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">Vector</span><span class="hl-code">&lt;</span><span class="hl-identifier">Dog</span><span class="hl-code">&gt; </span><span class="hl-identifier">dog</span><span class="hl-code">; </span><span class="hl-identifier">Pack</span><span class="hl-brackets">(){</span><span class="hl-identifier">dog</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Vector</span><span class="hl-code">&lt;</span><span class="hl-identifier">Dog</span><span class="hl-code">&gt;</span><span class="hl-brackets">()</span><span class="hl-code">;</span><span class="hl-identifier">dog</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">())</span><span class="hl-code">;</span><span class="hl-identifier">dog</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">())</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">; @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">height</span><span class="hl-code">; @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">width</span><span class="hl-code">; @</span><span class="hl-identifier">XmlElement</span><span class="hl-code"> </span><span class="hl-identifier">Coat</span><span class="hl-code"> </span><span class="hl-identifier">coat</span><span class="hl-code">; </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(){</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">Spot</span><span class="hl-quotes">"</span><span class="hl-code">;</span><span class="hl-identifier">height</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">;</span><span class="hl-identifier">width</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">;</span><span class="hl-identifier">coat</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Coat</span><span class="hl-brackets">()</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Coat</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlElement</span><span class="hl-code"> </span><span class="hl-identifier">ColorX</span><span class="hl-code"> </span><span class="hl-identifier">color</span><span class="hl-code">; @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">thickness</span><span class="hl-code">; @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">type</span><span class="hl-code">; </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Coat</span><span class="hl-brackets">(){</span><span class="hl-identifier">color</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">ColorX</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-identifier">thickness</span><span class="hl-code">=</span><span class="hl-number">2</span><span class="hl-code">; </span><span class="hl-identifier">type</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">solid</span><span class="hl-quotes">"</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">ColorX</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">Color</span><span class="hl-code"> </span><span class="hl-identifier">col</span><span class="hl-code">; </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">ColorX</span><span class="hl-brackets">(){</span><span class="hl-identifier">col</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Color</span><span class="hl-brackets">(</span><span class="hl-number">33</span><span class="hl-code">,</span><span class="hl-number">33</span><span class="hl-code">,</span><span class="hl-number">33</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">getRed</span><span class="hl-brackets">(){</span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getRed</span><span class="hl-brackets">()</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setRed</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">r</span><span class="hl-brackets">){</span><span class="hl-identifier">col</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Color</span><span class="hl-brackets">(</span><span class="hl-identifier">r</span><span class="hl-code">,</span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getGreen</span><span class="hl-brackets">()</span><span class="hl-code">,</span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getBlue</span><span class="hl-brackets">())</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">getGreen</span><span class="hl-brackets">(){</span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getGreen</span><span class="hl-brackets">()</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setGreen</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">g</span><span class="hl-brackets">){</span><span class="hl-identifier">col</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Color</span><span class="hl-brackets">(</span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getRed</span><span class="hl-brackets">()</span><span class="hl-code">,</span><span class="hl-identifier">g</span><span class="hl-code">,</span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getBlue</span><span class="hl-brackets">())</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">getBlue</span><span class="hl-brackets">(){</span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getBlue</span><span class="hl-brackets">()</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setBlue</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-brackets">){</span><span class="hl-identifier">col</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Color</span><span class="hl-brackets">(</span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getRed</span><span class="hl-brackets">()</span><span class="hl-code">,</span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getGreen</span><span class="hl-brackets">()</span><span class="hl-code">,</span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">throws</span><span class="hl-code"> </span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">JAXBContext</span><span class="hl-code"> </span><span class="hl-identifier">jc</span><span class="hl-code"> = </span><span class="hl-identifier">JAXBContext</span><span class="hl-code">.</span><span class="hl-identifier">newInstance</span><span class="hl-brackets">(</span><span class="hl-identifier">Pack</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">jc</span><span class="hl-code">.</span><span class="hl-identifier">createMarshaller</span><span class="hl-brackets">()</span><span class="hl-code">.</span><span class="hl-identifier">marshal</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Pack</span><span class="hl-brackets">()</span><span class="hl-code">,</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">FileOutputStream</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">dog.xml</span><span class="hl-quotes">"</span><span class="hl-brackets">))</span><span class="hl-code">; </span><span class="hl-brackets">}</span> </pre></div> </div> <p>Note that saving a color in terms of its components requires creating "get" and "set" methods for the color components (making it a "<a href="http://en.wikipedia.org/wiki/JavaBeans" >Java bean</a>"). A better approach is to use the <tt>@XmlJavaTypeAdapter</tt> (discussed later). Here is the resulting <tt>XML</tt> file, with the pack initialized to contain two of the default dogs:</p> <div class="code"> <div class="hl-main"> <pre> <span class="hl-brackets">&lt;</span><span class="hl-reserved">pack</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">dog</span><span class="hl-code"> </span><span class="hl-var">width</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">10.0</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">height</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">10.0</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">Spot</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">coat</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">solid</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">thickness</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">2.0</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">color</span><span class="hl-code"> </span><span class="hl-var">red</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">33</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">green</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">33</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">blue</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">33</span><span class="hl-quotes">"</span><span class="hl-brackets">/&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">coat</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">dog</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">dog</span><span class="hl-code"> </span><span class="hl-var">width</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">10.0</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">height</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">10.0</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">Spot</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">coat</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">solid</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">thickness</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">2.0</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">color</span><span class="hl-code"> </span><span class="hl-var">red</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">33</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">green</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">33</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">blue</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">33</span><span class="hl-quotes">"</span><span class="hl-brackets">/&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">coat</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">dog</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">pack</span><span class="hl-brackets">&gt;</span> </pre></div> </div> <h1><span>Unmarshalling</span></h1> <p>The process of returning the XML code into objects is called "unmarshalling". If the XML file <tt>doggin.xml</tt> is edited, maintaining the same structure, then one can read the data back in just as easily. Here is the code to read the dogs back into a <tt>Pack</tt> object:</p> <div class="code"> <div class="hl-main"> <pre> <span class="hl-code"> </span><span class="hl-identifier">Pack</span><span class="hl-code"> </span><span class="hl-identifier">inputPack</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">Pack</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-identifier">jc</span><span class="hl-code">.</span><span class="hl-identifier">createUnmarshaller</span><span class="hl-brackets">()</span><span class="hl-code">.</span><span class="hl-identifier">unmarshal</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">FileInputStream</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">doggin.xml</span><span class="hl-quotes">"</span><span class="hl-brackets">))</span><span class="hl-code">;</span> </pre></div> </div> <br /> It seems easy, but one must be design the Java class carefully to ensure that things end up back in the proper places. Here is the order in which the unmarshal command calls Java methods: <ol> <li>Call to no-argument constructor to create the class</li> <li>Call to get and set methods in the order they appear within the XML</li> </ol> <p>It is a good idea to design constructors with arguments to work the same way within your code (this avoids bugs).</p> <h2><span>Source Code</span></h2> <p>Here is a sample input file: <a href="http://elishapeterson.wikidot.com/local--files/technotes:xml-output-with-jaxp/doggin.xml">doggin.xml</a>. You can modify it directly, and see the resulting change in the <tt>inputPack</tt> object. The complete source code with <tt>String</tt> outputs to test the process is: <a href="http://elishapeterson.wikidot.com/local--files/technotes:xml-output-with-jaxp/Main.java">Main.java</a>.</p> <h1><span>Working with Inheritance</span></h1> <h2><span>First Technique</span></h2> <div class="wiki-note"> <p><span style="color:red">This section is partially incomplete/inaccurate. I am still trying to figure out how this works!</span></p> </div> <p>XML binding will usually take place with a declared class rather than the "runtime" class. One approach that works is annotating with a list of possible concrete types. Suppose <tt>Animal</tt> is an abstract class that might represent a <tt>Cat</tt> or <tt>Dog</tt> or other animal? One can annotate the corresponding property with <tt>@XmlElements</tt> as follows:</p> <div class="code"> <div class="hl-main"> <pre> <span class="hl-code">@</span><span class="hl-identifier">XmlRootElement</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Zoo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlElements</span><span class="hl-brackets">({</span><span class="hl-code"> @</span><span class="hl-identifier">XmlElement</span><span class="hl-brackets">(</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">dog</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">type</span><span class="hl-code">=</span><span class="hl-identifier">Dog</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-brackets">)</span><span class="hl-code">, @</span><span class="hl-identifier">XmlElement</span><span class="hl-brackets">(</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">cat</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">type</span><span class="hl-code">=</span><span class="hl-identifier">Cat</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">})</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Animal</span><span class="hl-code"> </span><span class="hl-identifier">animal</span><span class="hl-code">; ... </span><span class="hl-brackets">}</span> </pre></div> </div> <br /> This will allow for multiple types of XML tags within the output's <tt>&lt;zoo&gt;</tt> tag: <div class="code"> <div class="hl-main"> <pre> <span class="hl-brackets">&lt;</span><span class="hl-reserved">zoo</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">dog</span><span class="hl-code"> ...</span><span class="hl-brackets">/&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">zoo</span><span class="hl-brackets">&gt;</span> </pre></div> </div> <br /> or <div class="code"> <div class="hl-main"> <pre> <span class="hl-brackets">&lt;</span><span class="hl-reserved">zoo</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">cat</span><span class="hl-code"> ...</span><span class="hl-brackets">/&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">zoo</span><span class="hl-brackets">&gt;</span> </pre></div> </div> <br /> are both acceptable. <p>The use of the <tt>@XmlElements</tt> tag makes the following differences in the XML:</p> <ul> <li>tag names are specific to the type of subclass of the <tt>Animal</tt> class</li> <li>any additional attributes or elements specified as part of the subclass are included in the XML</li> </ul> <p>Thus, one generally needs to use this technique to ensure that inheritance works properly.</p> <h3><span>Collections</span></h3> <p>How do you deal with structures such as <tt>Vector&lt;Animal&gt;</tt>? The process is similar:</p> <div class="code"> <div class="hl-main"> <pre> <span class="hl-code">@</span><span class="hl-identifier">XmlRootElement</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Zoo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlElements</span><span class="hl-brackets">({</span><span class="hl-code"> @</span><span class="hl-identifier">XmlElement</span><span class="hl-brackets">(</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">dog</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">type</span><span class="hl-code">=</span><span class="hl-identifier">Dog</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-brackets">)</span><span class="hl-code">, @</span><span class="hl-identifier">XmlElement</span><span class="hl-brackets">(</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">cat</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">type</span><span class="hl-code">=</span><span class="hl-identifier">Cat</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">})</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Vector</span><span class="hl-code">&lt;</span><span class="hl-identifier">Animal</span><span class="hl-code">&gt; </span><span class="hl-identifier">animals</span><span class="hl-code">; ... </span><span class="hl-brackets">}</span> </pre></div> </div> <br /> This will allow for multiple types of XML tags within the output's <tt>&lt;zoo&gt;</tt> tag: <div class="code"> <div class="hl-main"> <pre> <span class="hl-brackets">&lt;</span><span class="hl-reserved">zoo</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">dog</span><span class="hl-code"> ...</span><span class="hl-brackets">/&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">dog</span><span class="hl-code"> ...</span><span class="hl-brackets">/&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">cat</span><span class="hl-code"> ...</span><span class="hl-brackets">/&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">zoo</span><span class="hl-brackets">&gt;</span> </pre></div> </div> <h2><span>Second Technique</span></h2> <p>Another approach is to register the descendant classes into the <tt>JAXBContext</tt> and to use the <tt>@XmlAnyElement</tt> annotation. Here's how. First, you'll need to annotate the subclasses as follows:</p> <div class="code"> <div class="hl-main"> <pre> <span class="hl-code">@</span><span class="hl-identifier">XmlRootElement</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAccessorType</span><span class="hl-brackets">(</span><span class="hl-identifier">XmlAccessType</span><span class="hl-code">.</span><span class="hl-identifier">FIELD</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Zoo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAnyElement</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">Animal</span><span class="hl-code">&gt; </span><span class="hl-identifier">animal</span><span class="hl-code">; ... </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">interface</span><span class="hl-code"> </span><span class="hl-identifier">Animal</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> ... </span><span class="hl-brackets">}</span><span class="hl-code"> @</span><span class="hl-identifier">XmlRootElement</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAccessorType</span><span class="hl-brackets">(</span><span class="hl-identifier">XmlAccessType</span><span class="hl-code">.</span><span class="hl-identifier">FIELD</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Lion</span><span class="hl-code"> </span><span class="hl-reserved">implements</span><span class="hl-code"> </span><span class="hl-identifier">Animal</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code"> = </span><span class="hl-quotes">"</span><span class="hl-string">Simba</span><span class="hl-quotes">"</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-comment">// other subclasses of animal</span> </pre></div> </div> <br /> Now when initializing the <tt>JAXBContext</tt> use: <div class="code"> <div class="hl-main"> <pre> <span class="hl-identifier">JAXBContext</span><span class="hl-code"> </span><span class="hl-identifier">jc</span><span class="hl-code"> = </span><span class="hl-identifier">JAXBContext</span><span class="hl-code">.</span><span class="hl-identifier">newInstance</span><span class="hl-brackets">(</span><span class="hl-identifier">Zoo</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-code">, </span><span class="hl-identifier">Lion</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-code">, ...</span><span class="hl-brackets">)</span><span class="hl-code">;</span> </pre></div> </div> <br /> This registers the subclasses with the context so it recognizes them during the marshalling process. <h1><span>Customizing the XML</span></h1> <p>The <tt>@XmlJavaTypeAdapter</tt> annotation allows one to "intercept" the marshalling process and so specify exactly what the <tt>XML</tt> will look like. This requires writing two additional classes: one for the adapted version of the class, and an adapter class that handles the conversion process. Here is an example that stores dogs at half their height:</p> <div class="code"> <div class="hl-main"> <pre> <span class="hl-comment">// our original class</span><span class="hl-code"> @</span><span class="hl-identifier">XmlJavaTypeAdapter</span><span class="hl-brackets">(</span><span class="hl-identifier">DogAdapter</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">height</span><span class="hl-code">; </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(</span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">height</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">height</span><span class="hl-code"> = </span><span class="hl-identifier">height</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-comment">// our adapted class</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAccessorType</span><span class="hl-brackets">(</span><span class="hl-identifier">XmlAccessType</span><span class="hl-code">.</span><span class="hl-identifier">FIELD</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">LittleDog</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">height</span><span class="hl-code">; </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">LittleDog</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-brackets">{}</span><span class="hl-code"> </span><span class="hl-comment">// no-arg default constructor required for marshalling</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">LittleDog</span><span class="hl-brackets">(</span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">height</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">height</span><span class="hl-code"> = </span><span class="hl-identifier">height</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-comment">// this performs the class conversion</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">DogAdapter</span><span class="hl-code"> </span><span class="hl-reserved">extends</span><span class="hl-code"> </span><span class="hl-identifier">XmlAdapter</span><span class="hl-code">&lt;</span><span class="hl-identifier">LittleDog</span><span class="hl-code">, </span><span class="hl-identifier">Dog</span><span class="hl-code">&gt; </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">Override</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-identifier">unmarshal</span><span class="hl-brackets">(</span><span class="hl-identifier">LittleDog</span><span class="hl-code"> </span><span class="hl-identifier">v</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">throws</span><span class="hl-code"> </span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(</span><span class="hl-identifier">v</span><span class="hl-code">.</span><span class="hl-identifier">height</span><span class="hl-code">*</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> @</span><span class="hl-identifier">Override</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">LittleDog</span><span class="hl-code"> </span><span class="hl-identifier">marshal</span><span class="hl-brackets">(</span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-identifier">v</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">throws</span><span class="hl-code"> </span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">LittleDog</span><span class="hl-brackets">(</span><span class="hl-identifier">v</span><span class="hl-code">.</span><span class="hl-identifier">height</span><span class="hl-code">/</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-comment">// the enclosing instance</span><span class="hl-code"> @</span><span class="hl-identifier">XmlRootElement</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAccessorType</span><span class="hl-brackets">(</span><span class="hl-identifier">XmlAccessType</span><span class="hl-code">.</span><span class="hl-identifier">FIELD</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Pack</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-identifier">dog1</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(</span><span class="hl-number">5.0</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-identifier">dog2</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(</span><span class="hl-number">3.0</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-brackets">}</span> </pre></div> </div> <p>Now when we perform the marshalling process, we get the following XML:</p> <div class="code"> <div class="hl-main"> <pre> <span class="hl-brackets">&lt;</span><span class="hl-reserved">pack</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">dog1</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">height</span><span class="hl-brackets">&gt;</span><span class="hl-code">2.5</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">height</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">dog1</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">dog2</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">height</span><span class="hl-brackets">&gt;</span><span class="hl-code">1.5</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">height</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">dog2</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">pack</span><span class="hl-brackets">&gt;</span> </pre></div> </div> <p>by <span class="printuser avatarhover"><a href="http://www.wikidot.com/user:info/elishapeterson" ><!--[if gte IE 7]><!--><img class="small" src="http://www.wikidot.com/common--images/avatars/59/59229/a16.png" alt="elishapeterson" style="background-image:url(http://www.wikidot.com/userkarma.php?u=59229)" /><!--<![endif]--><!--[if lt IE 7]><img class="small" src="http://www.wikidot.com/common&#45;&#45;images/avatars/59/59229/a16.png" alt="elishapeterson" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=http://www.wikidot.com/userkarma.php?u=59229,sizingMethod='scale')"/><![endif]--></a><a href="http://www.wikidot.com/user:info/elishapeterson" >elishapeterson</a></span></p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://elishapeterson.wikidot.com/technotes:geogebra-cobwebs</guid>
				<title>Creating a Dynamic Cobwebbing Demonstration with Geogebra</title>
				<link>http://elishapeterson.wikidot.com/technotes:geogebra-cobwebs</link>
				<description>

&lt;p&gt;Elisha Peterson&lt;br /&gt;
United States Military Academy&lt;/p&gt;
&lt;p&gt;by &lt;span class=&quot;printuser avatarhover&quot;&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;&lt;!--[if gte IE 7]&gt;&lt;!--&gt;&lt;img class=&quot;small&quot; src=&quot;http://www.wikidot.com/common--images/avatars/59/59229/a16.png&quot; alt=&quot;elishapeterson&quot; style=&quot;background-image:url(http://www.wikidot.com/userkarma.php?u=59229)&quot; /&gt;&lt;!--&lt;![endif]--&gt;&lt;!--[if lt IE 7]&gt;&lt;img class=&quot;small&quot; src=&quot;http://www.wikidot.com/common&amp;#45;&amp;#45;images/avatars/59/59229/a16.png&quot; alt=&quot;elishapeterson&quot; style=&quot;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=http://www.wikidot.com/userkarma.php?u=59229,sizingMethod=&#039;scale&#039;)&quot;/&gt;&lt;![endif]--&gt;&lt;/a&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;elishapeterson&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
</description>
				<pubDate>Thu, 03 Jul 2008 20:40:46 +0000</pubDate>
												<content:encoded>
					<![CDATA[
						 <p>Elisha Peterson<br /> United States Military Academy</p> <div class="content-separator" style="display: none:"></div> <p>This is a tutorial describing how to use Geogebra to implement a cobwebbing demonstration. The techniques demonstrated can also be applied to many situations involving numerical and recursive techniques.</p> <div class="content-separator" style="display: none:"></div> <table style="margin:0; padding:0"> <tr> <td style="margin:0; padding:0"> <div id="toc"> <div id="toc-action-bar"><a href="javascript:;" >fold</a><a style="display: none" href="javascript:;" >unfold</a></div> <div class="title">Table of Contents</div> <div id="toc-list"> <div style="margin-left: 1em;"><a href="#toc0">Introduction</a></div> <div style="margin-left: 2em;"><a href="#toc1">How Cobwebbing Works</a></div> <div style="margin-left: 1em;"><a href="#toc2">Designing the First Iteration</a></div> <div style="margin-left: 2em;"><a href="#toc3">Method 1: Using Perpendicular Lines and Points of Intersection</a></div> <div style="margin-left: 2em;"><a href="#toc4">Method 2: Using Manual Input</a></div> <div style="margin-left: 1em;"><a href="#toc5">Subsequent Iterations</a></div> <div style="margin-left: 2em;"><a href="#toc6">Method 1: Tools</a></div> <div style="margin-left: 2em;"><a href="#toc7">Method 2: Sequence</a></div> <div style="margin-left: 1em;"><a href="#toc8">Exporting the Applet to the Web</a></div> <div style="margin-left: 2em;"><a href="#toc9">Exporting to the Web</a></div> <div style="margin-left: 2em;"><a href="#toc10">Adjusting the HTML and Implementing Javascript</a></div> <div style="margin-left: 1em;"><a href="#toc11">References</a></div> </div> </div> </td> </tr> </table> <div class="content-separator" style="display: none:"></div> <p><em><strong>NOTE:</strong> Readers unfamiliar with Geogebra may wish to refer to [<a href="javascript:;" class="bibcite" id="bibcite-531096-2-88641a" >2</a>] for a basic introduction. You can leave comments or questions at the bottom of the page.</em></p> <h1><span>Introduction</span></h1> <p><strong>Cobwebbing</strong> is a basic technique useful for visualizing solutions to simple recurrence equations. Given an initial value <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/3e0d691f3a530e6c7e079636f20c111b.png" alt="x_0" />, the recurrence <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/ecf9614a7a907548f1d307077d066838.png" alt="x_n=f(x_{n-1})" /> determines a sequence of points</p> <span class="equation-number">(1)</span> <div class="math-equation" id="equation-582800-1"><img src="http://elishapeterson.wikidot.com/local--math/eqs/1252290cda08e0480bb16c2989976d45.png" alt="x_0, x_1=f(x_0), x_2=f(x_1)=f(f(x_0)), \ldots, x_n=f(x_{n-1})=f(f(\cdots f(x_0)\cdots)), \ldots." /></div> <p>Typically, one is interested in the long-term behavior of this sequence. Does it converge? Does it approach a limit cycle? What else can you say about the sequence? Cobwebbing is a successful technique in large part because it provides a visual approach to determining the long-term behavior of the system. In particular, it can be used to show directly how the <em>slope</em> of the function <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/8fa14cdd754f91cc6554c9e71929cce7.png" alt="f" /> determines much of that behavior.</p> <p><strong>Geogebra</strong> is a free open-source mathematics tool designed for the interactive exploration of mathematics[<a href="javascript:;" class="bibcite" id="bibcite-531096-1-12632a" >1</a>]. Partly a dynamic <strong>geo</strong>metry platform, partly a computer al<strong>gebra</strong> system, it enables the creation of workbooks that beg for user interaction. This tutorial describes how to create a cobwebbing workbook that allows the user to change the initial point <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/3e0d691f3a530e6c7e079636f20c111b.png" alt="x_0" />, the recurrence function <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/8fa14cdd754f91cc6554c9e71929cce7.png" alt="f" />, and the number of iterations displayed on the cobwebbing plot. It covers some advanced techniques such as using the Iteration command, designing customized functions with Geogebra's <em>Tool</em> feature, and creating webpages that support Javascript interactions.</p> <h2><span>How Cobwebbing Works</span></h2> <p>How does cobwebbing work? First, plot the function <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/7c1c9491ba7c6e8d6d2cfa82e39b22ca.png" alt="y=f(x)" /> along with the diagonal line <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/5dbad057040ec6eb5aa5841786e25d33.png" alt="y=x" />. The resulting figure makes it immediately obvious whether any fixed points exist; <em>the number of intersections is precisely the number fixed points</em>. Second, mark the point <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/a07253042f49b06324f567f1195530c9.png" alt="(x_0,0)" /> and draw a <em>vertical</em> line from the point to the function (whether above or below the point). Third, draw a <em>horizontal</em> line from that point to the diagonal line. This results in a line segment from <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/a07253042f49b06324f567f1195530c9.png" alt="(x_0,0)" /> to <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/95a184f9dbcc23c56cd3c2743062a7e3.png" alt="(x_0,f(x_0))" />, and another from <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/95a184f9dbcc23c56cd3c2743062a7e3.png" alt="(x_0,f(x_0))" /> to <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/7a73a77dd26cfdf1103f470c7abffd9d.png" alt="(f(x_0),f(x_0))" />. Fourth, repeat this process starting with the point on the diagonal. Continuing in this manner, the <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/9dd4e461268c8034f5c8564e155c67a6.png" alt="x" />-values of the vertical segments correspond to the values <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/3776a491d03f5aac9065dfe81491ce12.png" alt="\{x_i\}" /> in the sequence. Figure 1 shows cobwebbing in action.</p> <div style="margin:auto; width:400px; font-size:80%"> <div class="image-container aligncenter"><a href="http://elishapeterson.wikidot.com/local--files/technotes:geogebra-cobwebs/cobwebbing.svg"><img src="http://elishapeterson.wikidot.com/local--resized-images/technotes:geogebra-cobwebs/cobwebbing.svg/medium.jpg" alt="cobwebbing.svg" class="image" /></a></div> <p><strong>Figure 1.</strong> Cobwebbing demonstrates that iterating the sequence <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/8b7fec693a8454209ef9a9e8c1671aac.png" alt="x_n=x_{n-1}(3-x_{n-1})" /> with initial condition <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/fe1a25aa1f9ee71867f1c452205690ef.png" alt="x_0=0.25" /> slowly approaches the fixed point <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/fd7cb86e63b91e7d4288a5779a19d27c.png" alt="x^*=2" />.</p> </div> <h1><span>Designing the First Iteration</span></h1> <div style="float:right; width:500px; margin-left:20px; font-size:80%"> <div style="text-align: center;"><a href="http://elishapeterson.wikidot.com/local--files/technotes:geogebra-cobwebs/cobweb1.jpg"><img src="http://elishapeterson.wikidot.com/local--resized-images/technotes:geogebra-cobwebs/cobweb1.jpg/medium.jpg" alt="cobweb1.jpg" class="image" /></a><br /> <strong>Figure 2.</strong> The function, an initial point, and the <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/5dbad057040ec6eb5aa5841786e25d33.png" alt="y=x" /> line.</div> </div> <br /> We'll start by inputting a function and the initial point <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/a07253042f49b06324f567f1195530c9.png" alt="(x_0,0)" />. Type <div class="code"> <pre> <code>f(x)=3x-x^2</code> </pre></div> <br /> into the input line. Next, type in <tt>y=x</tt> to draw the diagonal line. Finally, select the <img src="http://www.geogebra.org/help/docuen/topics/doc-Dateien/image032.gif" alt="image032.gif" class="image" /> <strong>Point</strong> tool and click somewhere on the x-axis (not too close to any intersections with other objects). This ensures the point is constrained to move on the axis. Check the constraint by hitting <tt>Esc</tt> and moving the point around. <p>At this point, you can change the appearances or names of any of the objects, by right-clicking on the object and selecting "Properties". You should end up with something similar to Figure 2.</p> <div style="clear:right; height: 0px; font-size: 1px"></div> <p>We can now implement the <em>cobwebbing</em> technique. There are a few ways to generate additional points that move when the initial point is shifted. We demonstrate two different methods. In what follows, we assume the initial point is named "<tt>A</tt>", the function is named "<tt>f</tt>", and the line <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/5dbad057040ec6eb5aa5841786e25d33.png" alt="y=x" /> is named "<tt>a</tt>".</p> <h2><span>Method 1: Using Perpendicular Lines and Points of Intersection</span></h2> <div style="float:right; width:500px; margin-left:20px; font-size:80%"> <div style="text-align: center;"><a href="http://elishapeterson.wikidot.com/local--files/technotes:geogebra-cobwebs/cobweb3.jpg"><img src="http://elishapeterson.wikidot.com/local--resized-images/technotes:geogebra-cobwebs/cobweb3.jpg/medium.jpg" alt="cobweb3.jpg" class="image" /></a><br /> <strong>Figure 3.</strong> Creation of horizontal and vertical lines for the "up and over" cobweb construction.</div> </div> <br /> The first method uses standard toolbar features. <ol> <li>Select the <strong>Perpendicular line</strong> <img src="http://www.geogebra.org/help/docuen/topics/doc-Dateien/image044.gif" alt="image044.gif" class="image" /> tool, click on the point <tt>A</tt>, and then click on the x-axis. This creates a vertical line.</li> <li>With the same tool selected, move your mouse to the intersection of the vertical line and the function <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/50bbd36e1fd2333108437a2ca378be62.png" alt="f(x)" />. Once both pieces are highlighted (indicated by an increase in line thickness), click to select the point of intersection. Then, click on the y-axis. This creates a horizontal line through the point <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/95a184f9dbcc23c56cd3c2743062a7e3.png" alt="(x_0,f(x_0))" />.</li> <li>Create a point at the intersection of the horizontal line and the diagonal line <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/5dbad057040ec6eb5aa5841786e25d33.png" alt="y=x" /> by selecting the <strong>Point</strong> tool and clicking the point of intersection.</li> </ol> <p>Check that the dynamic structure is working properly by adjusting the point <tt>A</tt>. The additional lines should also move. The points that are <em>free</em> appear colored, while points that are not free appear black. The workbook should look like Figure 3.</p> <div style="clear:right; height: 0px; font-size: 1px"></div> <p>You could leave the lines as is, but it's probably better to visualize segments than lines. My trick for fixing this problem follows. First, hide the lines by right-clicking on each and deselecting the "Show object" option. Second, use the <strong>Segment</strong> <img src="http://www.geogebra.org/help/docuen/topics/doc-Dateien/image037.gif" alt="image037.gif" class="image" /> tool to add segments between each pair of points. Alternately, one could use the <strong>Vector</strong> <img src="http://www.geogebra.org/help/docuen/topics/doc-Dateien/image035.gif" alt="image035.gif" class="image" /> tool to display segments with arrows.</p> <p>I finish by cleaning up the workbook. Right-click on the objects whose labels are unnecessary and deselect the "Show label" option. This is also a good time to change the visual appearance of the figure. If you want to go with a minimalist approach, you may even wish to hide the displayed points and just display the lines. One useful trick is the <strong>Copy visual style</strong> tool, which allows you to quickly duplicate a visual style that you've created. See Figure 4 for my result.</p> <h2><span>Method 2: Using Manual Input</span></h2> <p>The second method uses direct input, and minimizes the creation of unnecessary features like the horizontal and vertical lines in the above. Here is the process:</p> <ol> <li>Type <tt>Segment[A,(x(A),f(x(A)))]</tt> into the input bar. This creates the desired vertical segment. Note that "<tt>x(A)</tt>" indicates the value of the x-coordinate of the point <tt>A</tt>.</li> <li>Create the horizontal segment by typing in <tt>Segment[(x(A),f(x(A))),(f(x(A)),f(x(A)))]</tt>.</li> <li>Turn off the labels on the segments, and change the appearance as desired.</li> </ol> <p>Visually, this provides exactly the same output as the first result, as seen in Figure 4.</p> <div style="float:left; width:50%; font-size:80%"> <div style="text-align: center;"><a href="http://elishapeterson.wikidot.com/local--files/technotes:geogebra-cobwebs/cobweb4.jpg"><img src="http://elishapeterson.wikidot.com/local--resized-images/technotes:geogebra-cobwebs/cobweb4.jpg/small.jpg" alt="cobweb4.jpg" class="image" /></a><br /> <strong>Figure 4.</strong> Clean version of the perpendicular-line construction.</div> </div> <div style="float:right; width:50%; font-size:80%"> <div style="text-align: center;"><a href="http://elishapeterson.wikidot.com/local--files/technotes:geogebra-cobwebs/cobweb5.jpg"><img src="http://elishapeterson.wikidot.com/local--resized-images/technotes:geogebra-cobwebs/cobweb5.jpg/small.jpg" alt="cobweb5.jpg" class="image" /></a><br /> <strong>Figure 5.</strong> Cobweb construction using direct algebraic input.</div> </div> <div style="clear:both; height: 0px; font-size: 1px"></div> <h1><span>Subsequent Iterations</span></h1> <p>You could repeat either process to generate several steps of the cobweb diagram, but it's easier to automate the process. We describe two methods for iterating the process, which illustrate some of the most useful features of Geogebra.</p> <h2><span>Method 1: Tools</span></h2> <p>The first technique is to create a <strong>Tool</strong>. A tool is a customized function (or macro) in which several output objects are dependent on several input objects. Most of the functions we have used thus far are tools. Geogebra permits users to design their own tools as well.</p> <p>The tool created here will have as inputs the function and initial point, and as outputs the vertical and horizontal cobweb lines. You must have completed one of the methods above before beginning this section. See Figure 6 for a visual of this process.</p> <ol> <li>Select "Tools»Create New Tool…" from the menu. You select the outputs first. Select the vertical and horizontal segments corresponding to the first iteration.</li> <li>For the inputs, select the function and the point <tt>A</tt>. The order in which these are listed corresponds to the order in which they must be selected to use the tool later, so be sure they are ordered properly. I prefer to select the point first, so I moved the point to the top of the list.</li> <li>Give the function a name (like "CobwebIteration"), and for help type in "<tt>Point, Function</tt>". This provides a useful reminder of the order of inputs when the tool is selected.</li> </ol> <div style="width:750px; margin:auto; font-size:80%"><a href="http://elishapeterson.wikidot.com/local--files/technotes:geogebra-cobwebs/tool1.jpg"><img src="http://elishapeterson.wikidot.com/local--resized-images/technotes:geogebra-cobwebs/tool1.jpg/small.jpg" alt="tool1.jpg" class="image" /></a> <a href="http://elishapeterson.wikidot.com/local--files/technotes:geogebra-cobwebs/tool2.jpg"><img src="http://elishapeterson.wikidot.com/local--resized-images/technotes:geogebra-cobwebs/tool2.jpg/small.jpg" alt="tool2.jpg" class="image" /></a> <a href="http://elishapeterson.wikidot.com/local--files/technotes:geogebra-cobwebs/tool3.jpg"><img src="http://elishapeterson.wikidot.com/local--resized-images/technotes:geogebra-cobwebs/tool3.jpg/small.jpg" alt="tool3.jpg" class="image" /></a><br /> <div style="text-align: center;"> <p><strong>Figure 6.</strong> Creating the <strong>CobwebIteration</strong> tool.</p> </div> </div> <div style="clear:both; height: 0px; font-size: 1px"></div> <p>After clicking "Finish", the tool shows up in the normal toolbar. You can use the tool to perform further cobweb iterations by selecting the point of intersection of the last segment with the <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/5dbad057040ec6eb5aa5841786e25d33.png" alt="y=x" /> line, and then the function. Figure 7 demonstrates the result after several iterations. Note the new button on the right side of the toolbar. You can also save the tool just created, for use in other workbooks, by selecting "Tools»Manage Tools…" from the menu.</p> <div style="margin:auto; width:500px; font-size:80%"><a href="http://elishapeterson.wikidot.com/local--files/technotes:geogebra-cobwebs/cobweb6.jpg"><img src="http://elishapeterson.wikidot.com/local--resized-images/technotes:geogebra-cobwebs/cobweb6.jpg/medium.jpg" alt="cobweb6.jpg" class="image" /></a><br /> <div style="text-align: center;"> <p><strong>Figure 7.</strong> Iterated cobweb construction with a user-created tool.</p> </div> </div> <p>The workbook is now fully functional as a demonstration of the cobwebbing technique. The author's versions are attached to this page<sup class="footnoteref"><a id="footnoteref-873347-1" href="javascript:;" class="footnoteref" >1</a></sup>. In the next section, we'll make the demo a little more powerful by allowing the user to change the number of iterations dynamically.</p> <h2><span>Method 2: Sequence</span></h2> <p>Geogebra also has the functionality to construct a sequence of objects which depend on some input parameter. This requires knowing a little more of the command line structure, but offers the added functionality of being able to choose the desired number of iterations.</p> <p>There are two types of iteration processes available: <tt>Sequence</tt>, which produces a list of objects indexed by a particular value, and <tt>Iteration</tt>, which produces a list of numbers formed by iterating a single function.</p> <p>Be sure the function <tt>f</tt>, the point <tt>A</tt>, and the line <img class="math-inline" src="http://elishapeterson.wikidot.com/local--math/inline/5dbad057040ec6eb5aa5841786e25d33.png" alt="y=x" /> have all been defined. We first create a slider that can be used to dynamically change the number of iterations displayed on the window. Begin by selecting the <strong>Slider</strong> <img src="http://www.geogebra.org/help/docuen/topics/doc-Dateien/image061.gif" alt="image061.gif" class="image" /> tool and clicking somewhere in the window. Rename the slider "<tt>numIterations</tt>". Then set the minimum value to <tt>1</tt>, the maximum value to <tt>50</tt>, and the increment to <tt>1</tt>. Next, type</p> <div class="code"> <pre> <code>values = IterationList[f,x(A),numIterations]</code> </pre></div> <br /> into the input line. This should create a list of several values with the name "<tt>values</tt>". To create the horizontal and vertical segments, use the <tt>Sequence</tt> command. Type the following three lines into the input: <div class="code"> <pre> <code>Segment[A,(x(A),f(x(A)))] cobwebHorizontal = Sequence[ Segment[(Element[values, i], Element[values, i+1]), (Element[values, i+1], Element[values, i+1])], i, 1, numIterations-1] cobwebVertical = Sequence[ Segment[(Element[values, i], Element[values, i]), (Element[values, i], Element[values, i+1])], i, 2, numIterations]</code> </pre></div> <br /> Now you should be able to adjust the number of cobweb iterations by moving the slider. Figure 8 shows the resulting figure. The Geogebra workbook generated by the author using the <tt>Sequence</tt> and <tt>Iteration</tt> commands is attached below (<a href="http://elishapeterson.wikidot.com/local--files/technotes:geogebra-cobwebs/CobwebIteration-Sequence.ggb">CobwebIteration-Sequence.ggb</a>). <div style="margin:auto; width:500px; font-size:80%"><a href="http://elishapeterson.wikidot.com/local--files/technotes:geogebra-cobwebs/cobweb7.jpg"><img src="http://elishapeterson.wikidot.com/local--resized-images/technotes:geogebra-cobwebs/cobweb7.jpg/medium.jpg" alt="cobweb7.jpg" class="image" /></a><br /> <div style="text-align: center;"> <p><strong>Figure 8.</strong> Cobweb construction with slider, allowing the user to change the number of iterations.</p> </div> </div> <p>At this point, the user could export the displayed figure as an image file, creating beautiful images of cobwebbing. Figure 1 was exported from Geogebra as an <em>SVG</em>, or "Scalable Vector Graphics" file. This type of file can be displayed at any resolution without loss of image quality.</p> <h1><span>Exporting the Applet to the Web</span></h1> <p>Geogebra allows you to easily post workbooks that you create to a website. This section describes how to do this, and how to use Javascript to provide the user with a textbox to input the function directly.</p> <h2><span>Exporting to the Web</span></h2> <div style="float:right; width:500px; margin-left:20px; font-size:80%"> <div style="text-align: center;"><a href="http://elishapeterson.wikidot.com/local--files/technotes:geogebra-cobwebs/export1.jpg"><img src="http://elishapeterson.wikidot.com/local--resized-images/technotes:geogebra-cobwebs/export1.jpg/medium.jpg" alt="export1.jpg" class="image" /></a><br /> <strong>Figure 9.</strong> Exporting a Worksheet to HTML.</div> </div> <br /> Exporting to the web using Geogebra is a simple process. Go to "<tt>File»Export»Dynamic Worksheet as Webpage (html)</tt>" or press <tt>Ctrl+Shift+W</tt>. This loads a template, shown in Figure 9, which allows you to fill in text before and after the worksheet. Under the "<tt>Advanced</tt>" tab, you can change the displayed size of the worksheet, control whether or not the menu and toolbars are displayed, and so on. <p><em><strong>Warning</strong>: It is best to resize the window to your desired size</em> before <em>exporting. Otherwise, the change in size may not display all the objects in your worksheet properly, or may not display some of them at all.</em></p> <p>Once you click "<tt>Export</tt>", an <tt>html</tt> file will be created along with a new worksheet file <tt>.ggb</tt> and two <tt>.jar</tt> files that are required for Java to run the program: <tt>geogebra.jar</tt> and <tt>geogebra_properties.jar</tt>. In order for the applet to work, you will need to upload all four files to a web folder, and link to the <tt>html</tt> file. Note that in some cases the <tt>.ggb</tt> files will not load properly because the file type is not "enabled" on the server. If this comes up, you can ask IT to permit you to post <tt>.ggb</tt> files. The author's version is posted <a href="http://www.dean.usma.edu/math/people/Peterson/geogebra/CobwebIteration_Sequence.html">here</a>.</p> <div style="clear:right; height: 0px; font-size: 1px"></div> <h2><span>Adjusting the HTML and Implementing Javascript</span></h2> <p>After the <tt>html</tt> file has been created, it can be modified directly or with an <tt>html</tt> editor. One can add additional titles, links, and text, or update the appearance.</p> <p>One particularly nice feature of <em>Geogebra</em> is that it can interface with <em>Javascript</em>. We'll create a simple form that the user can use to input a new function, similar to that shown in Figure 10. The Javascript code required is minimal. For further details on the Geogebra/Javascript interface, see [<a href="javascript:;" class="bibcite" id="bibcite-531096-5-66346a" >5</a>, <a href="javascript:;" class="bibcite" id="bibcite-531096-6-42091a" >6</a>].</p> <div style="margin:auto; width:600px; font-size:80%"> <div class="image-container aligncenter"><img src="http://elishapeterson.wikidot.com/local--files/technotes:geogebra-cobwebs/formInput.jpg" alt="formInput.jpg" class="image" /></div> <div style="text-align: center;"> <p><strong>Figure 10.</strong> Javascript form input used to interface with Geogebra cobwebbing worksheet.</p> </div> </div> <p>To implement the form, I inserted the following <tt>html</tt> code block immediately before the applet:</p> <div class="code"> <div class="hl-main"> <pre> <span class="hl-brackets">&lt;</span><span class="hl-reserved">form</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">p</span><span class="hl-code"> </span><span class="hl-var">align</span><span class="hl-code">=</span><span class="hl-var">center</span><span class="hl-brackets">&gt;</span><span class="hl-code"> Change function: </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">b</span><span class="hl-brackets">&gt;</span><span class="hl-code">f(x)=</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">b</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">input</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">text</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">T1</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">size</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">20</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">value</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">3x-x^2</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">input</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">button</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">value</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">Submit</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">B1</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">onclick</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">document.ggbApplet.evalCommand('f(x)='+T1.value);</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">input</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">button</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">value</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">Reset</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">B2</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">onclick</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">T1.value='3x-x^2';document.ggbApplet.reset();</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">p</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">form</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">applet</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">ggbApplet</span><span class="hl-quotes">"</span><span class="hl-code"> ....</span> </pre></div> </div> <br /> The two commands used to interface with Geogebra are <tt>evalComand()</tt>, which takes as argument a <tt>String</tt> and is equivalent to typing a command at the Input line, and <tt>reset()</tt>, which restores the applet to its initial state. The author's applet with user-input form is posted <a href="http://www.dean.usma.edu/math/people/Peterson/geogebra/CobwebIteration_Sequence_2.html">here</a>. <h1><span>References</span></h1> <p>Much more is possible using Geogebra. An outstanding resource is the online wiki [<a href="javascript:;" class="bibcite" id="bibcite-531096-4-22297a" >4</a>]. My collection of worksheets and webpages created with Geogebra is available at [<a href="javascript:;" class="bibcite" id="bibcite-531096-7-44796a" >7</a>].</p> <div class="bibitems"> <div class="bibitem" id="bibitem-531096-1">1. <strong>Geogebra</strong> (application), available by download or Java WebStart at <a href="http://www.geogebra.org">http://www.geogebra.org</a>.</div> <div class="bibitem" id="bibitem-531096-2">2. <strong>Geogebra Documentation</strong>, available at <a href="http://www.geogebra.org/help/docuen/index.html">http://www.geogebra.org/help/docuen/index.html</a></div> <div class="bibitem" id="bibitem-531096-3">3. <strong>Creating Mathlets with Open-Source Tools</strong>, Markus Hohenwarter and Judith Preiner, available at <a href="http://www.maa.org/joma/Volume7/Hohenwarter2/index.html">http://www.maa.org/joma/Volume7/Hohenwarter2/index.html</a></div> <div class="bibitem" id="bibitem-531096-4">4. <strong>Geogebra Wiki</strong>, <a href="http://www.geogebra.org/en/wiki/index.php/Main_Page">http://www.geogebra.org/en/wiki/index.php/Main_Page</a></div> <div class="bibitem" id="bibitem-531096-5">5. <strong>Geogebra Applets and Javascript</strong>, available at <a href="http://www.geogebra.org/source/program/applet/geogebra_applet_javascript.html">http://www.geogebra.org/source/program/applet/geogebra_applet_javascript.html</a></div> <div class="bibitem" id="bibitem-531096-6">6. <em>Creating Interactive Exercises using JavaScript</em>, Markus Hohenwarter and Judith Preiner, available at <a href="http://www.maa.org/joma/Volume7/Hohenwarter2/javascript.html">http://www.maa.org/joma/Volume7/Hohenwarter2/javascript.html</a></div> <div class="bibitem" id="bibitem-531096-7">7. Collection of Geogebra webpages and worksheets, available at <a href="http://www.dean.usma.edu/math/people/Peterson/geogebra/mathlets.html">http://www.dean.usma.edu/math/people/Peterson/geogebra/mathlets.html</a></div> </div> <p>by <span class="printuser avatarhover"><a href="http://www.wikidot.com/user:info/elishapeterson" ><!--[if gte IE 7]><!--><img class="small" src="http://www.wikidot.com/common--images/avatars/59/59229/a16.png" alt="elishapeterson" style="background-image:url(http://www.wikidot.com/userkarma.php?u=59229)" /><!--<![endif]--><!--[if lt IE 7]><img class="small" src="http://www.wikidot.com/common&#45;&#45;images/avatars/59/59229/a16.png" alt="elishapeterson" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=http://www.wikidot.com/userkarma.php?u=59229,sizingMethod='scale')"/><![endif]--></a><a href="http://www.wikidot.com/user:info/elishapeterson" >elishapeterson</a></span></p> <div class="footnotes-footer"> <div class="title">Footnotes</div> <div class="footnote-footer" id="footnote-873347-1"><a href="javascript:;" >1</a>. The Geogebra workbook generated by the author using this method is attached to this page (<a href="http://elishapeterson.wikidot.com/local--files/technotes:geogebra-cobwebs/CobwebIteration-Tool.ggb">CobwebIteration-Tool.ggb</a>). The custom tool is also attached (<a href="http://elishapeterson.wikidot.com/local--files/technotes:geogebra-cobwebs/CobwebIterationTool.ggt">CobwebIterationTool.ggt</a>).</div> </div> 
				 	]]>
				</content:encoded>							</item>
				</channel>
</rss>