<?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; fx</title>
	<atom:link href="http://www.jappit.com/blog/tag/fx/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>ImageFx: first commercial application is out!</title>
		<link>http://www.jappit.com/blog/2008/10/13/imagefx-first-commercial-application-is-out/</link>
		<comments>http://www.jappit.com/blog/2008/10/13/imagefx-first-commercial-application-is-out/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 11:23:35 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[advertising]]></category>
		<category><![CDATA[cool stuff]]></category>
		<category><![CDATA[j2me]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[effects]]></category>
		<category><![CDATA[fx]]></category>
		<category><![CDATA[image library]]></category>
		<category><![CDATA[imagefx]]></category>
		<category><![CDATA[java me]]></category>
		<category><![CDATA[transitions]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=163</guid>
		<description><![CDATA[ImageFx is a Java ME library that allows you to easily add animated transitions between different Images. And today Systema Mutui, the first commercial applications that makes use of it, has finally been launched The application itself is a simple animation that promotes bank services and allows users to share the animation itself with a [...]]]></description>
			<content:encoded><![CDATA[<p><a title="ImageFx launch" href="http://www.jappit.com/blog/2008/06/06/imagefx-j2me-image-effects-library/">ImageFx</a> is a <strong>Java ME library</strong> that allows you to <strong>easily add animated transitions between different Images</strong>.</p>
<p>And today <strong>Systema Mutui</strong>, the <strong>first commercial applications</strong> that makes use of it, has finally <strong>been launched</strong> <img src='http://www.jappit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><img class="alignnone" title="Systema Mutui screenshot" src="http://www.jappit.com/images/blog/uploads/systema_mutui.jpg" alt="" /></p>
<p>The application itself is a <strong>simple animation</strong> that promotes bank services and allows users to share the animation itself with a friend. Obviously, <a title="ImageFx transitions examples" href="http://www.jappit.com/index.php?page=emulator&amp;midlet=image_fx" target="_blank">ImageFx transitions</a> were used to <strong>make the whole animation nicer</strong> <img src='http://www.jappit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>To get further details about <strong>ImageFx API features</strong>, you can access its <strong>full JavaDocs</strong> here: <a title="ImageFx JavaDocs" href="http://www.jappit.com/j2me/imagefx/doc/" target="_blank">ImageFx JavaDocs</a>. For any other infos, feel free to <a title="About us page" href="http://www.jappit.com/index.php?page=about_us" target="_blank">contact me</a>!</p>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/10/13/imagefx-first-commercial-application-is-out/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to add sliding transitions between Canvas in J2ME</title>
		<link>http://www.jappit.com/blog/2008/06/23/how-to-add-sliding-transitions-between-canvas-in-j2me/</link>
		<comments>http://www.jappit.com/blog/2008/06/23/how-to-add-sliding-transitions-between-canvas-in-j2me/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 12:39:03 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[cool stuff]]></category>
		<category><![CDATA[flash lite]]></category>
		<category><![CDATA[j2me]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[symbian]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[fx]]></category>
		<category><![CDATA[java me]]></category>
		<category><![CDATA[sliding animations]]></category>
		<category><![CDATA[transitions]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=53</guid>
		<description><![CDATA[Today we&#8217;ll see a simple way to add sliding transition between Canvas in a Java ME application. Just take a look at it in the emulator page to see how it performs: Canvas sliding transitions in action! The source code FxBaseCanvas First of all, we know that Canvas paint() method is protected, so, to access [...]]]></description>
			<content:encoded><![CDATA[<p>Today we&#8217;ll see a simple way to add <strong>sliding transition between Canvas</strong> in a Java ME application. Just take a look at it in the emulator page to see how it performs: <a title="Java ME Canvas sliding transition in action" href="http://www.jappit.com/index.php?page=emulator&amp;midlet=canvas_sliding_fx">Canvas sliding transitions in action</a>!</p>
<p><img src="http://www.jappit.com/images/blog/uploads/j2me_canvas_sliding_fx.jpg" alt="j2me canvas sliding transition screenshot" width="247" height="185" /></p>
<h3>The source code</h3>
<h4>FxBaseCanvas</h4>
<p>First of all, we know that <strong>Canvas</strong> <strong>paint()</strong> method is protected, so, to access it from the class we&#8217;ll build, we must extend its visibility by <strong>extending Canvas class itself</strong>. We&#8217;ll do it by defining our <strong>FxBaseClass</strong> in this simple way:</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;">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.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>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FxBaseCanvas <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Canvas</span>
<span style="color: #009900;">&#123;</span>
	<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>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4>CanvasSlideFx</h4>
<p>Now, we start building our <strong>CanvasSlideFx </strong>class, that will extend Canvas itself. We start defining some useful properties:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// time related properties</span>
<span style="color: #000066; font-weight: bold;">long</span> startTime <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">long</span> duration <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// current state of the transition</span>
<span style="color: #000066; font-weight: bold;">boolean</span> running <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// direction of sliding</span>
<span style="color: #000066; font-weight: bold;">int</span> direction <span style="color: #339933;">=</span> LEFT<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// the previous and next Canvas instances</span>
FxBaseCanvas fromCanvas <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
FxBaseCanvas toCanvas <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;">// the current Display object</span>
Display display <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;">// properties used to correctly place the 2 Canvas</span>
<span style="color: #000066; font-weight: bold;">int</span> deltaX <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> deltaY <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, we define our class constructor, that will initialize coordinate properties according to the specified transition direction. The detailed argument list is:</p>
<ul>
<li>The <strong>current Display object</strong>, that will be used to retrieve the current displayed Canvas, and to set the next one</li>
<li><strong>The destination Canvas</strong>, that will extend the previously defined FxBaseCanvas class</li>
<li>The transition <strong>direction</strong> (one of Canvas.UP, RIGHT, DOWN or LEFT)</li>
<li>The transition <strong>duration</strong>, in milliseconds</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> CanvasSlideFx<span style="color: #009900;">&#40;</span>Display display, FxBaseCanvas toCanvas, <span style="color: #000066; font-weight: bold;">int</span> direction, <span style="color: #000066; font-weight: bold;">long</span> duration<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;">display</span> <span style="color: #339933;">=</span> display<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">fromCanvas</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>FxBaseCanvas<span style="color: #009900;">&#41;</span>display.<span style="color: #006633;">getCurrent</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;">toCanvas</span> <span style="color: #339933;">=</span> toCanvas<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">direction</span> <span style="color: #339933;">=</span> direction<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">duration</span> <span style="color: #339933;">=</span> duration<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>direction<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">case</span> UP<span style="color: #339933;">:</span>
		deltaY <span style="color: #339933;">=</span> <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: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">case</span> RIGHT<span style="color: #339933;">:</span>
		deltaX <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: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">case</span> DOWN<span style="color: #339933;">:</span>
		deltaY <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: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">case</span> LEFT<span style="color: #339933;">:</span>
		deltaX <span style="color: #339933;">=</span> <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: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, we define a <strong>startTransition()</strong> method, that will actually start the animation. To implement the animation itself, we let our class implement <strong>Runnable</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">void</span> startTransition<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;">this</span>.<span style="color: #006633;">startTime</span> <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>
&nbsp;
	running <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</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></pre></div></div>

<p>And here&#8217;s the <strong>run()</strong> method implementation, that we must implement from Runnable interface. As long as the animation is running, we will repaint our animated Canvas, with a given interval of 50 milliseconds (but you can freely change this). When it finishes (so, running is false), we will exit the main loop and <strong>set the current Displayable</strong> to our <strong>toCanvas</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: #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>running<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>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: #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>
	display.<span style="color: #006633;">setCurrent</span><span style="color: #009900;">&#40;</span>toCanvas<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Finally, we have our <strong>paint()</strong> method, that will actually paint our transition. It will do this by <strong>painting both the source and the destination Canvas</strong>, translating them according to the transition direction, and to the elapsed time.</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: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>running<span style="color: #009900;">&#41;</span>
		startTransition<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;">long</span> diff <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> startTime<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> duration<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		running <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
		diff <span style="color: #339933;">=</span> duration<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">int</span> perc <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: #cc66cc;">100</span> <span style="color: #339933;">*</span> diff <span style="color: #339933;">/</span> duration<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">int</span> dx <span style="color: #339933;">=</span> deltaX <span style="color: #339933;">*</span> perc <span style="color: #339933;">/</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">int</span> dy <span style="color: #339933;">=</span> deltaY <span style="color: #339933;">*</span> perc <span style="color: #339933;">/</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
&nbsp;
	g.<span style="color: #006633;">translate</span><span style="color: #009900;">&#40;</span>dx, dy<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	fromCanvas.<span style="color: #006633;">paint</span><span style="color: #009900;">&#40;</span>g<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	g.<span style="color: #006633;">translate</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span> deltaX, <span style="color: #339933;">-</span> deltaY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	toCanvas.<span style="color: #006633;">paint</span><span style="color: #009900;">&#40;</span>g<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	g.<span style="color: #006633;">translate</span><span style="color: #009900;">&#40;</span>deltaX <span style="color: #339933;">-</span> dx, deltaY <span style="color: #339933;">-</span> dy<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>How to use it?</h3>
<p>Now we&#8217;ll see how to <strong>integrate the CanvasSlideFx within an existing code</strong> that already uses Canvas.<br />
So, let&#8217;s assume that our application currently has, somewhere in its code, these lines:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">MyCanvas firstCanvas <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyCanvas<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Display.<span style="color: #006633;">getDisplay</span><span style="color: #009900;">&#40;</span>myMidlet<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setCurrent</span><span style="color: #009900;">&#40;</span>firstCanvas<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
...
<span style="color: #666666; font-style: italic;">//and somewhere else</span>
MyOtherCanvas secondCanvas <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherCanvas<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Display.<span style="color: #006633;">getDisplay</span><span style="color: #009900;">&#40;</span>myMidlet<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setCurrent</span><span style="color: #009900;">&#40;</span>secondCanvas<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>where MyCanvas and MyOtherCanvas will likely extend Canvas.</p>
<p>Now here are the required steps to integrate transitions in our code:</p>
<ol>
<li> First of all, we must make <strong>MyCanvas and MyOtherCanvas extend FxBaseCanvas </strong>instead of directly Canvas. So, we&#8217;ll have:

<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> MyCanvas extend FxBaseCanvas ...</pre></div></div>

</li>
<li> Then, let&#8217;s say we want to animate the transition between firstCanvas and secondCanvas, we will <strong>remove the code</strong>:

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

<p>and <strong>replace it </strong>with:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">CanvasSlideFx fxCanvas <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CanvasSlideFx<span style="color: #009900;">&#40;</span>
	Display.<span style="color: #006633;">getDisplay</span><span style="color: #009900;">&#40;</span>myMidlet<span style="color: #009900;">&#41;</span>,
	secondCanvas,
	<span style="color: #003399;">Canvas</span>.<span style="color: #006633;">LEFT</span>,
	500L
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Display.<span style="color: #006633;">getDisplay</span><span style="color: #009900;">&#40;</span>myMidlet<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setCurrent</span><span style="color: #009900;">&#40;</span>fxCanvas<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li> and we&#8217;ve done it! <img src='http://www.jappit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ol>
<h3>Resources and download</h3>
<p>You can download the code explained in this article with the following links:</p>
<ul>
<li><a title="FxBaseCanvas class source code" href="http://www.jappit.com/uploads/src/FxBaseCanvas.java">FxBaseCanvas.java</a>: our base class extending Canvas</li>
<li><a title="CanvasSlideFx class source code" href="http://www.jappit.com/uploads/src/CanvasSlideFx.java">CanvasSlideFx.java</a>: the class used to implement the sliding transition</li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/06/23/how-to-add-sliding-transitions-between-canvas-in-j2me/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to create a color fading text in J2ME</title>
		<link>http://www.jappit.com/blog/2008/06/18/create-color-fading-text-in-j2me/</link>
		<comments>http://www.jappit.com/blog/2008/06/18/create-color-fading-text-in-j2me/#comments</comments>
		<pubDate>Wed, 18 Jun 2008 11:40:54 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[j2me]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[colors]]></category>
		<category><![CDATA[effects]]></category>
		<category><![CDATA[fading]]></category>
		<category><![CDATA[forumnokia]]></category>
		<category><![CDATA[fx]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[transitions]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=51</guid>
		<description><![CDATA[It&#8217;s time for new effects! Today we&#8217;ll see how to implement and and use text that changes multiple colors with a fading effect. You can see it in action here: J2ME text color fade effect in action. So, let&#8217;s start defining our ColorFadeText class. The code We start defining the necessary properties: //will hold the [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s time for new effects! <img src='http://www.jappit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Today we&#8217;ll see how to implement and and use <strong>text that changes multiple colors with a fading effect</strong>. You can see it in action here: <a title="Sample midlet using ColorFadeText class" href="http://www.jappit.com/index.php?page=emulator&#038;midlet=color_fade_text">J2ME text color fade effect in action</a>.</p>
<p><img src="http://www.jappit.com/images/blog/uploads/j2me_color_fade_text.gif" alt="Color Fading Text screenshot" /></p>
<p>So, let&#8217;s start defining our <strong>ColorFadeText </strong>class.</p>
<h3>The code</h3>
<p>We start defining the necessary <strong>properties</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//will hold the colors to fade</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> colors <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;">//duration of a single fade</span>
<span style="color: #000066; font-weight: bold;">int</span> fadeDuration <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//effect start time</span>
<span style="color: #000066; font-weight: bold;">long</span> startTime <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//property used to check if effect has started</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> started <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//the text to be drawn</span>
<span style="color: #003399;">String</span> text <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, we define the main <strong>constructor</strong>, that will accept the following arguments:</p>
<ul>
<li>the <strong>text </strong>to be drawn</li>
<li>an int[] array containing the <strong>colors </strong>to fade into</li>
<li>the <strong>duration </strong>of a single fade</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> ColorFadeText<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> text, <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> colors, <span style="color: #000066; font-weight: bold;">int</span> fadeDuration<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>colors.<span style="color: #006633;">length</span> <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;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;You must define at least 1 color&quot;</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;">this</span>.<span style="color: #006633;">text</span> <span style="color: #339933;">=</span> text<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">colors</span> <span style="color: #339933;">=</span> colors<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">fadeDuration</span> <span style="color: #339933;">=</span> fadeDuration<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The effect <strong>start()</strong> method will simply set the startTime value to current time and the started property to true</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> start<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	startTime <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>
&nbsp;
	started <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then, we have the <strong>paint()</strong> method, that will be used to paint the text on the given Graphics instance:</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: #000066; font-weight: bold;">int</span> x, <span style="color: #000066; font-weight: bold;">int</span> y, <span style="color: #000066; font-weight: bold;">int</span> anchor<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>started<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">long</span> diff <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> startTime<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">int</span> module <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>diff <span style="color: #339933;">%</span> fadeDuration<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">int</span> colorIndex <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>diff <span style="color: #339933;">/</span> fadeDuration<span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> colors.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">int</span> midColor <span style="color: #339933;">=</span> midColor<span style="color: #009900;">&#40;</span>
			colors<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span>colorIndex <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> colors.<span style="color: #006633;">length</span><span style="color: #009900;">&#93;</span>,
			colors<span style="color: #009900;">&#91;</span>colorIndex<span style="color: #009900;">&#93;</span>,
			module,
			fadeDuration
		<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>midColor<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>
		g.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span>colors<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	g.<span style="color: #006633;">drawString</span><span style="color: #009900;">&#40;</span>text, x, y, anchor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The following utility method will be the one actually used <strong>to get the current text color</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> midColor<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> color1, <span style="color: #000066; font-weight: bold;">int</span> color2, <span style="color: #000066; font-weight: bold;">int</span> prop, <span style="color: #000066; font-weight: bold;">int</span> max<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">int</span> red <span style="color: #339933;">=</span>
		<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>color1 <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> 0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> prop <span style="color: #339933;">+</span>
		<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>color2 <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> 0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>max <span style="color: #339933;">-</span> prop<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> max<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">int</span> green <span style="color: #339933;">=</span>
		<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>color1 <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> 0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> prop <span style="color: #339933;">+</span>
		<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>color2 <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> 0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>max <span style="color: #339933;">-</span> prop<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> max<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">int</span> blue <span style="color: #339933;">=</span>
		<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>color1 <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> 0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> prop <span style="color: #339933;">+</span>
		<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>color2 <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> 0xff<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>max <span style="color: #339933;">-</span> prop<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> max<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">int</span> color <span style="color: #339933;">=</span> red <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">16</span> <span style="color: #339933;">|</span> green <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">8</span> <span style="color: #339933;">|</span> blue<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">return</span> color<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>How to use it?</h3>
<p>Using the <strong>ColorFadeText </strong>object is quite simple, since it&#8217;ll be very similar to use a common String. Just follow this plain steps.</p>
<p><strong>1. </strong><strong>Create </strong>a ColorFadeText instance</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ColorFadeText text <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ColorFadeText<span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">&quot;I'M A FADING TEXT!&quot;</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><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>0xff0000, 0x00ff00, 0x0000ff, 0xff00ff<span style="color: #009900;">&#125;</span>,
	<span style="color: #cc66cc;">1000</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>2. </strong><strong>Start </strong>it&#8230;</p>

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

<p><strong>3. </strong>And then, in your Canvas paint() method, <strong>paint </strong>it, using the same arguments used by Graphics drawString() method (adding a reference to the Graphics instance of course):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">text.<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>,
	<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;">TOP</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Just a side note: since it&#8217;s an animated effect, you&#8217;ll need to <strong>repaint it quite frequently</strong>, so, for example, you can use a Thread to periodically call Canvas repaint() method.</p>
<h3>Source code download</h3>
<p>You can download <strong>source code </strong>of ColorFadeText, and of a sample Canvas that makes use of it:</p>
<ul>
<li><a title="ColorFadeText class source code" href="http://www.jappit.com/uploads/src/ColorFadeText.java">ColorFadeText.java</a>: main class source code</li>
<li><a title="AnimatedTextCanvas class source code" href="http://www.jappit.com/uploads/src/AnimatedTextCanvas.java">AnimatedTextCanvas.java</a>: a Canvas using the ColorFadeText object</li>
</ul>
<p>This article is also available on <strong>Forum Nokia Wiki</strong>: <a title="How to create a color fading text in Java ME" href="http://wiki.forum.nokia.com/index.php/How_to_create_a_color_fading_text_in_Java_ME" target="_blank">How to create a color fading text in Java ME</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/06/18/create-color-fading-text-in-j2me/feed/</wfw:commentRss>
		<slash:comments>0</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>
	</channel>
</rss>

