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
crack 6:26 pm on March 21, 2009 Permalink
New version still with login error.
Message is:
Error: Login exceptiom (400)
SK 10:06 pm on March 21, 2009 Permalink
Using this latest update v0.3.4, getting “Error: Login exception (400)” using Samsung Instinct with all latest updates.
troy o 11:52 pm on March 21, 2009 Permalink
version 0.3.3 actually worked on samsung instinct. installed v0.3.4 and get login exception 400 error. uninstalled and reinstalled previous version and now it doesnt work…java exception
Marcus 2:49 pm on March 22, 2009 Permalink
This is a really useful piece of software!
I have one problem on my N95, however, some of the appointments (but not all of them) are one hour earlier than they should be. This is sadly a major problem since it is not the same for all appointments
Is this a problem you are aware of?
Thank you so much for the software however!
Dirk Kotze 11:03 am on March 23, 2009 Permalink
Hi. Thank you for fixing the errors. Another small thing, the event times are only displayed in GMT time, which doesn’t make sense for myself (workin on GMT+2hr time). Otherwise, well done and thank you.
Cliff 7:48 pm on March 23, 2009 Permalink
Hey — looks like a great piece of software.
I’m getting an error message when I try and download the software from the .jad link through the Samsung Instinct M800 browser. It goes to the download screen, I hit “continue download,” and I receive the following text:
This issue has bee reported.
Please try again later.
907
Invalid content.
Anything you can do to fix that would be greatly appreciated!
Chris 9:59 pm on March 23, 2009 Permalink
FYI, still have the same Java root certificate expired problem on the Helio Ocean, already reported, thanks… Chris
Alberto 9:20 pm on March 30, 2009 Permalink
ciao! ma sei italiano?
cmq appena scaricato e installato sul mio touch hd (rom xor nextgen 1.0). funziona alla perfezione tranne che per il già citato problema dell’orario, se ho un appuntamento segnato alle 16.30, il tuo gcal mi indica 14.30. una volta che lo si sa ci si puo’ gestire di conseguenza, ma penso anche che se il bug sia una seccatura
in english: installed on touch hd (rom ita xor nextgen), work fine but the already know bug about gmt time.
pit 9:44 am on March 31, 2009 Permalink
Hi all,
thank you again for all your precious feedbacks!
@Chris: unfortunately, I have not a workaround for this issue right now. I should work on a proxy server, but it’s surely not a fast solution
@Cliff: thanks for reporting, I’ll check this and let you know
@Alberto: sì, sono Italiano
Pit
Alfie 12:36 pm on April 2, 2009 Permalink
This application is very useful
Is it possible to add a reminder when setting up an event. I can’t seem to figure this one out.
If not is it something that could be added, or even use the users default notification settings to automatically add a reminder to an event.
Thanks
Laura 10:37 pm on April 9, 2009 Permalink
Hi–This is a great app! The only problem for me (I’m using a Lg Vu CU920) is I need the correct time zone. All the times in my calendar are 4 hours later than they should be. I’m assuming they are in Pacific Time, and I need them in Eastern. Unfortunately, I can’t use it for my calendar until that gets fixed–but I’m SOOO looking forward to it!!
Thanks for all the effort!
ivan 1:50 pm on April 14, 2009 Permalink
hi, I’m sorry to say I cannot login, neither can try it. I mean, in the Account data page I can write my e-mail adress and also the password buy I can’t (or just I don’t know) how to login.
I see the login command in the down-left part of the screen but I can’t make it works.
I have a BlackBerry 8320.
thank you.
KL 3:54 pm on April 15, 2009 Permalink
GCal is great. And as an Samsung Instinct user, if you need any assistance I’d offer my assistance in the effort to resolve the login issue. Please email if this will help you and thank you for your time in building a great app!
david 3:50 am on April 21, 2009 Permalink
the login button disappears so cant login
Guillaume 2:02 pm on April 21, 2009 Permalink
I have 2 hours wrong between Google Calendar and what appears in GCal
Guillaume 11:09 am on April 22, 2009 Permalink
The only problem for me (I’m using a Lg KF700) is I need the correct time zone. All the times in my calendar are 2 hours earlier than they should be.
Adeel 9:41 pm on April 22, 2009 Permalink
can someone fix this app so it would work with samsung instinct, keeps getting 400 exception error…
Laura 2:41 pm on April 30, 2009 Permalink
Any idea when we can get the time zone issue fixed?? I’m really wanting this app, but can’t use it if the time zone can’t be switched to Eastern!!
Please please??
Thanks!
byron 10:40 pm on August 11, 2009 Permalink
Hi I have a Sony C510 and I am able to enter my email address but, when I enter p/w, it does not pick up all the characters and thus, I cannot log in.
blackadder1st 1:56 pm on September 14, 2009 Permalink
Same as Byron. I have a 9 figure password but when I ‘OK’ it only shows 5 *s. When I click on Login, it sits at Logging In at the top right corner for ever, and doesn’t actually log in. I am using a CECT i9 3G phone on O2 network if it makes any difference.
The lack of calendar on the i9 phone is one of its major letdowns, so if I could just get GCal working and sync my calendar, I’d have my ‘iPhone’.
Regards
pit 2:01 pm on September 14, 2009 Permalink
Hi,
@blackadder1st and @byron: only 5 ‘*’ characters are displayed as a security practice, but all the password characters are correctly used when logging in. So, the login issue is probably related to something else.
Since I have not these devices, I have to get them to test GCal on, and spot out which are the issues.
Will keep you updated
Pit
Mays 4:26 pm on November 9, 2009 Permalink
Gostaria de poder utilizar o GCAL no Brasil, mas para isso, deve ser possível mudar o fuso. Já estava acostumada a utilizar o GCAL, mas quando entrou em vigor o horário de verão, fiquei impossibilitada. Por favor, corrijam este detalhe!
Sogo 7:58 pm on January 12, 2011 Permalink
The latest version I save in my SE W800 as J2ME phone still got error while retrieving data. I could login to my Google Calendar but ” Error while retrieving events” appeared.