It’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’s start by our method declaration:
public static Image createReflectedImage(Image image, int bgColor, int reflectionHeight)
{
}
We have 3 arguments:
- the original image that we want to reflect
- the background color (used for transparent images)
- the height of the reflection effect
2. The mutable Image
Now, let’s create the mutable Image that will hold the resulting effect:
int w = image.getWidth();
int h = image.getHeight();
Image reflectedImage = Image.createImage(w, h + reflectionHeight);
We store the original image width and height into 2 int variables, and then create the mutable image with the same width, but with an height equal to h (the original image) plus the specified reflection height.
3. Copy the original Image
Now, first drawing steps are:
- Getting the Graphics object of our mutable image
- Filling the image with the background color
- Drawing the original image on the upper part of the mutable one
Graphics g = reflectedImage.getGraphics();
g.setColor(bgColor);
g.fillRect(0, 0, w, h + reflectionHeight);
g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
4. Create the reflection effect
Now, let’s get to the important part of this tutorial, that is the reflection effect itself:
- for each horizontal line of the reflected image part, take the corresponding vertical coordinate of the original image
- get the RGBA data of the corresponding horizontal line of the original image
- calculate the alpha to be applied to this line, and apply it to each element of the RGB data array
- draw the RGB data into the reflected image, by using its Graphics object
And here is the source code:
int[] rgba = new int[w];
int currentY = -1;
for(int i = 0; i < reflectionHeight; i++)
{
int y = (h - 1) - (i * h / reflectionHeight);
if(y != currentY)
image.getRGB(rgba, 0, w, 0, y, w, 1);
int alpha = 0xff - (i * 0xff / reflectionHeight);
for(int j = 0; j < w; j++)
{
int origAlpha = (rgba[j] >> 24);
int newAlpha = (alpha & origAlpha) * alpha / 0xff;
rgba[j] = (rgba[j] & 0x00ffffff);
rgba[j] = (rgba[j] | (newAlpha << 24));
}
g.drawRGB(rgba, 0, w, 0, h + i, w, 1, true);
}
as you can see, the rgba[] int array holds the current pixel row data, and will be refreshed only when necessary (so, when the y coordinate of the original image changes).
Sample usage
Using the above method is really simple, since it’s only necessary to:
- Create the original Image
- Call createReflectedImage() method by passing the original Image as argument, together with the background color and the reflection effect height
Image originalImage = Image.createImage("/cap_man1.png");
Image reflectedImage = ReflectedImage.create(originalImage, bgColor, 64);
Downloads
You can download the complete source code of this article here:
Vote this article!
If you liked this tutorial, feel free to vote it on Forum Nokia Wiki: How to create an image reflection effect in Java ME
Laura 12:32 pm on May 4, 2009 Permalink
Thank you thank you thank you!!!!
One other request–is there a way to make it able to see a month view at a time? Or can it do that now and I just don’t know how?
Thanks so much!!!
Dirk Kotze 9:41 am on May 6, 2009 Permalink
Thank you for the time-zone functionality.
morguena 6:41 pm on May 7, 2009 Permalink
in an n70 when it comes to loading the calendar closes the program automatically or gives the error java.io.IOException: -36
¿¿¿One of the questions is whether the program works offline or if there is to be connected?
Louis de Lange 11:06 pm on May 12, 2009 Permalink
Phone: Samsung SCH-R610. Version 0.3.3 used to work. Then it stopped working. When I try to enter my password the text window on the phone allows me to input my full password, but when I accept the password and the screen returns back to the gcal login screen it only shows the first 5 character of the password. Obviously google won’t allow me to log in without the full password. Upgraded to 0.3.5, but same problem.
s t 6:19 pm on May 15, 2009 Permalink
I use Willcom WX341K (PHS).
I installed and ran Gcal.jad
(http://www.jappit.com/blog/2009/04/m/gcal/Gcal.jad).
But I can not login google account.
I can not push “Login” button,
and “Exit” button.
I can push “Cancel” / “Ok” button….
PB 9:25 am on May 27, 2009 Permalink
Thank you for the time-zone functionality.
Nuno Gomes 2:39 pm on June 3, 2009 Permalink
Hi,
I’m not able to download the jad part using the provided link.
By the way: do you foresee to have an “edit event” functionality
Thanks
Regards
pit 2:48 pm on June 3, 2009 Permalink
Hi all,
@Laura: month and week views are planned, and should be included in a future release. Cannot give precise timeplan about it
@morguena: thanks for your feedback, I’ll check your issue and let you know what I find. GCal works online. Browing events offline would be a nice functionality, but it is not currently implemented.
@Louis de Lange: unfortunately this is a known issue, but I currently have not a fix for it. I’m actually working on it, and hope to have good news asap
The five ‘*’ are only for security purposes, to avoid displaying the exact length of the typed password.
@s t: unfortunately I have not that device to make tests, but I’ll check if I can find a fix for it.
@Nuno Gomes: which error do you get when downloading the JAD? Edit functionality is surely planned, hope to include it in a very next release!
Pit
Thomas Gehring 8:50 am on June 7, 2009 Permalink
Hi,
is there a way to display Google´s tasks? They don´t show up on my calenders.
Thanks
Tom
Grigory 6:39 pm on June 10, 2009 Permalink
Hi, I can’t create a new event in my Google Calendar. When I click Create, program write “Error while saving new event (java.lang.Exeption:CODE:403)”.
Reading calendar is Ok.
Help me please
What setting I can changed in Google that it work???
or is it bag????
Sorry for my English ))))
Grigory 7:04 pm on June 10, 2009 Permalink
Philips Xenium X800
Julie 9:18 am on July 18, 2009 Permalink
Hi Pit
I’ve just installed this version on my Nokia N97 (previously had an E90), and all day events are showing up in the following day’s events list, saying “To All Day” (which is a strange phrase in itself).
I’m pretty sure this didn’t used to happen on the previous version of GCal which was installed on my old phone.
Then I realised that Use server timezone was set to No with Timezone +01:00 by default (I think the default should be to use it, as it was in previous versions where you had no choice). Setting Use server timezone to Yes fixed the problem. Presumably with a manual offset of +1, gCal thought All Day finishes at 1:00am?
Julie
Marty 8:49 pm on October 27, 2009 Permalink
Love the GCal app but 0.3.5 doesn’t handle Eastern Time Zone “All-Day” events correctly.
I have an all day event for 10/30 and it shows on 10/29’s list of events. Doesn’t matter what settings I use it will not work correctly. I have it set to be -04:00 and the times show correctly on regular events but the AllDay event is wrong. My Google calendar is set for Eastern GMT-05:00 but if I change Gcal to that my times are off an hour.
menetekel 11:37 am on November 13, 2009 Permalink
Hi Pit, thanks for this GREAT app.
One question: when I add a new event to my calendar from the app, it does not create a reminder for the it on Google, even though this is set as default on the website. Any way to fix this, please.
Many thanks,
Heinz.
ffffff 1:31 pm on December 19, 2009 Permalink
When I try to log in, there’s an Exception (javax.microedition.pki.CertificateException). Then I can only click Ok, which returns me to the login screen
Elros 3:47 am on December 20, 2009 Permalink
The jad download link returns this error: Error 404 – Not Found
pit 10:18 am on December 21, 2009 Permalink
Hi all,
thank you for your precious feedback!
@Thomas: not yet, will check it out for including them in a future release
@Grigory: are you creating an event on your personal calendar, or on a shared one?
@Julie: not noticed yet, but I’ll try to reproduce your problem and see what could be the cause
@Marty: I’m working to fix this, will release an update as soon as it properly works
@menetekel: do you refer to mail/sms reminders? Actually, there’s no explicit support yet, but will be surely added in one of the next updates
Elros: thanks, should be fixed now
Matt 5:59 pm on January 14, 2010 Permalink
Is there a way to create repeating events in Gcal?