<?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; source code</title>
	<atom:link href="http://www.jappit.com/blog/tag/source-code/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>Handling touch gestures in Flash Lite</title>
		<link>http://www.jappit.com/blog/2009/02/23/handling-touch-gestures-in-flash-lite/</link>
		<comments>http://www.jappit.com/blog/2009/02/23/handling-touch-gestures-in-flash-lite/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 15:24:50 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[flash lite]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[forumnokia]]></category>
		<category><![CDATA[gestures]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[touchscreen]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=331</guid>
		<description><![CDATA[With the release of many touch-enabled devices, as the latest Nokia 5800 XpressMusic and the forthcoming Nokia N97, applications can benefit from new ways of touch-based interactions. A typical example of these new patterns is represented by gestures: simple finger/stylus actions that allow the user to perform specific task, without the need of a precise [...]]]></description>
			<content:encoded><![CDATA[<p>With the release of many <strong>touch-enabled devices</strong>, as the latest <a title="Nokia 5800 XpressMusic specifications" href="http://www.forum.nokia.com/devices/5800_XpressMusic" target="_blank">Nokia 5800 XpressMusic</a> and the forthcoming <a title="Nokia N97 specifications" href="http://www.forum.nokia.com/devices/N97" target="_blank">Nokia N97</a>, applications can benefit from <strong>new ways of touch-based interactions</strong>. A typical example of these new patterns is represented by <strong>gestures</strong>: simple finger/stylus actions that <strong>allow the user to perform specific task</strong>, <strong>without the need of a precise interaction</strong> (e.g.: identify and press a specific button).</p>
<p>This article will explain how to implement <strong>basic touch gestures</strong> in <a title="Flash Lite website" href="http://www.adobe.com/products/flashlite/" target="_blank">Flash Lite</a>. Specifically, we&#8217;ll see how to <strong>detect both horizontal (left-to-right and right-to-left) and vertical (up-to-down and down-to-up) gestures</strong>.</p>
<p style="text-align: center;"><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=3333440&#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=3333440&#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>
<p>In the above video it&#8217;s possible to see a simple <strong>Flash Lite photo viewer</strong>, where the user can <strong>go from a photo to the next/previous just sliding finger or stylus</strong>.</p>
<h3>The source code</h3>
<h4>Step 1. Detect touch events</h4>
<p>First thing to do is to <strong>detect and handle touch-based events</strong>, and this can be done by implementing a <em>MouseListener</em>, and its <em>onMouseDown()</em> and <em>onMouseUp()</em> methods. We also define <strong>4 Number variables</strong> to <em>hold the x and y coordinates</em> associated to the mouse up and down events: these will be <strong>used to detect if the touch interaction was actually a gesture</strong> or not.</p>
<p>So, take a <strong>new and empty FLA</strong>, and add this code on your first frame:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> startX:<span style="color: #0066CC;">Number</span>;
<span style="color: #000000; font-weight: bold;">var</span> startY:<span style="color: #0066CC;">Number</span>;
<span style="color: #000000; font-weight: bold;">var</span> endX:<span style="color: #0066CC;">Number</span>;
<span style="color: #000000; font-weight: bold;">var</span> endY:<span style="color: #0066CC;">Number</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> gesturesListener:<span style="color: #0066CC;">Object</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
gesturesListener.<span style="color: #0066CC;">onMouseDown</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	startX = <span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">_xmouse</span>;
	startY = <span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">_ymouse</span>;
<span style="color: #66cc66;">&#125;</span>
gesturesListener.<span style="color: #0066CC;">onMouseUp</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	endX = <span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">_xmouse</span>;
	endY = <span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">_ymouse</span>;
&nbsp;
	checkGesture<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">Mouse</span>.<span style="color: #0066CC;">addListener</span><span style="color: #66cc66;">&#40;</span>gesturesListener<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>The <em>onMouseDown()</em> function just <strong>sets the starting coordinates values</strong>, while the <em>onMouseUp()</em> also calls the <em>checkGesture()</em> function, defined below, that will <strong>check if the touch interaction was actually a gesture</strong>.</p>
<h4>Step 2. Identify a gesture</h4>
<p>Before trying to identify a gesture, let&#8217;s define some variables used to identify and define specific gesture types:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// minimum length of an horizontal gesture</span>
<span style="color: #000000; font-weight: bold;">var</span> MIN_H_GESTURE:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Stage</span>.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">3</span>;
<span style="color: #808080; font-style: italic;">// minimum length of a vertical gesture</span>
<span style="color: #000000; font-weight: bold;">var</span> MIN_V_GESTURE:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Stage</span>.<span style="color: #0066CC;">height</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">3</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// flags for each kind of gesture</span>
<span style="color: #000000; font-weight: bold;">var</span> UP_TO_DOWN:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1</span>;
<span style="color: #000000; font-weight: bold;">var</span> DOWN_TO_UP:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">2</span>;
<span style="color: #000000; font-weight: bold;">var</span> LEFT_TO_RIGHT:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">4</span>;
<span style="color: #000000; font-weight: bold;">var</span> RIGHT_TO_LEFT:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">8</span>;</pre></div></div>

<p>The <em>MIN_H_GESTURE</em> and <em>MIN_V_GESTURE</em> variables define the <strong>minimum horizontal and vertical distances </strong>that must exist between the <em>onMouseDown()</em> and the <em>onMouseUp()</em> event to have, respectively, a horizontal or a vertical gesture.</p>
<p>Now, we can easily implement the <em>checkGesture()</em> function, by <strong>getting the horizontal and vertical length of the touch interaction</strong>, and <strong>comparing them with the minimum distances</strong> defined above.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> checkGesture<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> xDelta:<span style="color: #0066CC;">Number</span> = endX - startX;
	<span style="color: #000000; font-weight: bold;">var</span> yDelta:<span style="color: #0066CC;">Number</span> = endY - startY;
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> gesture:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>xDelta <span style="color: #66cc66;">&gt;</span> MIN_H_GESTURE<span style="color: #66cc66;">&#41;</span>
		gesture <span style="color: #66cc66;">|</span>= LEFT_TO_RIGHT;
	<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>xDelta <span style="color: #66cc66;">&lt;</span> - MIN_H_GESTURE<span style="color: #66cc66;">&#41;</span>
		gesture <span style="color: #66cc66;">|</span>= RIGHT_TO_LEFT;
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>yDelta <span style="color: #66cc66;">&gt;</span> MIN_V_GESTURE<span style="color: #66cc66;">&#41;</span>
		gesture <span style="color: #66cc66;">|</span>= UP_TO_DOWN;
	<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>yDelta <span style="color: #66cc66;">&lt;</span> - MIN_V_GESTURE<span style="color: #66cc66;">&#41;</span>
		gesture <span style="color: #66cc66;">|</span>= DOWN_TO_UP;
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>gesture <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
		handleGesture<span style="color: #66cc66;">&#40;</span>gesture<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h4>Step 3. Handling the detected gesture</h4>
<p>Once identified a gesture, we have to actually handle it in some way. To do it, we&#8217;ll implement the <em>handleGesture()</em> function, that will check the passed argument to find which is the specific identified gesture.</p>
<p>In this example, we&#8217;ll simply trace the kind of identified gesture(s).</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> handleGesture<span style="color: #66cc66;">&#40;</span>gestureFlags:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>gestureFlags <span style="color: #66cc66;">&amp;</span> LEFT_TO_RIGHT<span style="color: #66cc66;">&#41;</span>
		<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;left to right gesture&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>gestureFlags <span style="color: #66cc66;">&amp;</span> RIGHT_TO_LEFT<span style="color: #66cc66;">&#41;</span>
		<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;right to left gesture&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>gestureFlags <span style="color: #66cc66;">&amp;</span> UP_TO_DOWN<span style="color: #66cc66;">&#41;</span>
		<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;up to down gesture&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>gestureFlags <span style="color: #66cc66;">&amp;</span> DOWN_TO_UP<span style="color: #66cc66;">&#41;</span>
		<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;down to up gesture&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h4>Further development</h4>
<p>A lot of improvements can be done to the code presented in this article. Just some example:</p>
<ul>
<li><strong>Detect diagonal gestures</strong>: by checking the appropriate <strong>gesture flags</strong>, it&#8217;s already possible to <strong>identify mixed diagonal gestures</strong>. So, basically you need to extend a bit the handleGesture() method</li>
<li>Define a <strong>maximum time</strong> to perform a gesture: basically, accept a gesture only if the time elapsed to perform it is below a certain limit. This could be useful in some scenarios, to avoid detecting fake gestures.</li>
</ul>
<h3>Download source code</h3>
<p>You can download the full source code used in this article here:</p>
<ul>
<li><a title="Handling touch gestures in Flash Lite source code" href="http://www.jappit.com/uploads/flashlite/TouchGestures.zip">Handling gestures in Flash Lite source code</a></li>
</ul>
<h3>Rate this article!</h3>
<p>If you like this article, please <a href="http://wiki.forum.nokia.com/index.php/How_to_detect_touch_gestures_in_Flash_Lite" title="Vote this article on Forum Nokia Wiki">vote it on Forum Nokia Wiki</a>. Thanks <img src='http://www.jappit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2009/02/23/handling-touch-gestures-in-flash-lite/feed/</wfw:commentRss>
		<slash:comments>6</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>Create your first Flash Lite ringtone with KuneriLite</title>
		<link>http://www.jappit.com/blog/2008/10/08/create-your-first-flash-lite-ringtone-with-kunerilite/</link>
		<comments>http://www.jappit.com/blog/2008/10/08/create-your-first-flash-lite-ringtone-with-kunerilite/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 14:02:27 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[cool stuff]]></category>
		<category><![CDATA[flash lite]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[nokia]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[kunerilite]]></category>
		<category><![CDATA[ringtones]]></category>
		<category><![CDATA[s60]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[symbian]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=110</guid>
		<description><![CDATA[For those of you who missed it (really??) latest KuneriLite versions have added support for Flash Lite ringtones, one of the coolest FlashLite features around!! Today, we&#8217;ll see how it is simple to create a FlashLite ringtone with caller-id support and an application that allows users to easily set and unset it. Step 1: The [...]]]></description>
			<content:encoded><![CDATA[<p>For those of you who missed it (really??) latest <a title="KuneriLite" href="http://www.kunerilite.net" target="_blank">KuneriLite</a> versions have added support for <a title="KuneriLite Ringtone plugin" href="http://wiki.kunerilite.net/index.php?title=Ringtone_plugin" target="_blank">Flash Lite ringtones</a>, <strong>one of the coolest FlashLite features around</strong>!!</p>
<p>Today, we&#8217;ll see how it is simple to <strong>create a FlashLite ringtone with caller-id support</strong> and an application that allows users to easily set and unset it.</p>
<h3>Step 1: The FlashLite ringtone</h3>
<p>To start, we&#8217;ll build a really simple <strong>FlashLite ringtone</strong>.</p>
<p>Let&#8217;s start building a <strong>simple interface, with these elements</strong>:</p>
<p><img class="alignnone" title="KuneriLite ringtone setter screenshot" src="http://www.jappit.com/images/blog/uploads/kunerilite_ringtone.png" alt="" /></p>
<p>Now it&#8217;s time to add some <strong>ActionScript</strong> to our interface. So, let&#8217;s open <strong>frame 1 of our Actions layer</strong>.</p>
<blockquote><p><strong>Important note:</strong> when using KuneriLite from a ringtone SWF, you MUST use <strong>port 2001</strong>.</p></blockquote>
<p>First, we&#8217;ll define a method to <strong>retrieve caller infos</strong>, and display it, depending on the returned data:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">var loader<span style="color: #339933;">:</span>LoadVars <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LoadVars<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
getCallerName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
function getCallerName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	commandOutput.<span style="color: #006633;">text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Getting caller's info... &quot;</span><span style="color: #339933;">;</span>
&nbsp;
	loader.<span style="color: #006633;">onLoad</span> <span style="color: #339933;">=</span> callerNameHandler<span style="color: #339933;">;</span>
&nbsp;
	loader.<span style="color: #006633;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://127.0.0.1:2001/Basic/ring?klCommand=callerid&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
function callerNameHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	commandOutput.<span style="color: #006633;">text</span> <span style="color: #339933;">+=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">toString</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><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">klError</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		callerName.<span style="color: #006633;">text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Command error: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">klError</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><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">klName</span> <span style="color: #339933;">!=</span> undefined<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		callerName.<span style="color: #006633;">text</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">klName</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">else</span>
	<span style="color: #009900;">&#123;</span>
		callerName.<span style="color: #006633;">text</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">klNumber</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And then, let&#8217;s add the call <strong>answer/reject functionality</strong> to our ringtone. Two other KuneriLite calls will do the job (note that we&#8217;ll reuse the LoadVars instance defined above):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">answer.<span style="color: #006633;">onPress</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>
	commandOutput.<span style="color: #006633;">text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Answering call... &quot;</span><span style="color: #339933;">;</span>
&nbsp;
	loader.<span style="color: #006633;">onLoad</span> <span style="color: #339933;">=</span> callCommandHandler<span style="color: #339933;">;</span>
&nbsp;
	loader.<span style="color: #006633;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://127.0.0.1:2001/Basic/ring?klCommand=answercall&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
reject.<span style="color: #006633;">onPress</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>
	commandOutput.<span style="color: #006633;">text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Rejecting call... &quot;</span><span style="color: #339933;">;</span>
&nbsp;
	loader.<span style="color: #006633;">onLoad</span> <span style="color: #339933;">=</span> callCommandHandler<span style="color: #339933;">;</span>
&nbsp;
	loader.<span style="color: #006633;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://127.0.0.1:2001/Basic/ring?klCommand=hangupcall&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
function callCommandHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	commandOutput.<span style="color: #006633;">text</span> <span style="color: #339933;">+=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<blockquote><p><strong>Important note</strong>: since <strong>KuneriLite ringtone plugin already handles device answer and reject keys</strong> (the green and red one) you could avoid implementing your custom buttons in ringtone SWF (<strong>thanks Jukka</strong> for the reminder!)</p></blockquote>
<h3>Step 2: Setting and unsetting the ringtone</h3>
<p>Now, it&#8217;s time to <strong>build the &#8220;main&#8221; SWF application</strong>, that is the one that the user would launch from phone menu to manage its FlashLite ringtones.</p>
<p>As usual, let&#8217;s create a basic interface, with this layout:</p>
<p><img class="alignnone" title="KuneriLite ringtone setter screenshot" src="http://www.jappit.com/images/blog/uploads/kunerilite_ringtone_setter.png" alt="" width="450" height="322" /></p>
<p>Now, let&#8217;s <strong>add the necessary ActionScript code</strong> to our Buttons.<br />
This is for the <strong>enable button</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">enableButton.<span style="color: #006633;">onPress</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>
	commandOutput.<span style="color: #006633;">text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Enabling ringtone..&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	var loader<span style="color: #339933;">:</span>LoadVars <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LoadVars<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	loader.<span style="color: #006633;">onLoad</span> <span style="color: #339933;">=</span> handleResponse<span style="color: #339933;">;</span>
&nbsp;
	loader.<span style="color: #006633;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://127.0.0.1:1001/Basic/ring?klCommand=enableringswf&amp;amp;klPath=ringtone.swf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And similarly, this is for the <strong>disable button</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">disableButton.<span style="color: #006633;">onPress</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>
	commandOutput.<span style="color: #006633;">text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Disabling ringtone..&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	var loader<span style="color: #339933;">:</span>LoadVars <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LoadVars<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	loader.<span style="color: #006633;">onLoad</span> <span style="color: #339933;">=</span> handleResponse<span style="color: #339933;">;</span>
&nbsp;
	loader.<span style="color: #006633;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://127.0.0.1:1001/Basic/ring?klCommand=disableringswf&amp;amp;klPath=ringtone.swf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And here&#8217;s the handler, used by both commands calls, to print out the KuneriLite response error code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">function handleResponse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	commandOutput.<span style="color: #006633;">text</span> <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot; Error code: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">klError</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Step 3: building and testing</h3>
<p>Building a <strong>KuneriLite </strong>app is <a title="KuneriLite Wizard Beginner's Guide" href="http://wiki.kunerilite.net/index.php?title=KuneriLite_Wizard_Beginner%27s_Guide" target="_blank">easy as always</a>, but you need to follow these <strong>4 specific steps</strong> to make the ringtone correctly work:</p>
<ol>
<li><strong>Select Ringtone plugin</strong><br />
<img class="alignnone" title="KuneriLite ringtone wizard step 1 screenshot" src="http://www.jappit.com/images/blog/uploads/ringtone_wizard_step1.jpg" alt="" width="351" height="252" /></li>
<li>Place your <strong>ringtone SWF in a separate folder</strong>, containing only that SWF, and then select it on Wizard Step 2<br />
<img class="alignnone" title="KuneriLite ringtone wizard step 2 screenshot" src="http://www.jappit.com/images/blog/uploads/ringtone_wizard_step2.jpg" alt="" width="412" height="271" /></li>
<li>Select the <strong>ringtone setter as main SWF</strong><br />
<img class="alignnone" title="Ringtone KuneriLite wizard step 4 screenshot" src="http://www.jappit.com/images/blog/uploads/ringtone_wizard_step4.jpg" alt="" width="394" height="130" /></li>
<li>Since <strong>Ringtone plugin needs signing</strong>, on Step 3 fill in the certificate infos<br />
<img class="alignnone" title="KuneriLite ringtone wizard step 3 screenshot" src="http://www.jappit.com/images/blog/uploads/ringtone_wizard_step3.jpg" alt="" width="360" height="228" /></li>
</ol>
<p>Once done, just compile and transfer your SIS on your phone, install and launch it:</p>
<ul>
<li>on main app screen, <strong>click the enable button</strong></li>
<li>check the command output, to see if the command executed <strong>successfully</strong>: you should see this message
<pre>Enabling ringtone... Error code: 0</pre>
</li>
<li>if yes, just close the app and <strong>call your own phone</strong>, and your FlashLite ringtone will magically appear!</li>
<li>within the ringtone SWF you will see the <strong>caller&#8217;s name</strong> (if available on your phonebook), otherwise its phone number</li>
<li>to <strong>answer or reject the incoming call</strong>, simply use the buttons we previously placed on stage</li>
</ul>
<p>That&#8217;s it!</p>
<h3>Conclusions</h3>
<p>Now, add this with the other <a title="KuneriLite Plugins" href="http://wiki.kunerilite.net/index.php?title=KuneriLite_Plug-ins" target="_blank">KuneriLite features</a>, and you could end up having:</p>
<ul>
<li><strong>browsable ringtones catalogs</strong>, directly downloadable from your FlashLite app</li>
<li><strong>ringones for specific contacts</strong> (a phonebook plugin would be great!)</li>
<li><strong>location-based ringones</strong>!</li>
</ul>
<p>Isn&#8217;t this enough?</p>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/10/08/create-your-first-flash-lite-ringtone-with-kunerilite/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Touch and J2ME part 1: how scroll an image</title>
		<link>http://www.jappit.com/blog/2008/10/06/touch-and-j2me-part-1-how-scroll-an-image/</link>
		<comments>http://www.jappit.com/blog/2008/10/06/touch-and-j2me-part-1-how-scroll-an-image/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 08:33:43 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[j2me]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[nokia]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[forumnokia]]></category>
		<category><![CDATA[java me]]></category>
		<category><![CDATA[s60 5th edition]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[touch scrolling]]></category>
		<category><![CDATA[touch ui]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=128</guid>
		<description><![CDATA[When you start writing your first app for the new S60 5th edition platform you&#8217;ll find that, among the first things to deal with, there is interface scrolling: when playing with touchscreen devices, users expect to be able to interact with on-screen objects by simple stylus/finger gestures, rather than by an old-fashion, on-screen keyboard. Today, [...]]]></description>
			<content:encoded><![CDATA[<p>When you start writing your first app for the new <a title="Nokia S60 5th edition" href="http://www.forum.nokia.com/main/platforms/s60/#5th" target="_blank">S60 5th edition platform</a> you&#8217;ll find that, among the first things to deal with, there is <strong>interface scrolling</strong>: when playing with touchscreen devices, users expect to be able to <strong>interact with on-screen objects by simple stylus/finger gestures</strong>, rather than by an old-fashion, on-screen keyboard.</p>
<p>Today, we&#8217;ll see <strong>how to implement touch scrolling in Java ME</strong> in a simple scenario: scrolling a large Image, that doesn&#8217;t fit on the device display.</p>
<p><img class="alignnone" title="J2me Touch scrolling screenshot" src="http://www.jappit.com/images/blog/uploads/J2me_touch_scrollable_image.jpg" alt="" width="235" height="254" /></p>
<p>A sample MIDlet showing this code in action is available on the emulator page: <a title="Touch scrollable image in action" href="http://www.jappit.com/index.php?page=emulator&#038;midlet=touch_scrollable_image">Touch scrollable image in action</a>.</p>
<h3>Source Code: ScrollableImageCanvas class</h3>
<p>Let&#8217;s start defining the main class, that will <strong>extend the Canvas object</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> <span style="color: #000000; font-weight: bold;">class</span> ScrollableImageCanvas <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Canvas</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#123;</span></pre></div></div>

<p>Now, we define <strong>properties</strong> that we&#8217;ll use to manage:</p>
<ul>
<li> image size</li>
<li> image translation</li>
<li> user touch interaction</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// image-related properties</span>
<span style="color: #000066; font-weight: bold;">int</span> imageHeight <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> imageWidth <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Image</span> image <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;">// scroll properties</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">int</span> translationX <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">int</span> translationY <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// touch properties</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">int</span> lastPointerX <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">int</span> lastPointerY <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Class constructor</strong> is quite straightforward, and only needs an Image as argument:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> ScrollableImageCanvas<span style="color: #009900;">&#40;</span><span style="color: #003399;">Image</span> image<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;">image</span> <span style="color: #339933;">=</span> image<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">imageWidth</span> <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>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">imageHeight</span> <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>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Also <strong>paint()</strong> implementation is simple, since it simply draws the given Image at current translation x and y coordinates:</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>
	g.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span>0xffffff<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>
	g.<span style="color: #006633;">drawImage</span><span style="color: #009900;">&#40;</span>image, <span style="color: #339933;">-</span> translationX, <span style="color: #339933;">-</span> translationY, <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></pre></div></div>

<p>Finally, we must implement the touch-based scrolling functionality. To do this, we&#8217;ll override the 3 pointer handlers provided by Canvas objects:</p>
<ul>
<li> <strong>pointerPressed</strong>: called when the pointer is pressed</li>
<li> <strong>pointerReleased</strong>: called when the pointer is released</li>
<li> <strong>pointerDragged</strong>: called when the pointer is dragged</li>
</ul>

<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> pointerPressed<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> x, <span style="color: #000066; font-weight: bold;">int</span> y<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	lastPointerX <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span>
	lastPointerY <span style="color: #339933;">=</span> y<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> pointerReleased<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> x, <span style="color: #000066; font-weight: bold;">int</span> y<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	lastPointerX <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	lastPointerY <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> pointerDragged<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> x, <span style="color: #000066; font-weight: bold;">int</span> y<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	scrollImage<span style="color: #009900;">&#40;</span>lastPointerX <span style="color: #339933;">-</span> x, lastPointerY <span style="color: #339933;">-</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	lastPointerX <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span>
	lastPointerY <span style="color: #339933;">=</span> y<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The <strong>scrollImage()</strong> method implementation follows. What it does is:</p>
<ul>
<li><strong>increment the current translation</strong> x and y coordinated by the given x and y deltas</li>
<li><strong>normalize the new translation</strong> x and y coordinates, so that Image will not go out of bounds</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">void</span> scrollImage<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> deltaX, <span style="color: #000066; font-weight: bold;">int</span> deltaY<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>imageWidth <span style="color: #339933;">&gt;</span> getWidth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		translationX <span style="color: #339933;">+=</span> deltaX<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>translationX <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
			translationX <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</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>translationX <span style="color: #339933;">+</span> getWidth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> imageWidth<span style="color: #009900;">&#41;</span>
			translationX <span style="color: #339933;">=</span> imageWidth <span style="color: #339933;">-</span> getWidth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>imageHeight <span style="color: #339933;">&gt;</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>
		translationY <span style="color: #339933;">+=</span> deltaY<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>translationY <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
			translationY <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</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>translationY <span style="color: #339933;">+</span> getHeight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> imageHeight<span style="color: #009900;">&#41;</span>
			translationY <span style="color: #339933;">=</span> imageHeight <span style="color: #339933;">-</span> getHeight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</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></pre></div></div>

<p>Complete class source code is available here: <a title="ScrollableImageCanvas java source code" href="http://www.jappit.com/uploads/src/ScrollableImageCanvas.java">ScrollableImageCanvas.java</a>.</p>
<p>If you like this article, feel free to rate it on <a title="How to implement touch image scrolling in Java ME Wiki article" href="http://wiki.forum.nokia.com/index.php?title=How_to_implement_touch_image_scrolling_in_Java_ME" target="_blank">Forum Nokia Wiki</a>.</p>
<h3>Further development</h3>
<p>A lot of <strong>improvements</strong> can be done to the code presented in this article. Just some examples are:</p>
<ul>
<li> <strong>scrollbars</strong>: a fundamental element to let the user know how much he can scroll both in vertical and horizontal directions</li>
<li> <strong>smooth scrolling</strong>: using some inertia when applying scrolling movement will make the whole scrolling effect a lot more realistic</li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/10/06/touch-and-j2me-part-1-how-scroll-an-image/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Building a J2ME sliding menu with text and images</title>
		<link>http://www.jappit.com/blog/2008/09/15/building-a-j2me-sliding-menu-with-text-and-images/</link>
		<comments>http://www.jappit.com/blog/2008/09/15/building-a-j2me-sliding-menu-with-text-and-images/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 14:09:25 +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[java me]]></category>
		<category><![CDATA[menu]]></category>
		<category><![CDATA[menu transition]]></category>
		<category><![CDATA[sliding menu]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=81</guid>
		<description><![CDATA[Whatever you&#8217;re building, a game or an application, you always need a menu to let users navigate through sections of your MIDlet. This article will show how to build a menu with text and icons, and with a nice sliding effect to go from one menu item to another. You can see the final effect [...]]]></description>
			<content:encoded><![CDATA[<p>Whatever you&#8217;re building, <strong>a game or an application</strong>, you always <strong>need a menu</strong> to let users navigate through sections of your MIDlet.</p>
<p>This article will show <strong>how to build a menu with text and icons</strong>, and with a nice <strong>sliding effect</strong> to go from one menu item to another. You can see <strong>the final effect on the emulator page</strong>: <a title="J2ME Sliding Menu" href="http://www.jappit.com/index.php?page=emulator&#038;midlet=slide_icons_menu" target="_blank">J2ME Sliding Menu in action</a>.</p>
<p><img class="alignnone" title="J2ME Sliding menu screenshot" src="http://www.jappit.com/images/blog/uploads/j2me_sliding_menu.png" alt="" /></p>
<h3>Source code: SlideIconsMenu class</h3>
<p>As always, let&#8217;s start <strong>defining our class</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> <span style="color: #000000; font-weight: bold;">class</span> SlideIconsMenu
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, we define some <strong>appearance-related properties</strong>, that can be customized to change menu colors and font.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// selected item index</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> selectedIndex <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// icon label color</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> textColor <span style="color: #339933;">=</span> 0xff0000<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// menu bg color</span>
<span style="color: #000000; font-weight: bold;">public</span> <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: #666666; font-style: italic;">// icon label font</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Font</span> textFont <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: #666666; font-style: italic;">// menu right and left Images</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Image</span> slideRightImage <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;">public</span> <span style="color: #003399;">Image</span> slideLeftImage <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span></pre></div></div>

<p>And some other properties, that will be internally used by the menu class itself, to <strong>handle content and the sliding animation</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// menu size</span>
<span style="color: #000066; font-weight: bold;">int</span> width <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> height <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// item labels</span>
<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> labels <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;">// item icons</span>
<span style="color: #003399;">Image</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> icons <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;">// previous item index (during menu translation)</span>
<span style="color: #000066; font-weight: bold;">int</span> prevIndex <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// menu sliding translation properties</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> translationDuration <span style="color: #339933;">=</span> <span style="color: #cc66cc;">500</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">long</span> startTranslationTime <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, let&#8217;s define the <strong>menu constructor</strong>. We need a constructor that will accept these arguments:</p>
<ul>
<li>a set of item <strong>labels</strong></li>
<li>a set of <strong>images</strong>, one for each item</li>
<li>the menu <strong>width</strong></li>
<li>and the menu <strong>height</strong></li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> SlideIconsMenu<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> labels, <span style="color: #003399;">Image</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> icons, <span style="color: #000066; font-weight: bold;">int</span> width, <span style="color: #000066; font-weight: bold;">int</span> height<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">width</span> <span style="color: #339933;">=</span> width<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">height</span> <span style="color: #339933;">=</span> height<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">labels</span> <span style="color: #339933;">=</span> labels<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">icons</span> <span style="color: #339933;">=</span> icons<span style="color: #339933;">;</span>
&nbsp;
	slideRightImage <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;/slide_right.png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	slideLeftImage <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;/slide_left.png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The constructor also creates <strong>2 Images to represent the left and right sliding arrows</strong>, that will be used to indicate more items on the menu left/right side. So, in your code, you must <strong>adapt those image paths</strong> to match existing ones within your project.</p>
<p>Now we must handle item change, by letting our menu slide with a nice transition. To do this, we&#8217;ll manage the item change with the following <strong>slideItem()</strong> method:</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;">void</span> slideItem<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> delta<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>isTranslating<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> selectedIndex <span style="color: #339933;">+</span> delta <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> selectedIndex <span style="color: #339933;">+</span> delta <span style="color: #339933;">&lt;</span> labels.<span style="color: #006633;">length</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		prevIndex <span style="color: #339933;">=</span> selectedIndex<span style="color: #339933;">;</span>
&nbsp;
		selectedIndex <span style="color: #339933;">+=</span> delta<span style="color: #339933;">;</span>
&nbsp;
		startTranslationTime <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</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: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isTranslating<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> prevIndex <span style="color: #339933;">!=</span> selectedIndex<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And finally, since we need to paint our menu, here is its <strong>paint()</strong> method:</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;">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>
	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>, width, height<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>textColor<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>selectedIndex <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</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>slideLeftImage, <span style="color: #cc66cc;">2</span>, height <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span>, <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">LEFT</span> <span style="color: #339933;">|</span> <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">VCENTER</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;">if</span><span style="color: #009900;">&#40;</span>selectedIndex <span style="color: #339933;">&lt;</span> icons.<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>
		g.<span style="color: #006633;">drawImage</span><span style="color: #009900;">&#40;</span>slideRightImage, width <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span>, height <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span>, <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">RIGHT</span> <span style="color: #339933;">|</span> <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">VCENTER</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	g.<span style="color: #006633;">drawString</span><span style="color: #009900;">&#40;</span>labels<span style="color: #009900;">&#91;</span>selectedIndex<span style="color: #009900;">&#93;</span>, width <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span>, height <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span>, <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">BOTTOM</span> <span style="color: #339933;">|</span> <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">HCENTER</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	g.<span style="color: #006633;">setClip</span><span style="color: #009900;">&#40;</span>slideLeftImage.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #cc66cc;">0</span>, width <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> slideLeftImage.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, height<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>selectedIndex <span style="color: #339933;">!=</span> prevIndex<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">int</span> diff <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> startTranslationTime<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>diff <span style="color: #339933;">&gt;</span> translationDuration<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			diff <span style="color: #339933;">=</span> translationDuration<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">int</span> coeff <span style="color: #339933;">=</span> selectedIndex <span style="color: #339933;">&gt;</span> prevIndex <span style="color: #339933;">?</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> currentX <span style="color: #339933;">=</span> width <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">-</span> coeff <span style="color: #339933;">*</span> diff <span style="color: #339933;">*</span> width <span style="color: #339933;">/</span> translationDuration<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> nextX <span style="color: #339933;">=</span> currentX <span style="color: #339933;">+</span> width <span style="color: #339933;">*</span> coeff<span style="color: #339933;">;</span>
&nbsp;
		g.<span style="color: #006633;">drawImage</span><span style="color: #009900;">&#40;</span>icons<span style="color: #009900;">&#91;</span>prevIndex<span style="color: #009900;">&#93;</span>, currentX, height <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span>, <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">VCENTER</span> <span style="color: #339933;">|</span> <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">HCENTER</span><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>icons<span style="color: #009900;">&#91;</span>selectedIndex<span style="color: #009900;">&#93;</span>, nextX, height <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span>, <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">VCENTER</span> <span style="color: #339933;">|</span> <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">HCENTER</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>diff <span style="color: #339933;">&gt;=</span> translationDuration<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			prevIndex <span style="color: #339933;">=</span> selectedIndex<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">else</span>
	<span style="color: #009900;">&#123;</span>
		g.<span style="color: #006633;">drawImage</span><span style="color: #009900;">&#40;</span>icons<span style="color: #009900;">&#91;</span>selectedIndex<span style="color: #009900;">&#93;</span>, width <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span>, height <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span>, <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">VCENTER</span> <span style="color: #339933;">|</span> <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">HCENTER</span><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>

<h3>Sample usage</h3>
<p>Here&#8217;s a <strong>sample Canvas that uses the SlideIconsMenu class</strong>. The main steps are:</p>
<ul>
<li>the <strong>menu constructions</strong></li>
<li>the <strong>key handling</strong>, done within the Canvas keyPressed() method</li>
<li>the <strong>menu repainting</strong>, done periodically, to allow the sliding transition to be smoothly drawn</li>
</ul>

<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;">javax.microedition.lcdui.Canvas</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.microedition.lcdui.Graphics</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.microedition.lcdui.Image</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SlideIconsCanvas <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Canvas</span> <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span>
<span style="color: #009900;">&#123;</span>
	SlideIconsMenu menu <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> SlideIconsCanvas<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Image</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> im <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: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
			im<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <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;/item0.png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			im<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <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;/item1.png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			im<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <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;/item2.png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			menu <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SlideIconsMenu<span style="color: #009900;">&#40;</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>,
				im,
				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: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</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: #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: #009900;">&#125;</span>
	<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>
		menu.<span style="color: #006633;">paint</span><span style="color: #009900;">&#40;</span>g<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</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> gameKey <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: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>gameKey <span style="color: #339933;">==</span> <span style="color: #003399;">Canvas</span>.<span style="color: #006633;">RIGHT</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			menu.<span style="color: #006633;">slideItem</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>gameKey <span style="color: #339933;">==</span> <span style="color: #003399;">Canvas</span>.<span style="color: #006633;">LEFT</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			menu.<span style="color: #006633;">slideItem</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: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<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;">try</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				repaint<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;">synchronized</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					wait<span style="color: #009900;">&#40;</span>100L<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: #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: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Download source code</h3>
<p>You can <strong>download full source</strong> code here:</p>
<ul>
<li><a title="SlideIconsMenu source code" href="http://www.jappit.com/uploads/src/SlideIconsMenu.java">SlideIconsMenu.java</a>: menu source code</li>
<li><a title="SlideIconsCanvas source code" href="http://www.jappit.com/uploads/src/SlideIconsCanvas.java">SlideIconsCanvas.java</a>: sample Canvas using the SlideIconsMenu</li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/09/15/building-a-j2me-sliding-menu-with-text-and-images/feed/</wfw:commentRss>
		<slash:comments>6</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>Let your images explode in J2ME!</title>
		<link>http://www.jappit.com/blog/2008/05/20/let-your-images-explode-in-j2me/</link>
		<comments>http://www.jappit.com/blog/2008/05/20/let-your-images-explode-in-j2me/#comments</comments>
		<pubDate>Tue, 20 May 2008 09:26:17 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[j2me]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[effect]]></category>
		<category><![CDATA[explode]]></category>
		<category><![CDATA[fx]]></category>
		<category><![CDATA[Image]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[transition]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=42</guid>
		<description><![CDATA[After a MIDP 1.0 utility to rotate images now time has come for some image fun When writing mobile applications, it&#8217;s always cool to add some effects or transitions. But, while for example FlashLite has a nice builtin support for them, with J2ME you have to hand-code even the simplest movement (and this is the [...]]]></description>
			<content:encoded><![CDATA[<p>After <a title="Rotate images in J2ME using MIDP 1.0" href="http://www.jappit.com/blog/2008/05/19/rotating-images-in-j2me-using-midp-10/">a MIDP 1.0 utility to rotate images</a> now time has come for some image fun <img src='http://www.jappit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><img src="http://www.jappit.com/images/blog/uploads/j2me_image_explode.png" alt="J2ME Image explode effect screenshot" width="302" height="196" /><br />
When writing mobile applications, it&#8217;s always cool to add some effects or transitions. But, while for example FlashLite has a nice builtin support for them, with J2ME you have to hand-code even the simplest movement (and this is the main reason why most J2ME apps are all but attractive).</p>
<p>So, here&#8217;s a first class that you can use to add an &#8220;explode&#8221; effect to images in a straightforward way. How to do it? Here we come:</p>
<ol>
<li><strong>Download</strong> the <a title="ExplodingImage.java source code" href="http://www.jappit.com/uploads/src/ExplodingImage.java">ExplodingImage.java</a> source code and put it straight in your project</li>
<li><strong>Instantiate</strong> an ExplodingImage this way:

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//get your Image</span>
<span style="color: #003399;">Image</span> sourceImage <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;/image.png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//and then use it in ExplodingImage constructor</span>
ExplodingImage image <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ExplodingImage<span style="color: #009900;">&#40;</span>sourceImage , <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">8</span>, <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The ExplodingImage constructor accepts the following arguments:</p>
<ul>
<li>An <em>Image</em> instance</li>
<li>An <em>int</em> representing the <strong>&#8220;level&#8221;</strong> for the exploding effect, that is the <em>strength </em>of the effect itself (higher the level, stronger the effect).</li>
<li>The last 2 <em>int</em> arguments represent the horizontal and vertical pieces of the exploded image.</li>
</ul>
</li>
<li><strong>Start</strong> the explode effect with the explode() method, that will accept the effect duration as argument:

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">image.<span style="color: #006633;">explode</span><span style="color: #009900;">&#40;</span>2000L<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li>To <strong>paint</strong> it, simply use its paint() method, very similary to the Graphics drawImage() one. For example, in a Canvas paint() method, you can do something like this:

<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>
	g.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span>0xffffff<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>, width, height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	image.<span style="color: #006633;">paint</span><span style="color: #009900;">&#40;</span>g, getWidth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span>, getHeight<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: #003399;">Graphics</span>.<span style="color: #006633;">HCENTER</span> <span style="color: #339933;">|</span> <span style="color: #003399;">Graphics</span>.<span style="color: #006633;">VCENTER</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To give the effect a &#8220;smooth&#8221; animation, you should paint it quite frequently (let&#8217;s say, not once per second <img src='http://www.jappit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ). So, always using Canvas, a sample code could be like this:</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;">void</span> run<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;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		repaint<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;">try</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">synchronized</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				wait<span style="color: #009900;">&#40;</span>50L<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;">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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li>To <strong>test if the effect has ended</strong>, you can simply access your ExplodingImage <em>ended</em> instance variable:

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>image.<span style="color: #006633;">ended</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//effect-end related code</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li>And you&#8217;re done! See it in action here: <a title="J2ME explode effect in emulator" href="http://www.jappit.com/index.php?page=emulator&amp;midlet=image_explode">J2ME image explode effect in action</a></li>
</ol>
<p>Sample source code is available here:</p>
<ul>
<li><a title="ExplodingImage.java source code" href="http://www.jappit.com/uploads/src/ExplodingImage.java">ExplodingImage.java</a>: main class</li>
<li><a title="ExploginImageCanvas.java source code" href="http://www.jappit.com/uploads/src/ExplodingImageCanvas.java">ExplodingImageCanvas.java</a>: sample usage of ExplodingImage within a Canvas</li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/05/20/let-your-images-explode-in-j2me/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rotating images in J2ME using MIDP 1.0</title>
		<link>http://www.jappit.com/blog/2008/05/19/rotating-images-in-j2me-using-midp-10/</link>
		<comments>http://www.jappit.com/blog/2008/05/19/rotating-images-in-j2me-using-midp-10/#comments</comments>
		<pubDate>Mon, 19 May 2008 09:53:52 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[j2me]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[midp 1.0]]></category>
		<category><![CDATA[rotate image]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=40</guid>
		<description><![CDATA[So you&#8217;re still using MIDP 1.0 uh? And maybe you need to rotate an image? If this is your case, you could find useful this simple function: public static Image rotateImage&#40;Image image, int angle&#41; throws Exception &#123; if&#40;angle == 0&#41; &#123; return image; &#125; else if&#40;angle != 180 &#38;&#38; angle != 90 &#38;&#38; angle != [...]]]></description>
			<content:encoded><![CDATA[<p>So you&#8217;re still using MIDP 1.0 uh? And maybe you need to rotate an image?</p>
<p><img src="http://www.jappit.com/images/blog/uploads/j2me_rotate_image.png" alt="J2ME rotate image screenshot" width="297" height="152" /></p>
<p>If this is your case, you could  find useful this simple function:</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> rotateImage<span style="color: #009900;">&#40;</span><span style="color: #003399;">Image</span> image, <span style="color: #000066; font-weight: bold;">int</span> angle<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>angle <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> image<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>angle <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">180</span> <span style="color: #339933;">&amp;&amp;</span> angle <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">90</span> <span style="color: #339933;">&amp;&amp;</span> angle <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">270</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Exception</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Invalid angle&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">int</span> width <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>
	<span style="color: #000066; font-weight: bold;">int</span> height <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: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> rowData <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>width<span style="color: #009900;">&#93;</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> rotatedData <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>width <span style="color: #339933;">*</span> height<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">int</span> rotatedIndex <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</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> height<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		image.<span style="color: #006633;">getRGB</span><span style="color: #009900;">&#40;</span>rowData, <span style="color: #cc66cc;">0</span>, width, <span style="color: #cc66cc;">0</span>, i, width, <span style="color: #cc66cc;">1</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> width<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			rotatedIndex <span style="color: #339933;">=</span> 
				angle <span style="color: #339933;">==</span> <span style="color: #cc66cc;">90</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span>height <span style="color: #339933;">-</span> i <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> j <span style="color: #339933;">*</span> height <span style="color: #339933;">:</span> 
				<span style="color: #009900;">&#40;</span>angle <span style="color: #339933;">==</span> <span style="color: #cc66cc;">270</span> <span style="color: #339933;">?</span> i <span style="color: #339933;">+</span> height <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>width <span style="color: #339933;">-</span> j <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> 
					width <span style="color: #339933;">*</span> height <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">*</span> width <span style="color: #339933;">+</span> j<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span>
				<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			rotatedData<span style="color: #009900;">&#91;</span>rotatedIndex<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> rowData<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>angle <span style="color: #339933;">==</span> <span style="color: #cc66cc;">90</span> <span style="color: #339933;">||</span> angle <span style="color: #339933;">==</span> <span style="color: #cc66cc;">270</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: #003399;">Image</span>.<span style="color: #006633;">createRGBImage</span><span style="color: #009900;">&#40;</span>rotatedData, height, width, <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;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">Image</span>.<span style="color: #006633;">createRGBImage</span><span style="color: #009900;">&#40;</span>rotatedData, width, height, <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: #009900;">&#125;</span></pre></div></div>

<p>So, how can you use it? Nothing more than:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Image</span> original <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;/original_image.png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">Image</span> rotated_image <span style="color: #339933;">=</span> rotateImage<span style="color: #009900;">&#40;</span>original, <span style="color: #cc66cc;">90</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/05/19/rotating-images-in-j2me-using-midp-10/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A J2ME Calendar for all your Canvas!</title>
		<link>http://www.jappit.com/blog/2008/05/16/a-j2me-calendar-for-all-your-canvas/</link>
		<comments>http://www.jappit.com/blog/2008/05/16/a-j2me-calendar-for-all-your-canvas/#comments</comments>
		<pubDate>Fri, 16 May 2008 15:06:42 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[j2me]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[calendar]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[date field]]></category>
		<category><![CDATA[date item]]></category>
		<category><![CDATA[date picker]]></category>
		<category><![CDATA[forumnokia]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=39</guid>
		<description><![CDATA[You know that J2ME support for Canvas is quite ridiculous.. One list, some form items, and stop. Canvas is left to its terrible destiny, with nothing more than a couple of lines and circles.. isn&#8217;t it sad? So, after this melodramatic introduction, we&#8217;re ready for today&#8217;s code: a fully featured, customizable, Canvas based calendar! If [...]]]></description>
			<content:encoded><![CDATA[<p>You know that J2ME support for Canvas is quite ridiculous.. One list, some form items, and stop. Canvas is left to its terrible destiny, with nothing more than a couple of lines and circles.. isn&#8217;t it sad?<br />
So, after this melodramatic introduction, we&#8217;re ready for today&#8217;s code: a fully featured, customizable, Canvas based calendar!</p>
<p><img src="http://www.jappit.com/images/blog/uploads/j2me_calendar.png" alt="J2me Canvas Date Picker screenshot" width="253" height="223" /></p>
<p>If you prefer a live demonstration rather than a simple screenshot, just go here: <a title="J2me Canvas date picker in emulator" href="http://www.jappit.com/index.php?page=emulator&amp;midlet=calendar_widget">Canvas Calendar in action</a>.</p>
<p>So, how to use it?</p>
<ol>
<li><strong>Download</strong> its source code (<a title="J2me calendar date picker source code" href="http://www.jappit.com/uploads/src/CalendarWidget.java">CalendarWidget.java</a>) and put it straight in your project</li>
<li><strong>Instantiate </strong>it within your Canvas with its plain-old-unique constructor:

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">CalendarWidget calendar <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CalendarWidget<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li><strong>Customize </strong>it with the colors/fonts/padding you prefer:

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">calendar.<span style="color: #006633;">headerFont</span> <span style="color: #339933;">=</span> <span style="color: #003399;">Font</span>.<span style="color: #006633;">getFont</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Font</span>.<span style="color: #006633;">FACE_PROPORTIONAL</span>, <span style="color: #003399;">Font</span>.<span style="color: #006633;">STYLE_BOLD</span>, <span style="color: #003399;">Font</span>.<span style="color: #006633;">SIZE_LARGE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calendar.<span style="color: #006633;">weekdayFont</span> <span style="color: #339933;">=</span> <span style="color: #003399;">Font</span>.<span style="color: #006633;">getFont</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Font</span>.<span style="color: #006633;">FACE_PROPORTIONAL</span>, <span style="color: #003399;">Font</span>.<span style="color: #006633;">STYLE_BOLD</span>, <span style="color: #003399;">Font</span>.<span style="color: #006633;">SIZE_MEDIUM</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calendar.<span style="color: #006633;">weekdayBgColor</span> <span style="color: #339933;">=</span> 0xccccff<span style="color: #339933;">;</span>
calendar.<span style="color: #006633;">weekdayColor</span> <span style="color: #339933;">=</span> 0x0000ff<span style="color: #339933;">;</span>
calendar.<span style="color: #006633;">headerColor</span> <span style="color: #339933;">=</span> 0xffffff<span style="color: #339933;">;</span></pre></div></div>

</li>
<li>After you&#8217;ve customized it, remember to always call its <strong>initialize</strong>() method:

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">calendar.<span style="color: #006633;">initialize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li>Now, to paint it, you can simply call its paint() method from your Canvas paint(), like this:

<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>
    g.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span>0xffffff<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>
    calendar.<span style="color: #006633;">paint</span><span style="color: #009900;">&#40;</span>g<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li>Now you must allow users to interact with it, so you can, for example, use Canvas keyPressed() method to interact with <em>calendar</em>:

<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>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>keyCode <span style="color: #339933;">==</span> FIRE<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        Display.<span style="color: #006633;">getDisplay</span><span style="color: #009900;">&#40;</span>midlet<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setCurrent</span><span style="color: #009900;">&#40;</span>
            <span style="color: #000000; font-weight: bold;">new</span> Alert<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Selected date&quot;</span>, calendar.<span style="color: #006633;">getSelectedDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">null</span>, AlertType.<span style="color: #006633;">CONFIRMATION</span><span style="color: #009900;">&#41;</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: #009900;">&#123;</span>
        calendar.<span style="color: #006633;">keyPressed</span><span style="color: #009900;">&#40;</span>keyCode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        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></pre></div></div>

<p>As you see, what we do is this:</p>
<ul>
<li>if the user press FIRE button, we alert the current selected date</li>
<li>otherwise we call <em>calendar</em> keyPressed() method, to make it behave accordingly</li>
</ul>
</li>
<li>Other customizable properties include:
<ul>
<li><strong>MONTH_LABELS</strong>: change this to customize month labels in your own language</li>
<li><strong>WEEKDAY_LABELS</strong>: as above, change this to customize weekday labels</li>
<li><strong>startWeekday</strong>: this represents the week starting day, and its values range goes from 0 (for Monday) to 6 (for Sunday)</li>
</ul>
</li>
</ol>
<p>You can download source code of the example described above here: <a title="J2me Calendar example source code" href="http://www.jappit.com/uploads/src/CalendarCanvas.java">CalendarCanvas.java</a>.</p>
<p>To get some more details about CalendarWidget source code, you can take a look at my article on Forum Nokia Wiki: <a title="J2ME Canvas based Calendar / Date Picker" href="http://wiki.forum.nokia.com/index.php/Building_a_J2ME_Canvas_based_calendar/date_picker" target="_blank">Building a J2ME Canvas based Calendar / Date picker</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/05/16/a-j2me-calendar-for-all-your-canvas/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

