<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jappit.com &#187; tutorial</title>
	<atom:link href="http://www.jappit.com/blog/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jappit.com/blog</link>
	<description>Mobile blog by Alessandro La Rosa</description>
	<lastBuildDate>Wed, 23 Nov 2011 10:38:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How to build a CoverFlow component with QML</title>
		<link>http://www.jappit.com/blog/2011/02/21/how-to-build-a-coverflow-component-with-qml/</link>
		<comments>http://www.jappit.com/blog/2011/02/21/how-to-build-a-coverflow-component-with-qml/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 15:07:43 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[cool stuff]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[maemo]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[nokia]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[coverflow]]></category>
		<category><![CDATA[forum nokia]]></category>
		<category><![CDATA[qml]]></category>
		<category><![CDATA[qt]]></category>
		<category><![CDATA[qt quick]]></category>
		<category><![CDATA[wiki]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=1667</guid>
		<description><![CDATA[Today I&#8217;ve published a new technical article on Forum Nokia Wiki, that shows how a CoverFlow component can be easily built with Qt Quick and QML. You can see a video of the CoverFlow component in action on a Nokia N8 device below. The full source code is available here: Building a CoverFlow component with [...]]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;ve published a new technical article on <a title="Forum Nokia Wiki" href="http://wiki.forum.nokia.com">Forum Nokia Wiki</a>, that shows <a title="Qt Quick CoverFlow component" href="http://wiki.forum.nokia.com/index.php/Building_a_CoverFlow_component_with_QML">how a <strong>CoverFlow component</strong> can be easily built with Qt Quick and QML</a>.</p>
<p><a href="http://wiki.forum.nokia.com/index.php/Building_a_CoverFlow_component_with_QML"><img class="aligncenter size-full wp-image-1668" title="QML CoverFlow" src="http://www.jappit.com/blog/wp-content/blog_n8_qml_coverflow_front.png" alt="" width="580" height="293" /></a></p>
<p>You can <a href="http://www.youtube.com/watch?v=X8kvlPNBLeg">see a video</a> of the CoverFlow component in action on a <strong>Nokia N8</strong> device below.</p>
<div style="width: 560px; margin: auto;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="349" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/X8kvlPNBLeg?fs=1&amp;hl=en_US" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="349" src="http://www.youtube.com/v/X8kvlPNBLeg?fs=1&amp;hl=en_US" allowscriptaccess="always" allowfullscreen="true"></embed></object></div>
<p>The <strong>full source code</strong> is available here: <a title="Qt Quick CoverFlow component" href="http://wiki.forum.nokia.com/index.php/Building_a_CoverFlow_component_with_QML">Building a CoverFlow component with QML</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2011/02/21/how-to-build-a-coverflow-component-with-qml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Learning Qt: Beta Labs Example shows how to use QtWebKit</title>
		<link>http://www.jappit.com/blog/2010/01/29/learning-qt-beta-labs-example-shows-how-to-use-qtwebkit/</link>
		<comments>http://www.jappit.com/blog/2010/01/29/learning-qt-beta-labs-example-shows-how-to-use-qtwebkit/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 14:37:29 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[post]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[forum nokia]]></category>
		<category><![CDATA[forumnokia]]></category>
		<category><![CDATA[qt]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/2010/01/29/learning-qt-beta-labs-example-shows-how-to-use-qtwebkit/</guid>
		<description><![CDATA[If you&#8217;re starting learning Qt to develop your next mobile applications, or if you plan to do that (..you should ), you should definitly check out this good tutorial from Forum Nokia: QtWebKit: Beta Labs Example.]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re starting learning <a href="http://qt.nokia.com/" title="Qt">Qt</a> to develop your next mobile applications, or if you plan to do that (..you should <img src='http://www.jappit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ), you should definitly check out this good tutorial from <a href="http://www.forum.nokia.com" title="Forum Nokia">Forum Nokia</a>: <a title="qtwebkit beta labs example" href="http://www.forum.nokia.com/info/sw.nokia.com/id/f12cce87-430e-49b0-81b1-0ec1b07cdb1c/QtWebKit_Beta_Labs_Example.html">QtWebKit: Beta Labs Example</a>.<br />
<img src="http://www.jappit.com/blog/wp-content/qtwebkit_beta_labs_example.png" alt="qtwebkit beta labs example" title="qtwebkit beta labs example" width="500" height="248" class="alignnone size-full wp-image-740" /></p>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2010/01/29/learning-qt-beta-labs-example-shows-how-to-use-qtwebkit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mobilising websites: guidelines for WRT Widgets development, part 2</title>
		<link>http://www.jappit.com/blog/2009/05/21/mobilising-websites-guidelines-for-wrt-widgets-development-part-2/</link>
		<comments>http://www.jappit.com/blog/2009/05/21/mobilising-websites-guidelines-for-wrt-widgets-development-part-2/#comments</comments>
		<pubDate>Thu, 21 May 2009 12:53:59 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[cool stuff]]></category>
		<category><![CDATA[nokia]]></category>
		<category><![CDATA[symbian]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web runtime]]></category>
		<category><![CDATA[forumnokia]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[s60]]></category>
		<category><![CDATA[widgets]]></category>
		<category><![CDATA[wrt]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=429</guid>
		<description><![CDATA[Some days ago I wrote about WRT development, and listed some first considerations that I usually face when thinking about a new Web Runtime Widget. Today I continue this series with other points, hoping these guidelines could help other WRT developers as well. To read the full article, check out my blog on Forum Nokia: [...]]]></description>
			<content:encoded><![CDATA[<p>Some days ago <a title="Mobilising websites: guidelines for WRT Widgets development, part 1" href="http://www.jappit.com/blog/2009/05/19/mobilising-websites-guidelines-for-wrt-widgets-development-part-1/">I wrote about WRT development</a>, and listed some first considerations that I usually face when thinking about a new <strong>Web Runtime Widget</strong>.</p>
<p style="text-align: center;"><img class="aligncenter" title="STEW Web Runtime example Widget" src="http://www.jappit.com/images/blog/uploads/stew_home.jpg" alt="" width="430" height="246" /></p>
<p>Today I continue this series with other points, <strong>hoping these guidelines could help other WRT developers</strong> as well.</p>
<p>To <strong>read the full article</strong>, <a title="http://blogs.forum.nokia.com/blog/alessandro-la-rosas-forum-nokia-blog/2009/05/21/mobilising-websites-guidelines-for-wrt-widgets-development-part-2" href="http://blogs.forum.nokia.com/blog/alessandro-la-rosas-forum-nokia-blog">check out my blog</a> on <a title="Forum Nokia" href="http://www.forum.nokia.com" target="_blank">Forum Nokia</a>:</p>
<ul>
<li><a title="Mobilising websites: guidelines for WRT Widgets development, part 2" href="http://blogs.forum.nokia.com/blog/alessandro-la-rosas-forum-nokia-blog/2009/05/21/mobilising-websites-guidelines-for-wrt-widgets-development-part-2">Mobilising websites: guidelines for WRT Widgets development, part 2</a></li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2009/05/21/mobilising-websites-guidelines-for-wrt-widgets-development-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a dynamic fisheye menu in Flash Lite</title>
		<link>http://www.jappit.com/blog/2009/02/19/building-a-dynamic-fisheye-menu-in-flash-lite/</link>
		<comments>http://www.jappit.com/blog/2009/02/19/building-a-dynamic-fisheye-menu-in-flash-lite/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 12:43:13 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[cool stuff]]></category>
		<category><![CDATA[flash lite]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[fisheye menu]]></category>
		<category><![CDATA[forumnokia]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=304</guid>
		<description><![CDATA[Some time ago, we&#8217;ve seen how to build a fisheye menu with J2ME. Now, it&#8217;s time to see how to create the same component with Flash Lite. The FisheyeMenu source code Step 1. The menu MovieClip and external class Let&#8217;s create the FisheyeMenu ActionScript class, that will extend MovieClip, that will be used to implement [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago, we&#8217;ve seen <a title="J2ME Fisheye menu" href="http://www.jappit.com/blog/2008/04/24/building-a-fisheye-menu-in-j2me-with-jsr-226/" target="_blank">how to build a <strong>fisheye menu</strong> with J2ME</a>. Now, it&#8217;s time to see how to create the same component with <a title="Flash Lite website" href="http://www.adobe.com/products/flashlite/" target="_blank">Flash Lite</a>.</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="300" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://vimeo.com/moogaloop.swf?clip_id=3268318&#038;server=vimeo.com&#038;show_title=1&#038;show_byline=1&#038;show_portrait=0&#038;color=&#038;fullscreen=1" /><embed type="application/x-shockwave-flash" width="400" height="300" src="http://vimeo.com/moogaloop.swf?clip_id=3268318&#038;server=vimeo.com&#038;show_title=1&#038;show_byline=1&#038;show_portrait=0&#038;color=&#038;fullscreen=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<h3>The FisheyeMenu source code</h3>
<h4>Step 1. The menu MovieClip and external class</h4>
<p>Let&#8217;s create the <strong>FisheyeMenu ActionScript class</strong>, that will extend <strong>MovieClip</strong>, that will be used to implement the actual <strong>menu logic</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> FisheyeMenu <span style="color: #000000; font-weight: bold;">extends</span> MovieClip
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then, create an <strong>empty movie clip</strong> in your library, <strong>export</strong> it, and <strong>associate it with the FisheyeMenu class</strong>.</p>
<p><img class="alignnone" title="Flash Lite fisheye menu empty movieclip" src="http://www.jappit.com/images/blog/uploads/fl_fisheye_create_mc.gif" alt="" width="429" height="354" /></p>
<h4>Step 2. Initializing the menu</h4>
<p>First, define these <strong>4 menu properties</strong>, that will hold some useful values:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// focus index of the selected menu item</span>
var focusedIndex<span style="color: #339933;">:</span><span style="color: #003399;">Number</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// total number of menu items</span>
var itemsNum<span style="color: #339933;">:</span><span style="color: #003399;">Number</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// width of single menu items (in pixels)</span>
var itemWidth<span style="color: #339933;">:</span><span style="color: #003399;">Number</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// the MovieClip that will contain the menu items</span>
var itemsContainer<span style="color: #339933;">:</span>MovieClip<span style="color: #339933;">;</span></pre></div></div>

<p>Let&#8217;s also define an <strong>utility function</strong> that returns the <strong>currently focused item index</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> function getFocusedIndex<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">focusedIndex</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And then, implement a function that will be used to <strong>initialize the menu</strong> with the items you want.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> function initializeMenu<span style="color: #009900;">&#40;</span>itemIds<span style="color: #339933;">:</span><span style="color: #003399;">Array</span>, itemWidth<span style="color: #339933;">:</span><span style="color: #003399;">Number</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">itemsNum</span> <span style="color: #339933;">=</span> itemIds.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">focusedIndex</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">itemWidth</span> <span style="color: #339933;">=</span> itemWidth<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">initItems</span><span style="color: #009900;">&#40;</span>itemIds<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">private</span> function initItems<span style="color: #009900;">&#40;</span>itemIds<span style="color: #339933;">:</span><span style="color: #003399;">Array</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">itemsContainer</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">createEmptyMovieClip</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'itemsContainer'</span>, <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getNextHighestDepth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>var i<span style="color: #339933;">:</span><span style="color: #003399;">Number</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> itemIds.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		var item<span style="color: #339933;">:</span>MovieClip <span style="color: #339933;">=</span> itemsContainer.<span style="color: #006633;">attachMovie</span><span style="color: #009900;">&#40;</span>itemIds<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>, <span style="color: #0000ff;">'item_'</span> <span style="color: #339933;">+</span> i, itemsContainer.<span style="color: #006633;">getNextHighestDepth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #009900;">&#123;</span>_x<span style="color: #339933;">:</span> itemWidth <span style="color: #339933;">*</span> i, _y<span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			item._xscale <span style="color: #339933;">=</span> <span style="color: #cc66cc;">50</span><span style="color: #339933;">;</span>
			item._yscale <span style="color: #339933;">=</span> <span style="color: #cc66cc;">50</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The <em>initializeMenu()</em> function is the function you will call to initialize your fisheye menu with the items you want. Its arguments are:</p>
<ul>
<li>an <em>Array</em> containing the <strong>id of MovieClip symbols</strong> to be used as items</li>
<li>the <strong>width of single menu items</strong></li>
</ul>
<p>Once called, <em>initializeMenu()</em> <strong>initializes the menu properties</strong> and then calls the <em>initItems()</em> function, that will actually <strong>attach the item instances</strong>, <strong>scaling down</strong> the unselected items and <strong>translating</strong> the menu itself to its starting position.</p>
<p>The <em>getMenuLeft()</em> function returns the x position to be used for the <em>itemsContainer</em> <em>MovieClip</em>, and depends on the focused item index:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> function getMenuLeft<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #003399;">Number</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #339933;">-</span> itemWidth <span style="color: #339933;">*</span> focusedIndex<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4>Step 3. Implement sliding funcionality</h4>
<p>When the <strong>user presses LEFT and RIGHT keys</strong>, you want the menu to perform these steps:</p>
<ul>
<li><strong>change the focused item</strong>, scaling down the previously focused one, and scaling up the new</li>
<li><strong>translate the menu</strong> to be centered on the new focused item</li>
</ul>
<p>In <strong>ActionScript</strong>, you can do it  this way:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> function shiftItem<span style="color: #009900;">&#40;</span>itemDelta<span style="color: #339933;">:</span><span style="color: #003399;">Number</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	var nextIndex<span style="color: #339933;">:</span><span style="color: #003399;">Number</span> <span style="color: #339933;">=</span> focusedIndex <span style="color: #339933;">+</span> itemDelta<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>nextIndex <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> nextIndex <span style="color: #339933;">&lt;</span> itemsNum<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		scaleItem<span style="color: #009900;">&#40;</span>focusedIndex, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		focusedIndex <span style="color: #339933;">=</span> nextIndex<span style="color: #339933;">;</span>
&nbsp;
		scaleItem<span style="color: #009900;">&#40;</span>focusedIndex, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		moveMenu<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">private</span> function moveMenu<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #003399;">Void</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">new</span> Tween<span style="color: #009900;">&#40;</span>itemsContainer, <span style="color: #0000ff;">&quot;_x&quot;</span>, None.<span style="color: #006633;">easeNone</span>, itemsContainer._x, getMenuLeft<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, .50, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">private</span> function scaleItem<span style="color: #009900;">&#40;</span>itemIndex<span style="color: #339933;">:</span><span style="color: #003399;">Number</span>, scaleDown<span style="color: #339933;">:</span><span style="color: #003399;">Boolean</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #003399;">Void</span>
<span style="color: #009900;">&#123;</span>
	var item<span style="color: #339933;">:</span>MovieClip <span style="color: #339933;">=</span> itemsContainer<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'item_'</span> <span style="color: #339933;">+</span> itemIndex<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	var fromScale<span style="color: #339933;">:</span><span style="color: #003399;">Number</span> <span style="color: #339933;">=</span> scaleDown <span style="color: #339933;">?</span> <span style="color: #cc66cc;">100</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">50</span><span style="color: #339933;">;</span>
	var toScale<span style="color: #339933;">:</span><span style="color: #003399;">Number</span> <span style="color: #339933;">=</span> scaleDown <span style="color: #339933;">?</span> <span style="color: #cc66cc;">50</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">new</span> Tween<span style="color: #009900;">&#40;</span>item, <span style="color: #0000ff;">&quot;_xscale&quot;</span>, None.<span style="color: #006633;">easeNone</span>, fromScale, toScale, .50, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">new</span> Tween<span style="color: #009900;">&#40;</span>item, <span style="color: #0000ff;">&quot;_yscale&quot;</span>, None.<span style="color: #006633;">easeNone</span>, fromScale, toScale, .50, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this code snippet, there are <strong>3 functions</strong>:</p>
<ul>
<li><em>shiftItem()</em> is the function <strong>called to change the focused Item</strong> index by the passed delta argument. It checks if the change is ok, and then calls the following 2 functions:</li>
<li><em>moveMenu()</em> actually <strong>translates the items container</strong>, to have the new focused item horizontally centered</li>
<li><em>scaleItem()</em> <strong>scales up or down</strong>, depending on the scaleDown argument, the item corresponding at the index passed as argument</li>
</ul>
<p>Since here we use the <em>Tween </em>class, we have to add these <strong>2 import lines</strong> at the beginning of the <em>ActionScript</em> file:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">mx.transitions.Tween</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">mx.transitions.easing.*</span><span style="color: #339933;">;</span></pre></div></div>

<h3>How to use the fisheye-menu</h3>
<h4>Step 4. Create the menu items symbols</h4>
<p>Take back your FLA, and <strong>create 3 symbols</strong> that will be used as items within the fisheye menu. Also, remember to <strong>check the &#8220;Export for ActionScript&#8221; option</strong>, to have them actually usable from <em>ActionScript </em>itself.</p>
<p><img class="alignnone" title="Flash Lite fisheye menu items" src="http://www.jappit.com/images/blog/uploads/fl_fisheye_menu_items.gif" alt="" width="413" height="224" /></p>
<h4>Step 5. Attach and initialize the menu</h4>
<p>Now, <strong>attach a FisheyeMenu istance</strong> directly to the _root, and initialize it with the ID of the symbols created in the previous step:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">var menu<span style="color: #339933;">:</span>MovieClip <span style="color: #339933;">=</span> _root.<span style="color: #006633;">attachMovie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FisheyeMenu'</span>, <span style="color: #0000ff;">'main_menu'</span>, _root.<span style="color: #006633;">getNextHighestDepth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
var items<span style="color: #339933;">:</span><span style="color: #003399;">Array</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Item0'</span>, <span style="color: #0000ff;">'Item1'</span>, <span style="color: #0000ff;">'Item2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
menu._x <span style="color: #339933;">=</span> <span style="color: #cc66cc;">120</span><span style="color: #339933;">;</span>
menu._y <span style="color: #339933;">=</span> <span style="color: #cc66cc;">120</span><span style="color: #339933;">;</span>
&nbsp;
menu.<span style="color: #006633;">initializeMenu</span><span style="color: #009900;">&#40;</span>items, <span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>Step 6. Create a KeyListener to interact with the menu</h4>
<p>The <em>KeyListener </em>will be really simple, since it will simply <strong>call the shiftItem() function when the user press LEFT or RIGHT keys</strong>, and will call a custom function when the user press the ENTER key, to trace the index of the current focused item:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">var keyListener<span style="color: #339933;">:</span><span style="color: #003399;">Object</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
keyListener.<span style="color: #006633;">onKeyDown</span> <span style="color: #339933;">=</span> function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	var key<span style="color: #339933;">:</span><span style="color: #003399;">Number</span> <span style="color: #339933;">=</span> <span style="color: #003399;">Key</span>.<span style="color: #006633;">getCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>key <span style="color: #339933;">==</span> <span style="color: #003399;">Key</span>.<span style="color: #006633;">RIGHT</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		menu.<span style="color: #006633;">shiftItem</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>key <span style="color: #339933;">==</span> <span style="color: #003399;">Key</span>.<span style="color: #006633;">LEFT</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		menu.<span style="color: #006633;">shiftItem</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>key <span style="color: #339933;">==</span> <span style="color: #003399;">Key</span>.<span style="color: #006633;">ENTER</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		menuFireAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003399;">Key</span>.<span style="color: #006633;">addListener</span><span style="color: #009900;">&#40;</span>keyListener<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
function menuFireAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	trace<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MENU ITEM PRESSED: &quot;</span> <span style="color: #339933;">+</span> menu.<span style="color: #006633;">getFocusedIndex</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Downloads and related resources</h3>
<p>You can download full source code (FLA + ActionScript file) of this example here:</p>
<ul>
<li><a title="Flash Lite Fisheye menu source code" href="http://www.jappit.com/uploads/flashlite/FisheyeMenu.zip">Flash Lite Fisheye menu source code</a></li>
</ul>
<p>If you like this article, feel free to vote it on <a href="http://wiki.forum.nokia.com/index.php/How_to_create_a_dynamic_fisheye_menu_in_Flash_Lite" title="Flash Lite fisheye menu article on Forum Nokia Wiki">Forum Nokia Wiki</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2009/02/19/building-a-dynamic-fisheye-menu-in-flash-lite/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>J2ME Images: how to create a reflection effect</title>
		<link>http://www.jappit.com/blog/2009/01/25/j2me-images-how-to-create-a-reflection-effect/</link>
		<comments>http://www.jappit.com/blog/2009/01/25/j2me-images-how-to-create-a-reflection-effect/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 12:04:12 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[j2me]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[forumnokia]]></category>
		<category><![CDATA[image fx]]></category>
		<category><![CDATA[image reflection]]></category>
		<category><![CDATA[java me]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=238</guid>
		<description><![CDATA[It&#8217;s surely time for some new J2ME tutorial, so this article will explain how to create a nice reflection effect starting from a simple Image. You can see the final effect, as usual, on the emulator page: J2ME Image reflection in action. Source code 1. Method declaration Let&#8217;s start by our method declaration: public static [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s surely time for some new J2ME tutorial, so this article will explain how to create a nice <strong>reflection effect starting from a simple Image</strong>.</p>
<p><img class="alignnone" title="J2ME image reflection effect" src="http://www.jappit.com/images/blog/uploads/j2me_image_reflection.gif" alt="" width="309" height="184" /></p>
<p>You can see the final effect, as usual, on the emulator page: <a title="J2ME Image reflection" href="http://www.jappit.com/index.php?page=emulator&#038;midlet=image_reflection">J2ME Image reflection in action</a>.</p>
<h3>Source code</h3>
<h4>1. Method declaration</h4>
<p>Let&#8217;s start by our method declaration:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Image</span> createReflectedImage<span style="color: #009900;">&#40;</span><span style="color: #003399;">Image</span> image, <span style="color: #000066; font-weight: bold;">int</span> bgColor, <span style="color: #000066; font-weight: bold;">int</span> reflectionHeight<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We have 3 arguments:</p>
<ul>
<li>the <strong>original image</strong> that we want to reflect</li>
<li>the <strong>background color</strong> (used for transparent images)</li>
<li>the <strong>height of the reflection</strong> effect</li>
</ul>
<h4>2. The mutable Image</h4>
<p>Now, let&#8217;s <strong>create the mutable Image</strong> that will hold the resulting effect:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> w <span style="color: #339933;">=</span> image.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">int</span> h <span style="color: #339933;">=</span> image.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">Image</span> reflectedImage <span style="color: #339933;">=</span> <span style="color: #003399;">Image</span>.<span style="color: #006633;">createImage</span><span style="color: #009900;">&#40;</span>w, h <span style="color: #339933;">+</span> reflectionHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We store the original image width and height into 2 int variables, and then <strong>create the mutable image</strong> with the <strong>same width</strong>, but with an <strong>height equal to h (the original image) plus the specified reflection height</strong>.</p>
<h4>3. Copy the original Image</h4>
<p>Now, first drawing steps are:</p>
<ol>
<li> <strong>Getting the Graphics object</strong> of our mutable image</li>
<li><strong> Filling</strong> the image with the <strong>background color</strong></li>
<li><strong> Drawing the original image</strong> on the upper part of the mutable one</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Graphics</span> g <span style="color: #339933;">=</span> reflectedImage.<span style="color: #006633;">getGraphics</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
g.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span>bgColor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
g.<span style="color: #006633;">fillRect</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, w, h <span style="color: #339933;">+</span> reflectionHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
g.<span style="color: #006633;">drawImage</span><span style="color: #009900;">&#40;</span>image, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">TOP</span> <span style="color: #339933;">|</span> <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">LEFT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>4. Create the reflection effect</h4>
<p>Now, let&#8217;s get to the important part of this tutorial, that is the reflection effect itself:</p>
<ul>
<li><strong>for each horizontal line</strong> of the reflected image part, take the corresponding <strong>vertical coordinate of the original image</strong></li>
<li><strong>get the RGBA data</strong> of the corresponding horizontal line of the original image</li>
<li><strong>calculate the alpha</strong> to be applied to this line, and apply it to each element of the RGB data array</li>
<li><strong>draw the RGB data</strong> into the reflected image, by using its <em>Graphics</em> object</li>
</ul>
<p>And here is the <strong>source code</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> rgba <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span>w<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> currentY <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> reflectionHeight<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">int</span> y <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">*</span> h <span style="color: #339933;">/</span> reflectionHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>y <span style="color: #339933;">!=</span> currentY<span style="color: #009900;">&#41;</span>
		image.<span style="color: #006633;">getRGB</span><span style="color: #009900;">&#40;</span>rgba, <span style="color: #cc66cc;">0</span>, w, <span style="color: #cc66cc;">0</span>, y, w, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">int</span> alpha <span style="color: #339933;">=</span> 0xff <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">*</span> 0xff <span style="color: #339933;">/</span> reflectionHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> w<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">int</span> origAlpha <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>rgba<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">24</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> newAlpha <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>alpha <span style="color: #339933;">&amp;</span> origAlpha<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> alpha <span style="color: #339933;">/</span> 0xff<span style="color: #339933;">;</span>
&nbsp;
		rgba<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>rgba<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;</span> 0x00ffffff<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		rgba<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>rgba<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">|</span> <span style="color: #009900;">&#40;</span>newAlpha <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">24</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	g.<span style="color: #006633;">drawRGB</span><span style="color: #009900;">&#40;</span>rgba, <span style="color: #cc66cc;">0</span>, w, <span style="color: #cc66cc;">0</span>, h <span style="color: #339933;">+</span> i, w, <span style="color: #cc66cc;">1</span>, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>as you can see, the <em>rgba[]</em> <em>int</em> array holds the current pixel row data, and will be <strong>refreshed only when necessary</strong> (so, when the y coordinate of the original image changes).</p>
<h3>Sample usage</h3>
<p>Using the above method is really simple, since it&#8217;s only necessary to:</p>
<ol>
<li><strong>Create the original Image</strong></li>
<li><strong>Call</strong> <em>createReflectedImage()</em> method by passing the <strong>original Image as argument</strong>, together with the background color and the reflection effect height</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Image</span> originalImage <span style="color: #339933;">=</span> <span style="color: #003399;">Image</span>.<span style="color: #006633;">createImage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/cap_man1.png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">Image</span> reflectedImage <span style="color: #339933;">=</span> ReflectedImage.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span>originalImage, bgColor, <span style="color: #cc66cc;">64</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Downloads</h3>
<p>You can download the complete source code of this article here:</p>
<ul>
<li><a title="J2ME Image reflection source code" href="http://www.jappit.com/uploads/src/ReflectedImage.java">ReflectedImage.java</a></li>
</ul>
<h3>Vote this article!</h3>
<p>If you liked this tutorial, feel free to vote it on Forum Nokia Wiki: <a title="How to create an image reflection effect in Java ME" href="http://wiki.forum.nokia.com/index.php/How_to_create_an_image_reflection_effect_in_Java_ME">How to create an image reflection effect in Java ME</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2009/01/25/j2me-images-how-to-create-a-reflection-effect/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to build a Canvas based List in J2ME</title>
		<link>http://www.jappit.com/blog/2008/09/12/how-to-build-a-canvas-based-list-in-j2me/</link>
		<comments>http://www.jappit.com/blog/2008/09/12/how-to-build-a-canvas-based-list-in-j2me/#comments</comments>
		<pubDate>Fri, 12 Sep 2008 07:44:33 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[j2me]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[forumnokia]]></category>
		<category><![CDATA[java me]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=72</guid>
		<description><![CDATA[Quite a bit of time is passed since the last Java ME tutorial.. so It&#8217;s time for something new, don&#8217;t you think? Today we&#8217;ll see how it is possible to implement a simple Canvas based List, with the following features: customizable style (colors, margins, font) vertical scrolling image and text support (as for standard Java [...]]]></description>
			<content:encoded><![CDATA[<p>Quite a bit of time is passed since the last Java ME tutorial.. so <strong>It&#8217;s time for something new</strong>, don&#8217;t you think?</p>
<p>Today we&#8217;ll see how it is possible to implement a simple <strong>Canvas based List</strong>, with the following features:</p>
<ul>
<li><strong>customizable style</strong> (colors, margins, font)</li>
<li><strong>vertical scrolling</strong></li>
<li><strong>image and text support</strong> (as for standard Java ME Lists)</li>
</ul>
<p><img class="alignnone" title="Java ME Canvas based List screenshot" src="http://www.jappit.com/images/blog/uploads/java_me_canvas_based_list.png" alt="Java ME Canvas based List screenshot" width="304" height="247" /></p>
<p>It is possible to see this <strong>code in action</strong> on the <a title="Java ME Canvas based list in action" href="http://www.jappit.com/index.php?page=emulator&#038;midlet=canvas_based_list">emulator page</a>.</p>
<h3>Writing the code</h3>
<p>First thing you should define some <strong>style-related properties</strong>, that will be used to paint the List items. Name of single properties is self-explaining.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> linePadding <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> margin <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> padding <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Font</span> font <span style="color: #339933;">=</span> <span style="color: #003399;">Font</span>.<span style="color: #006633;">getDefaultFont</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">int</span> bgColor <span style="color: #339933;">=</span> 0xffffff<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">int</span> foreColor <span style="color: #339933;">=</span> 0x000000<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> foreSelectedColor <span style="color: #339933;">=</span> 0xffffff<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> backColor <span style="color: #339933;">=</span> 0xffffff<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> backSelectedColor <span style="color: #339933;">=</span> 0x0000ff<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">int</span> borderWidth <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> borderColor <span style="color: #339933;">=</span> 0x000000<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> borderSelectedColor <span style="color: #339933;">=</span> 0xff0000<span style="color: #339933;">;</span></pre></div></div>

<p>Now, here are some <strong>internal properties</strong>, that will be used to handle list <strong>items content and positioning</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// will contain item splitted lines</span>
<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> itemLines <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// will hold items image parts</span>
<span style="color: #003399;">Image</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> images <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// will hold selected item index</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> selectedItem <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// these will hold item graphical properties</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> itemsTop <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> itemsHeight <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// these will hold List vertical scrolling</span>
<span style="color: #000066; font-weight: bold;">int</span> scrollTop <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> SCROLL_STEP <span style="color: #339933;">=</span> <span style="color: #cc66cc;">40</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, here is the <strong>CanvasList constructor</strong>. Its arguments are (similarly to javax.microedition.lcdui.List constructor):<br />
* the <strong>screen&#8217;s title</strong><br />
* <strong>set of strings</strong> specifying the string parts of the List elements<br />
* <strong>set of images</strong> specifying the image parts of the List elements<br />
This article will not cover the handling of different types of Lists (e.g.: exclusive, multiple, ..).</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> CanvasList<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> title, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> items, <span style="color: #003399;">Image</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> imageElements<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	setTitle<span style="color: #009900;">&#40;</span>title<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">images</span> <span style="color: #339933;">=</span> imageElements<span style="color: #339933;">;</span>
&nbsp;
	itemLines <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span>items.<span style="color: #006633;">length</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	itemsTop <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span>itemLines.<span style="color: #006633;">length</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	itemsHeight <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span>itemLines.<span style="color: #006633;">length</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> itemLines.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// get image part of this item, if available</span>
		<span style="color: #003399;">Image</span> imagePart <span style="color: #339933;">=</span> getImage<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// get avaiable width for text</span>
		<span style="color: #000066; font-weight: bold;">int</span> w <span style="color: #339933;">=</span> getItemWidth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>imagePart <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> imagePart.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> padding <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// and split item text into text rows, to fit available width</span>
		itemLines<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> getTextRows<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>, font, w<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here are the 2 utility methods found in the CanvasList constructor:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getItemWidth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">return</span> getWidth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> borderWidth <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> padding <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> margin<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003399;">Image</span> getImage<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> index<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">return</span> images <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> images.<span style="color: #006633;">length</span> <span style="color: #339933;">&gt;</span> index <span style="color: #339933;">?</span> images<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, here is the <strong>paint() method</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> paint<span style="color: #009900;">&#40;</span><span style="color: #003399;">Graphics</span> g<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// paint List background</span>
	g.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span>bgColor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	g.<span style="color: #006633;">fillRect</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, getWidth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, getHeight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// translate accordingly to current List vertical scroll</span>
	g.<span style="color: #006633;">translate</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #339933;">-</span> scrollTop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">int</span> top <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	g.<span style="color: #006633;">setFont</span><span style="color: #009900;">&#40;</span>font<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// loop List items</span>
	<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> itemLines.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">int</span> itemRows <span style="color: #339933;">=</span> itemLines<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">Image</span> imagePart <span style="color: #339933;">=</span> getImage<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">int</span> itemHeight <span style="color: #339933;">=</span> itemRows <span style="color: #339933;">*</span> font.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> linePadding <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>itemRows <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		itemsTop<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> top<span style="color: #339933;">;</span>
		itemsHeight<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> itemHeight<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// is image part higher than the text part?</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>imagePart <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> imagePart.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> itemHeight<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			itemHeight <span style="color: #339933;">=</span> imagePart.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		itemHeight <span style="color: #339933;">+=</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> padding <span style="color: #339933;">+</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> borderWidth<span style="color: #339933;">;</span>
&nbsp;
		g.<span style="color: #006633;">translate</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, top<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>borderWidth <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// paint item border</span>
			g.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">==</span> selectedItem <span style="color: #339933;">?</span> borderSelectedColor <span style="color: #339933;">:</span> borderColor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			g.<span style="color: #006633;">fillRect</span><span style="color: #009900;">&#40;</span>margin, margin, getWidth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> margin, itemHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// paint item background</span>
		g.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">==</span> selectedItem <span style="color: #339933;">?</span> backSelectedColor <span style="color: #339933;">:</span> backColor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		g.<span style="color: #006633;">fillRect</span><span style="color: #009900;">&#40;</span>margin <span style="color: #339933;">+</span> borderWidth, margin <span style="color: #339933;">+</span> borderWidth, getWidth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> margin <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> borderWidth, itemHeight <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> borderWidth<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// has this item an image part?</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>imagePart <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			g.<span style="color: #006633;">drawImage</span><span style="color: #009900;">&#40;</span>imagePart, margin <span style="color: #339933;">+</span> borderWidth <span style="color: #339933;">+</span> padding, margin <span style="color: #339933;">+</span> borderWidth <span style="color: #339933;">+</span> padding, <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">TOP</span> <span style="color: #339933;">|</span> <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">LEFT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// paint item text rows</span>
		g.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">==</span> selectedItem <span style="color: #339933;">?</span> foreSelectedColor <span style="color: #339933;">:</span> foreColor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">int</span> textLeft <span style="color: #339933;">=</span> margin <span style="color: #339933;">+</span> borderWidth <span style="color: #339933;">+</span> padding <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>imagePart <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> imagePart.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> padding <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> itemRows<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			g.<span style="color: #006633;">drawString</span><span style="color: #009900;">&#40;</span>itemLines<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>, textLeft, margin <span style="color: #339933;">+</span> borderWidth <span style="color: #339933;">+</span> padding <span style="color: #339933;">+</span> j <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>linePadding <span style="color: #339933;">+</span> font.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">TOP</span> <span style="color: #339933;">|</span> <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">LEFT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		g.<span style="color: #006633;">translate</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #339933;">-</span> top<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		top <span style="color: #339933;">+=</span> itemHeight <span style="color: #339933;">+</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> margin<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">// finally, translate back</span>
	g.<span style="color: #006633;">translate</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, scrollTop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And finally, to handle user key events, here is the <strong>keyPressed() event</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> keyPressed<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> key<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">int</span> keyCode <span style="color: #339933;">=</span> getGameAction<span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// is there 1 item at least?</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>itemLines.<span style="color: #006633;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// going up</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>keyCode <span style="color: #339933;">==</span> <span style="color: #003399;">Canvas</span>.<span style="color: #006633;">UP</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// current item is clipped on top, so can scroll up</span>
			<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>itemsTop<span style="color: #009900;">&#91;</span>selectedItem<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> scrollTop<span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				scrollTop <span style="color: #339933;">-=</span> SCROLL_STEP<span style="color: #339933;">;</span>
&nbsp;
				repaint<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #666666; font-style: italic;">// is there a previous item?</span>
			<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>selectedItem <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				selectedItem<span style="color: #339933;">--;</span>
&nbsp;
				repaint<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #666666; font-style: italic;">//going down</span>
		<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>keyCode <span style="color: #339933;">==</span> <span style="color: #003399;">Canvas</span>.<span style="color: #006633;">DOWN</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// current item is clipped on bottom, so can scroll down</span>
			<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>itemsTop<span style="color: #009900;">&#91;</span>selectedItem<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> itemsHeight<span style="color: #009900;">&#91;</span>selectedItem<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;=</span> scrollTop <span style="color: #339933;">+</span> getHeight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				scrollTop <span style="color: #339933;">+=</span> SCROLL_STEP<span style="color: #339933;">;</span>
&nbsp;
				repaint<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #666666; font-style: italic;">// is there a following item?</span>
			<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>selectedItem <span style="color: #339933;">&lt;</span> itemLines.<span style="color: #006633;">length</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				selectedItem<span style="color: #339933;">++;</span>
&nbsp;
				repaint<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>About the <strong>getTextRows() method</strong>, you can grab an implementation (but you could find a lot of other ones on the Web) on this other article written some time ago:<a title="J2ME Scrollable Text" href="http://wiki.forum.nokia.com/index.php/J2me_Scrollable_Text" target="_blank"> J2ME Scrollable Text</a>.</p>
<h3>How to use CanvasList class</h3>
<p>Here is a <strong>sample usage of CanvasList class</strong>, that will display a list (without image parts):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> items <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Item 1&quot;</span>, <span style="color: #0000ff;">&quot;Item 2&quot;</span>, <span style="color: #0000ff;">&quot;Item 3&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
CanvasList myCanvas <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CanvasList<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Test canvas&quot;</span>, items, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To <strong>add images to your items</strong>, it&#8217;s necessary to instantiate an Image array, and pass it to CanvasList constructor as its third argument:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Image</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> images <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">try</span>
<span style="color: #009900;">&#123;</span>
	images <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Image</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Image</span>.<span style="color: #006633;">createImage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/item1.png&quot;</span><span style="color: #009900;">&#41;</span>,
		<span style="color: #003399;">Image</span>.<span style="color: #006633;">createImage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/item2.png&quot;</span><span style="color: #009900;">&#41;</span>,
		<span style="color: #003399;">Image</span>.<span style="color: #006633;">createImage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/item3.png&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> items <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Item 1&quot;</span>, <span style="color: #0000ff;">&quot;Item 2&quot;</span>, <span style="color: #0000ff;">&quot;Item 3&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
CanvasList myCanvas <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CanvasList<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Test canvas&quot;</span>, items, images<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Related resources</h3>
<p>You can download full <strong>CanvasList source code</strong> here: <a title="J2ME CanvasList source code" href="http://www.jappit.com/uploads/src/CanvasList.java">CanvasList.java</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/09/12/how-to-build-a-canvas-based-list-in-j2me/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Collapsible trees with J2ME</title>
		<link>http://www.jappit.com/blog/2008/04/15/collapsible-trees-with-j2me/</link>
		<comments>http://www.jappit.com/blog/2008/04/15/collapsible-trees-with-j2me/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 15:01:08 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[j2me]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[forumnokia]]></category>
		<category><![CDATA[sample code]]></category>
		<category><![CDATA[trees]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=17</guid>
		<description><![CDATA[Trees are not often used within j2me applications, but since it&#8217;s quite always a mess to deal with them, here&#8217;s a ready to use component for managing and drawing collapsible/scrollable trees As usual, you can go to Forum Nokia Wiki for the full source code: J2me Collapsible Trees, or go to the emulator page to [...]]]></description>
			<content:encoded><![CDATA[<p>Trees are not often used within j2me applications, but since it&#8217;s quite always a mess to deal with them, here&#8217;s a ready to use component for managing and drawing collapsible/scrollable trees <img src='http://www.jappit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<img src="http://www.jappit.com/images/blog/uploads/j2me_collapsible_tree.png" alt="j2me collapsible trees" width="253" height="156" /></p>
<p>As usual, you can go to Forum Nokia Wiki for the full source code: <a title="J2me collapsible trees" href="http://wiki.forum.nokia.com/index.php/J2ME_Drawing_Collapsible_Trees" target="_blank">J2me Collapsible Trees</a>, or go to the <a title="j2me collapsible tree sample" href="http://www.jappit.com/?page=emulator&amp;midlet=collapsible_tree">emulator page</a> to have a first look at a sample midlet using this component.</p>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/04/15/collapsible-trees-with-j2me/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Customizing game sprites with J2ME</title>
		<link>http://www.jappit.com/blog/2008/04/14/customizing-characters-images-with-j2me/</link>
		<comments>http://www.jappit.com/blog/2008/04/14/customizing-characters-images-with-j2me/#comments</comments>
		<pubDate>Mon, 14 Apr 2008 10:07:07 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[j2me]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[forumnokia]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[sprite colors]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=15</guid>
		<description><![CDATA[You know, my great passion are games, and here&#8217;s a first game-related tutorial Often in games a nice feature to give to users it the possibility to customize their own character, for example changing colors of the different parts (hairs, eyes, shirt, and so on). To support this features there are 2 possibilities: Include a [...]]]></description>
			<content:encoded><![CDATA[<p>You know, my great passion are games, and here&#8217;s a first game-related tutorial <img src='http://www.jappit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Often in games a nice feature to give to users it the possibility to customize their own character, for example changing colors of the different parts (hairs, eyes, shirt, and so on).</p>
<p><img src="http://www.jappit.com/images/blog/uploads/j2me_custom_colors.png" alt="" width="242" height="134" /></p>
<p>To support this features there are 2 possibilities:</p>
<ul>
<li> Include a different image for each color of each different part</li>
<li> Replace colors by code</li>
</ul>
<p>The second options will save you the effort to create these multiple images, and will strip down your JAR size. Also, it will allow you to support a lot more colors.</p>
<p>You can see a simple midlet showing how this can be done with simple code on the <a title="Customizing character colors with J2ME" href="http://www.jappit.com/?page=emulator&amp;midlet=custom_colors">emulator page</a></p>
<p>For the full source code you can visit the Forum Nokia Wiki page: <a title="J2ME Customizing Game Sprites Color" href="http://wiki.forum.nokia.com/index.php/J2ME_Customizing_Game_Sprites_Color">J2ME Customizing Game Sprites Color</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/04/14/customizing-characters-images-with-j2me/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a J2ME vertical scrollable text component</title>
		<link>http://www.jappit.com/blog/2008/04/10/building-a-j2me-vertical-scrollable-text-component/</link>
		<comments>http://www.jappit.com/blog/2008/04/10/building-a-j2me-vertical-scrollable-text-component/#comments</comments>
		<pubDate>Thu, 10 Apr 2008 09:48:11 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[j2me]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[forumnokia]]></category>
		<category><![CDATA[scrollable text]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=12</guid>
		<description><![CDATA[Everyone needs scrollable text, soon or late This is the reason why I&#8217;ve posted a simple tutorial on how vertical scrollable text can be implemented with J2ME. You can find it on Forum Nokia Wiki: J2me Scrollable Text tutorial Comments are welcome!]]></description>
			<content:encoded><![CDATA[<p>Everyone needs scrollable text, soon or late <img src='http://www.jappit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>This is the reason why I&#8217;ve posted a simple tutorial on how vertical scrollable text can be implemented with J2ME.</p>
<p><img class="alignnone" src="http://wiki.forum.nokia.com/images/d/da/Scrollable_text_screenshot.png" alt="Scrollable text screenshot" width="302" height="153" /></p>
<p>You can find it on Forum Nokia Wiki: <a title="J2me vertical scrollable text tutorial" href="http://wiki.forum.nokia.com/index.php/J2me_Scrollable_Text">J2me Scrollable Text tutorial</a></p>
<p>Comments are welcome!</p>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/04/10/building-a-j2me-vertical-scrollable-text-component/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Forum Nokia Wiki.. my first article!</title>
		<link>http://www.jappit.com/blog/2008/04/09/forum-nokia-wiki-my-first-article/</link>
		<comments>http://www.jappit.com/blog/2008/04/09/forum-nokia-wiki-my-first-article/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 20:46:04 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[j2me]]></category>
		<category><![CDATA[nokia]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[custom text field]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=10</guid>
		<description><![CDATA[Yeah, I&#8217;ve finally done it! Forum Nokia Wiki is a precious source for any kind of mobile related problems or questions, and now I&#8217;m really happy to have found the time to post my first article, that is&#8230; How to implement custom Text Fields with J2ME! Hope you&#8217;ll find it useful EDIT: you can now [...]]]></description>
			<content:encoded><![CDATA[<p><img style="vertical-align: bottom;" src="http://community.forum.nokia.com/images/wiki_02.gif" alt="Forum Nokia Wiki" width="420" height="35" /></p>
<p>Yeah, I&#8217;ve finally done it!</p>
<p><a title="Forum Nokia Wiki" href="http://wiki.forum.nokia.com/">Forum Nokia Wiki</a> is a precious source for any kind of mobile related problems or questions, and now I&#8217;m really happy to have found the time to post my first article, that is&#8230;</p>
<p><a title="J2ME Canvas Custom Text Fields" href="http://wiki.forum.nokia.com/index.php/J2ME_Custom_Text_Input">How to implement custom Text Fields with J2ME</a>!</p>
<p>Hope you&#8217;ll find it useful <img src='http://www.jappit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>EDIT: you can now test the custom Text Field component with the web emulator <a title="Custom J2me text field emulator" href="http://www.jappit.com/?page=emulator&amp;midlet=custom_text_field">here</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/04/09/forum-nokia-wiki-my-first-article/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

