<?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; effect</title>
	<atom:link href="http://www.jappit.com/blog/tag/effect/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>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>

