<?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; image fx</title>
	<atom:link href="http://www.jappit.com/blog/tag/image-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>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>ImageFx: J2ME image effects library</title>
		<link>http://www.jappit.com/blog/2008/06/06/imagefx-j2me-image-effects-library/</link>
		<comments>http://www.jappit.com/blog/2008/06/06/imagefx-j2me-image-effects-library/#comments</comments>
		<pubDate>Fri, 06 Jun 2008 13:37:57 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[j2me]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[animations]]></category>
		<category><![CDATA[effects]]></category>
		<category><![CDATA[image fx]]></category>
		<category><![CDATA[j2me api]]></category>
		<category><![CDATA[j2me library]]></category>
		<category><![CDATA[transitions]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=46</guid>
		<description><![CDATA[After two tutorials on Image animations in J2ME, I&#8217;ve finally decided to put together a library to easily integrate Image effects in a Java ME application. This first release is very very early, and is intended to be tested and to allow developers to give their feedbacks about it, so It&#8217;d be possible to modify [...]]]></description>
			<content:encoded><![CDATA[<p>After <a title="J2ME image sliding transitions" href="http://www.jappit.com/blog/2008/05/22/j2me-image-effects-part-2-sliding-transitions/">two</a> <a title="J2ME image explosion effect" href="http://www.jappit.com/blog/2008/05/20/let-your-images-explode-in-j2me/">tutorials</a> on Image animations in J2ME, I&#8217;ve finally decided to put together a <strong>library to easily integrate Image effects in a Java ME application</strong>. This first release is very very early, and is intended to be tested and to allow developers to give their feedbacks about it, so It&#8217;d be possible to modify and improve it.</p>
<p><img src="http://www.jappit.com/images/blog/uploads/imagefx_banner.gif" alt="ImageFx Banner" width="400" height="149" /></p>
<p>Currently supported effects are visible on the <a title="ImageFx in action" href="http://www.jappit.com/index.php?page=emulator&amp;midlet=image_fx">emulator page</a>, and are:</p>
<ul>
<li><strong>BlindsFx</strong></li>
<li><strong>ExplodeFx</strong></li>
<li><strong>PuzzleFx</strong></li>
<li><strong>ShakeFx</strong></li>
<li><strong>SlideFx</strong></li>
<li><strong>SpiralFx</strong></li>
<li><strong>WaveFx</strong></li>
<li><strong>WipeFx</strong></li>
</ul>
<h3>How to use it?</h3>
<p>Let&#8217;s start from a simple usage example. We start creating an Image, and then using it to create an AnimatedImage instance:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Image</span> baseImage <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;/base.png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
AnimatedImage animated <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AnimatedImage<span style="color: #009900;">&#40;</span>baseImage<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, we have created an <strong>AnimatedImage</strong>, that is the base class that will allow us to apply effects to <strong>Image</strong>s.</p>
<p>Now, we can create an effect (that will be a subclass of <strong>ImageFx</strong>) and apply it to our AnimatedImage:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ImageFx fx <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PuzzleFx<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">8</span>, <span style="color: #cc66cc;">8</span>, ImageFx.<span style="color: #006633;">TYPE_IN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
animated.<span style="color: #006633;">setFx</span><span style="color: #009900;">&#40;</span>fx<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In this example, we have chosen a <strong>PuzzleFx </strong>effect, that will show (or hide, depending on Fx type) our image piece by piece. As you can check on <a title="PuzzleFx JavaDocs" href="http://www.jappit.com/j2me/imagefx/doc/com/jappit/imagefx/fxs/PuzzleFx.html" target="_blank">JavaDocs page for PuzzleFx class</a>, first 2 arguments represent the horizontal and vertical pieces to split the Image into, while the last one is the type of the Fx itself.</p>
<p>Now, our AnimatedImage is ready to be animated. To start the animation, simply call the AnimatedImage <strong>start()</strong> method, passing as argument the <strong>effect duration in milliseconds</strong>:</p>

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

<p>About painting, you&#8217;ll simply have to call the <strong>paint()</strong> method, in a way that is really similar to Graphics drawImage():</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">animated.<span style="color: #006633;">paint</span><span style="color: #009900;">&#40;</span>g, <span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">100</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>

<p>Now, to begin using this library, you don&#8217;t really need any more infos. Just to point out some methods you can find useful in your app:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//stop() method will immediatly stop the FX</span>
fx.<span style="color: #006633;">stop</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;">//isRunning() method tells if the FX is running or not</span>
fx.<span style="color: #006633;">isRunning</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;">//isEnded() method tells if the FX animation is ended</span>
fx.<span style="color: #006633;">isEnded</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//and, if you want a looping effect, you can use</span>
fx.<span style="color: #006633;">setLooping</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: #339933;">;</span></pre></div></div>

<h3>Further information</h3>
<p>You can library JAR file here: <a title="ImageFx J2ME image effects library" href="http://www.jappit.com/uploads/j2me/ImageFx.jar">ImageFx.jar</a>. Current version requires MIDP 2.0 and CLDC 1.1 to be used (a version compatible with CLDC 1.0 will be released as soon as I&#8217;ve got time <img src='http://www.jappit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ).</p>
<p>Full API JavaDocs (still in early phase too <img src='http://www.jappit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ) are also available <a title="ImageFx API JavaDocs" href="http://www.jappit.com/j2me/imagefx/doc/" target="_blank">here</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/06/06/imagefx-j2me-image-effects-library/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

