Posted by: tuanbach on: August 10, 2009
As promised here is my former application fireworks which I migrated to Android OS.
I changed a little bit the application in order to handle touch event, so it’s not a fireworks anymore, it’s a kind of wasted-time ZEN application
The app is very simple, when you touched the screen it draws you a square in magenta and the screen is continuously blurred… the result aim to be a ZEN application !
Of course, it’s more for education purpose…
Here is a screen of our final application:

Check it !
So we have these 4 steps below in order to get the same result:
Please note that I assume you already have the SDK installed (1.1 or higher) and you know how to create a new project.
1 – Create a project and a activity
We created an activity and an anonymous class derived from View (Graphique which means graphic in french :p ) class. We have to do so because we want to override the onDraw function of the view then we can draw on the current View.
The anonymous class is an ugly solution. Because the application structure is very simple we can avoid this rule
But yes try to never use anonymous class as all good books told you!
We also defined a Bitmap which represents the back screen. We will firstly draw on the back screen before copy it to main screen (real phone screen). It’s a kind of double buffer. Also don’t forget to define a Paint which tell to Android how you want to draw the pixels.
<pre>public class Fireworks extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new Graphique(this));
}
private static class Graphique extends View {
private Paint mPaints, mFramePaint ;
public Graphique(Context context) {
super(context);
mFramePaint = new Paint();
mFramePaint.setAntiAlias(true);
mFramePaint.setStyle(Paint.Style.STROKE);
mFramePaint.setStrokeWidth(0);
mPaints = new Paint();
mPaints.setAntiAlias(true);
mPaints.setStyle(Paint.Style.FILL);
mPaints.setColor(Color.BLUE);
this.bufferscreen = Bitmap.createBitmap(
315,425,Bitmap.Config.ARGB_8888);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(this.bufferscreen, 0.0f,0.0f,mFramePaint);
invalidate();
}
}
}
2 – Implement onDraw function to compute and draw result
Here is the core of our application. Please refer to my original post to know how the algorithm is working.
You have to focus here on the mini_buffer and optimization code. Because it’s very slow to compute the screen entirely, I created a mini screen with fewer pixels where we draw and compute them. When all computing are finished I project the mini screen to the buffer screen with the real size. And after that I copy the buffer screen to the real phone’s screen. It’s seems very complex but in fact it’s very simple, please stay with me
For instance my real screen sizes are 100/100 and my mini screen are 10/10 (width/height). So we did all maths in the 10/10 screen then project it to the 100/100. Which mean:
- I have originally 100*100 pixels to compute, finally I only compute 10*10 (one line in the real screen !)
-1 pixel in my mini screen represents a square of 10/10 pixels in my real screen.
-my real screen will pixelize ! Yes it’s the fact, but the gain I earned by computing one single line instead of 100 lines is much relevant! Any way you can change the projection in order to match exactly what you need.
Finally what is this ?
r = ( ( ((p1 >> 16) & 0xff)+ ((p2 >> 16) & 0xff)+ ((p3 >> 16) & 0xff)+ ((p4 >> 16) & 0xff) )>>2 )<<16;
Because of the algorithm (please refer to my previous post) I have to extract each component (RGB) and compute separately these with pixel’s components around. Then the pixel’s components are stored as below:
Pixel = Red (8 bytes) | Green (8bytes) | Blue (8bytes)
So to get the component I wanted I have to shift to the right and do an AND to 0xff. The steps below will explain you why:
Pixel >> 16 <==> Pixel = 8 Unknown bytes | 8 Unknown bytes | Red (8 bytes)
0xff <==> 00000000 | 00000000 | 11111111
[ 8 Unknown bytes | 8 Unknown bytes | Red (8 bytes) ] AND 00000000 | 00000000 | 11111111 <==> 00000000 | 00000000 | Red (8 bytes)
Now I have the red component of my pixel, I can do it with all components. note that for the Blue component you don’t have to shift any bytes, because it ’s already at the beginning.
Then I add all red’s components, divide them by 4 by shifting to the right by 2. Yes it works, when you shift n to the right you divide your number by 2*n. The proof can be found on Google, or you can practice some exercises by trying to get the proof yourself. It’s due to the binary, with the same reason when you shift by 1 in the decimal system (human system) to the right you divide by 10 (e.g 100>>1 <==> 10)
And I shift back the component to the right position (16 for red, 8 for green, 0 for blue).
When I finished to compute I regroup all my components by doing a simple OR:
respix = 0xff000000 | r | g | b;
Note that I added the alpha component on the left of my variable because Android system stored the pixel as ARGB.
Finally I do the projection by writing my pixel square in the buffer screen. In my code I divided the real screen by 5, so I have to draw a square of 5/5 in my buffer for each pixel from my mini buffer.
int index,r,g,b,index2;
int[] buffer = this.mini_buffer;
int p1,p2,p3,p4;
int respix;
for(int y = 85 -2;y>0; y--)
{
for(int x = 63-2;x>0; x--)
{
index = y*63+ x;
p1 = buffer[index+63];
p2 = buffer[index-63];
p3 = buffer[index+1];
p4 = buffer[index-1];
r = (
(
((p1 >> 16) & 0xff)+
((p2 >> 16) & 0xff)+
((p3 >> 16) & 0xff)+
((p4 >> 16) & 0xff)
)>>2
)<<16;
g = (
(
((p1 >>
& 0xff)+
((p2 >>
& 0xff)+
((p3 >>
& 0xff)+
((p4 >>
& 0xff)
)>>2
)<<8;
b = (
(
((p1) & 0xff)+
((p2) & 0xff)+
((p3) & 0xff)+
((p4) & 0xff)
)>>2
);
//compute pixel color
respix = 0xff000000 | r | g | b;
index2 = (y*5)*315+ (x*5);
//write color pixels to front screen
this.buffer_class[index2]= respix;
this.buffer_class[index2+1]= respix;
this.buffer_class[index2+2]= respix;
this.buffer_class[index2+3]= respix;
this.buffer_class[index2+4]= respix;
this.buffer_class[index2+315]= respix;
this.buffer_class[index2+315+1]= respix;
this.buffer_class[index2+315+2]= respix;
this.buffer_class[index2+315+3]= respix;
this.buffer_class[index2+315+4]= respix;
this.buffer_class[index2+630]= respix;
this.buffer_class[index2+630+1]= respix;
this.buffer_class[index2+630+2]= respix;
this.buffer_class[index2+630+3]= respix;
this.buffer_class[index2+630+4]= respix;
this.buffer_class[index2+945]= respix;
this.buffer_class[index2+945+1]= respix;
this.buffer_class[index2+945+2]= respix;
this.buffer_class[index2+945+3]= respix;
this.buffer_class[index2+945+4]= respix;
this.buffer_class[index2+1260]= respix;
this.buffer_class[index2+1260+1]= respix;
this.buffer_class[index2+1260+2]= respix;
this.buffer_class[index2+1260+3]= respix;
this.buffer_class[index2+1260+4]= respix;
//write color pixels to back screen
buffer[index] = respix;
}
}
//
this.bufferscreen.setPixels(this.buffer_class,0,315,0,0,315,425);
canvas.drawBitmap(this.bufferscreen, 0.0f,0.0f,mFramePaint);
invalidate();
}
}
3-Handle finger touch
Final point I implement the right interface in order to handle the touch events and trackball events. Because it’s very simple, it avoids explanation…
You have to focus here on the Finger class, which represents the finger square to draw when user touches the screen. Note that I have to blit pixel in the mini buffer!
<pre>public class Fireworks extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new Graphique(this));
}
private static class Graphique extends View {
private Paint mPaints, mFramePaint;
private RectF mRect;
protected Bitmap bufferscreen;
int buffer_class[],mini_buffer[];
Finger finger;
public Graphique(Context context) {
super(context);
finger = new Finger();
mFramePaint = new Paint();
mFramePaint.setAntiAlias(true);
mFramePaint.setStyle(Paint.Style.STROKE);
mFramePaint.setStrokeWidth(0);
mPaints = new Paint();
mPaints.setAntiAlias(true);
mPaints.setStyle(Paint.Style.FILL);
mPaints.setColor(Color.BLUE);
mRect = new RectF(5, 5, 315, 425);
this.bufferscreen = Bitmap.createBitmap(
315,425,Bitmap.Config.ARGB_8888);
this.buffer_class = new int[315*425];
this.mini_buffer = new int[63*85];
}
public boolean onTouchEvent(MotionEvent ev){
if(ev.getAction() == MotionEvent.ACTION_UP)
this.finger.bprint = false;
else
this.finger.bprint = true;
this.finger.parametre_x((ev.getX()*63)/315);
this.finger.parametre_y((ev.getY()*105)/425);
return true;
}
public boolean onTrackballEvent (MotionEvent event){
this.finger.parametre_x(((this.finger.donne_x()+(Math.random()*100))*63)/315);
this.finger.parametre_y(((this.finger.donne_y()+(Math.random()*100))*105)/425);
return true;
}
@Override
protected void onDraw(Canvas canvas) {
int index,r,g,b,index2;
int[] buffer = this.mini_buffer;
int p1,p2,p3,p4;
int respix;
finger.dessine(this.mini_buffer,63,85);
for(int y = 85 -2;y>0; y--)
{
for(int x = 63-2;x>0; x--)
{
index = y*63+ x;
p1 = buffer[index+63];
p2 = buffer[index-63];
p3 = buffer[index+1];
p4 = buffer[index-1];
r = (
(
((p1 >> 16) & 0xff)+
((p2 >> 16) & 0xff)+
((p3 >> 16) & 0xff)+
((p4 >> 16) & 0xff)
)>>2
)<<16;
g = (
(
((p1 >>
& 0xff)+
((p2 >>
& 0xff)+
((p3 >>
& 0xff)+
((p4 >>
& 0xff)
)>>2
)<<8;
b = (
(
((p1) & 0xff)+
((p2) & 0xff)+
((p3) & 0xff)+
((p4) & 0xff)
)>>2
);
//compute pixel color
respix = 0xff000000 | r | g | b;
index2 = (y*5)*315+ (x*5);
//write color pixels to front screen
this.buffer_class[index2]= respix;
this.buffer_class[index2+1]= respix;
this.buffer_class[index2+2]= respix;
this.buffer_class[index2+3]= respix;
this.buffer_class[index2+4]= respix;
this.buffer_class[index2+315]= respix;
this.buffer_class[index2+315+1]= respix;
this.buffer_class[index2+315+2]= respix;
this.buffer_class[index2+315+3]= respix;
this.buffer_class[index2+315+4]= respix;
this.buffer_class[index2+630]= respix;
this.buffer_class[index2+630+1]= respix;
this.buffer_class[index2+630+2]= respix;
this.buffer_class[index2+630+3]= respix;
this.buffer_class[index2+630+4]= respix;
this.buffer_class[index2+945]= respix;
this.buffer_class[index2+945+1]= respix;
this.buffer_class[index2+945+2]= respix;
this.buffer_class[index2+945+3]= respix;
this.buffer_class[index2+945+4]= respix;
this.buffer_class[index2+1260]= respix;
this.buffer_class[index2+1260+1]= respix;
this.buffer_class[index2+1260+2]= respix;
this.buffer_class[index2+1260+3]= respix;
this.buffer_class[index2+1260+4]= respix;
//write color pixels to back screen
buffer[index] = respix;
}
}
this.bufferscreen.setPixels(this.buffer_class,0,315,0,0,315,425);
canvas.drawBitmap(this.bufferscreen, 0.0f,0.0f,mFramePaint);
invalidate();
}
}
}
<pre>
Here is the finger code
public class Finger extends Point {
public boolean bprint=true;
Finger(){
super();
this.pw = 10;
this.ph = 10;
this.couleur = Color.MAGENTA;
}
protected void dessine(int buffer [], int w, int h) {
if(!this.bprint)
return;
if(this.donne_x() > 0 && this.donne_x()+pw < w && this.donne_y()> 0 && this.donne_y()+ph < h){
for(int x = 0;x
<this.pw;x++)
for(int y = 0;y<this.ph;y++)
buffer[(this.donne_x()+x)+(this.donne_y()+y)*w] = this.donne_couleur();
}
}
}
That’s it !!

Yes it's a smiley
Posted by: tuanbach on: July 30, 2009
For few months now, I’m doing some stuff on Android. Ok it looks a little strange that I started to build things for Android but I have an Iphone…
But as you already know I can only develop application for the Iphone on Mac OS, but I don’t have a Mac (I know it’s a shame).
So what did I do?
I want to give you some feedbacks on all my tests, a kind of overview. Then I will write a post for each of my test separately.
First of all, yes Android is very easy to get start with. I like the Activity and View structure, I can’t give more details because I did not develop things really cool yet with UI, Map, contacts… Let’s see another time.
Regarding the performance, I have to admit that performance is very poor !! Especially on the emulator (the HTC magic is much faster than the emulator). For instance, I migrated my fireworks app to Android, and I have to reduce by 4 the size of the screen in order to get a correct frames rate. You can guess how bad is the visual rendering (you know like 320×240 dos game
). But I did not use OpenGL ES, it was a pure CPU computing.
But even when I tried to develop things with OpenGL ES, there is a very strange issue, I divided by 2 my frame rates when I blit a background! Actually the frame rate depend on how big is the surface to draw, for example you can draw 100 small bitmaps without performance issue if the total surface is not big… take a look on my figure to understand what I mean. So I guess it’s not the implemetnation of OpenGL ES which was bad but the device! But which device is it? It may be the screen/graphic component.

Blit OpenGL ES Android
Finally I wasted the rest of my time to try to build a physic’s game for Android. Then I tried Box2D engine. I got 5/10 FPS with a world containing 5 bodies !
So where did it come from?
Android is very bad in:
Ok we can understand the slow concerning the float computing. But regarding the garbage collector Google guys have to do something! And the main problem is that you can not have any influence on how the garbage works !! It tries to be smart and free everything for you but my god the GC is not smart at all !! The best defense on this issue is to code everything by yourself and be very careful on how you manage your memory. But as you know and Google guys also, we are now sharing codes, projects, then We can not review all codes!! For example I tried to review the Box2D codes to reduce the lack of memory, then it worked but It took me a week, moreover it’s not perfect and very hard to debug! It could be great if we can manage variables status (like Java standard) and set up the policy depending to our needs… (like Java standard)
By the way on this point the only good news is the DMMs tool, which help you to track and debug. It’s very useful, but again it’s not working perfectly, sometimes the tool did not identify correctly my activity.
Last thing was the NDK. Firstly the NDK is very hard to install. There is no good documentation (full documentation), for my part i don’t like this help page of Google. Yes developing with the NDK is much faster than Java, regarding some works done on Box2D (no comparison possible with Java Box2D!!). But you have to remain that will not be compatible with all devices (native code). Then you have to compile for each different CPU a different application, how can you deal with it in the market place?..Mmm moreover the promise of Google’s Android was not “a full compatibility on all devices” ? For my part it’s one of the argument that convinced me to develop for Android… now that if I have to develop an application for each device… what a difference for me comparing to the past?
I think Google realeased the NDK because there are not so much games on the market and the main reason for that is the fact that Android is to slow!
So to summarize, Google guys, you have to speed Android up for the next version please because NDK is not a good solution at all !!!
(to be continued with how to plot pixel in Android… )
Posted by: tuanbach on: July 10, 2009
A little word for the big guy…
I’m not a big fan of Michael Jackson. But when he played with the Jackson5, or the song Thriller, Bad, he is definitely the only one we can named “King of the pop”. I loved the Motown sound and the Jackson5 is one of the most famous artists from this time.
Then even if that’s true he did not make a new album for a long time now and we don’t know if it’s gonna happen again (yes there will have some albums which will run out of his Magic Hat falling from the sky… rumors said around 150 songs !) I think he deserved the Title of King of the Pop, he deserve it because he was so revolutionary in many aspect of the contemporary music. He “happened” at the right time with the right rhythms and now that we are in the big revolution of all the music industry I’m pretty sure that he was the only One who can be named as well. I’m also sure there will be others great artists but nothing compares to his impact, even if now we will more talk about “billion of downloads” and not “millions of albums”.
So I went to the “Giant Moonwalk” organized by some fans at the bottom of the Eiffel Tower last week-end, just to “be there” and take some interesting shots !
Keep Dancing Michael!
In my despair, I tried to find an enchantress to make some magic stuff, then he may revive Michael… but nothing worked but some fire appears on his finger… (no photoshop, live shot)
![finger fired [640x480] finger fired [640x480]](http://tuanbach.files.wordpress.com/2009/07/finger-fired-640x480.jpg?w=480&h=321)
![Magic guy [640x480] Magic guy [640x480]](http://tuanbach.files.wordpress.com/2009/07/magic-guy-640x480.jpg?w=480&h=321)
I’ll try to make him fly next time, maybe he can meet him somewhere over the rainbow…
Posted by: tuanbach on: July 7, 2009
A humble topic about the marriage of my friend Benjamin Fellous (a great android developer by the way) which I have had the honor to be invited!
As you can see there are lots of marriage during this time, I went to my cousin’s one a month ago, to the marriage of a friend of my girl two weeks ago and yesterday the marriage of my friend Benj. As the title said you can guess that was a Jewish marriage!
Then I have to admit that it was really fun and interesting for a Vietnamese guy like me. Firstly it was really fun because of the music, Jewish music and more generally North Africa’s music are really folklore. The sound was so captivating and the woman’s dance is so unctuous…
It’s also the first time that I see the “chair throw”, it means that the new husband and wife seat in a chair that everybody bring up and down. The cook was good too, I ate for the first time the Pkaila! It’s not really light…
but very good!
Everything was cacherouth (I don’t know the word in English, if someone can help, he’s welcome). Even the wine, which was a “Bordeau supérieur 2004″, also cacherouth !
I was very touched to see a friend got married…and scared, I mean who’s the next ??
I’m sure you will have a nice and wonderful marriage in Israel too!!
Enough, below some pictures I shot with my Iphone (sorry for the low quality), I think you can find some great shot on the Benj’s blog!
Again, MAZEL TOV Benjamin and Jessica!

I'm a king and you are my star ! :p


Hang on !!

Slaaam!

Meet on the camel seat!

Like a Sultan !!

Stand by me my princess.
Posted by: tuanbach on: June 18, 2009
I went a month ago to Berlin for my cousin marriage. But I went in Berlin few days before the ceremony with my friend Dewi. It was our second trip to Berlin, but the first time we were very young, both of us. We already loved this city but this time was very special.
The main feeling about this city was the fact that people are really cool, and they love their city, they’re really living in their. I mean they take their place in the town, I feel like I was in a urban jungle…
There are so many secret place, bars, gardens, and green’s places. In the garden you can find some hammock for your daily break, you can even make a fire in the garden in the evening. If you take time to walk around the city, I mean by feet, you can find some garden where people spontaneously come with friends, have a drink eat some grilled Wurst, have a cookout. For sure next time, I’ll come back with some wines and candles to share…
I definitely want to come back with more friends, and spend some fun time…
By the way you have to go to the Raw Tempel, a really nice place. It was an old train station, and now it became an association place where you can see movies in outdoor, have fun with your skateboard if you know how (not my case), do some climbing, and the night have a good concert, Electro party… beers are very cheap (don’t forget to bring back the bottle, you will have a discount on your next!! Yes… it’s so green even the beer !
)
I forgot my Nikon, then I don’t have lots of pictures..too bad, but Dewi shot some times, I’ll post here his best ones next time I have the opportunity.
Hopefully my father brought me the Nikon for the week-end.
Anyway, my main purpose is the marriage of my dear cousin. We were very close because we lived together when we were young in Vietnam, at Hanoi with our grand-mother. She was my protective sister against the bad guys and girls…
so nice… ok few times she had fun with me, just like children did… I’l never forget this two Van anh !
She’s very nice woman (just like Kyeu [my second cousin]
), espescially this day with her dress… Oh my god Sacha you are so lucky guy !
Here are some pictures from this day, I wish they’ll have a beautiful and happy life together, and have a lots of children…

Like a princess waiting for her charming prince (so romantic :p )

Sacha and Van ready for walking through life... (she's so nice !!)

As Van's friend said, Sascha means protective (you I did this shot !!
)

Our new family (the guy on the left is my step cousin, Steeve !)

Me and Van, for the Fame !

One more... fading out like a good dream....
Here is a last shot, I tried do some stuff in the dark. When I took the picture, the sky was quite black…

Peace line

Did you ever be an ant? Look how world is cool for an ant !
Posted by: tuanbach on: March 6, 2009
Then theses two weeks at Bucuresti are now done. I came here to meet my IT Team and also to be trained in our legacy production system.
Oh my god, technically speaking if you knew… I mean you never believe me
Understand what ever you want… Anyway It’s at least interesting let’s see how long I can manage with this… :p
If you have an American’s Iphone 3G which uses a sim hacking method (same as mine), you may be advised that it will not working here at Bucharest. I simply can’t connect to network. But hopefully, and I don’t really know why if you buy a sim card here, it’s working quite well. So Sim hack + sim from France == not working, you may change to sim hack + sim from Romania == works fine !??!!?!
Ok enough of geek topics, let’s talk about the city and people here. I were very lucky here at Bucharest because I only met nice people ! :p Really ! I mean in every places it should have some ugly guys but fortunately I didn’t met even one of them at Bucharest. My colleagues are all so nice, fun and also interesting guys. I learned a lot about my job. After the work we usedto go playing pool at the end of this week, and kidding about theses bard-maid… I don’t want to open long description about Romania’s girls because my girl will be jealous but I have to admit that it should begin by “Oh my God…” lol. Come to see yourself gentlemen !
People in Bucharest seems to not like very much their City, they use to say : “Bucharest is dirty, expensive, stressful…” All of theses things may be right but as a “Work Visitor” It was a real pleasure. During my first evening I met a french guy, Remi, who’s working in a booking call center here. Very nice guy who becomes a kind of travel guide… thanks Remi !!
He knows a mountain of things about Romania/n and Bucharest. I can’t find a better way to discover this town, I mean “Really thanks Remi !!!”
He introduced me to a friend of him, Lily who is a French teacher ! She really talks French fluently, with no accent.. really amazing… I met her with Remi at an Irish music’s pub. It was really a nice and fun night, Dancers were good and drinks too !
Players/ Singers were Romanian geeks, just entering in a kind of Irishmania !

The girl really enjoys here dance!
The best was when they danced all together !
They made big noises with their shoes and you made big noises with you glass !

Dance together !
There was this evening I just walked around the city, alone and of course I lost my way… After few hours I decided to make a break and I stopped to the first pub I found on my road. Fortunately it was one of the best hot wine bar in Bucharest !
I told you… I were very lucky !
After enjoying my hot drink I decided to find some help from people around this place then I met two nice students, Vlad and Bogdan ! After trying hard to explain how to go back to my hotel, they finally proposed to come with me. During the road we discussed together and I discovered that they are actors students !! They confessed to me that they’re just acting in some advertisements, and finally during our last met it seems they will play for a Romanian TV Show !! loool The TV Show seems to be famous in Romania, it titled a kind of ”Love Brokers”… they told me it’s a bullshit tv show… :p but it’s ok it could be fun for them!
I definitely decided to come back for vacation in this country, to see Brasov (I missed the train at my week-end here… :p ), to see the Delta of Danube, to swim in the Black Sea so many things and placed to see… Anyway Vlad’s wife invited me for a week !
I took it for words !
If you come around here you have to eat some local dishes and meals… Eat outside is cheap and it’s really good ! You may go to the student campus near from the river and eat some traditional meals, try their Enormous Pizza, and play pool with some friends
Below are some shots I made during my trip:

I'm sure the sky is missing you !

You should go in this place in the Bucuresti's center, Coyote's place !
There are so many pubs which are really nice and relaxing place in the small streets of Bucharest, you should take time to walk around.

One the most big building in the world. The Romanian Parliament was built by the dictator Nicolae Ceausescu. You should know that Romanian hate this building !
This building could be seen from the moon !!
Cheers !
Posted by: tuanbach on: February 26, 2009
We know Apple’s TV was not a big success, but it seems that’s Apple will make another shot.
And yes I think it could be a good idea because lots of TV constructors began to add gadgets and many features now in their products. Then one day, they will need a bigger OS, to do bigger stuffs, and then this day they will get an apple in their eyes !
For my part, if design is good enough and of course price is low engough maybe…I will try another shot too with this screen TV.

I have to admit that Iphone I used for few months now gave me some nice time, but also some ugly moment… more information about my last issue in the next post.
Posted by: tuanbach on: January 26, 2009
Ok as I promised for while now, there is a post about simple effect you can play with in java…
Please don’t laugh of my code because lot’s of what I done in java here came from my old “souvenirs” from what I learned when I coded it in C/Dos long time ago…I was very interested in Demoscene and all big effects you can do with sin/cos/+/-/*/:
I’m sure you can optimize this code for Java but I will let you enjoy this part !
Please stop grimacing at me like that, the code works well, easy to read and fast enough…
Here is the plan:
The smooth effect / fading effect
The fading algorithm is one of my favorites effects, because it’s easy and also nice !
Imagine we have a screen as an array, let’s name it back[width][height]. So as this array represents our screen, you should guess the screen contains pixel/color value. The pixel/color value is stored in an integer. So the array is an array of integer. I mention here the term integer as common sense, because as you should know in computer science we could represent an integer in several forms… We will come back to this remark later.
the fading effect consist simply in processing each pixel as below:
for each pixel ([x,y]) in back
{
if (x =0 || x=width||y=0||y=height)
{
back[x,y] = 0;
}
else
{
back[x,y+1] = average_of(pixels around [x,y]);
}
}
That’s it !
There are three main ways to find “pixels around [x,y]“:

The three main algorithm of fading effet
As you can note, in the third way, the pixel value is less than the others, which means the fading effect is faster than the others.
Thing to do to get more fun ! :p
Play to find another way to understand “around”. For example you could only computing pixels on the top of the current pixel, or you can enlarge the square around the current pixel, or even more complicated algorithm..
Here is the exact algorithm I used in my code:

Algorithm used in my code
As you can see I used the first fashion to find pixels around but I stored the new value to the pixel just at the bottom of the current pixel. So It gives to the fading effect a direction ! The fading seems to fall down… nice !!
Thing to do to have more fun ! :p
Again play with this to find another fun directions to give to the fading… for example we could do a spiral so the fading seems to be breathed in the center, you can fading up (set value to the top of the current pixel) and simulate things like fire… etc…
Come back to our pseudo-code, it gives us:
for each pixel ([x,y]) in back
{
if (x =0 || x=width||y=0||y=height)
{
back[x,y] = 0;
}
else
{
if(y == 1)
{
back[x][y] = 0;
}
back[x,y+1] = average_of(pixels around [x,y]);
}
}
What is theses tips around (x =0 || x=width||y=0||y=height) and (y == 1) if statement ?
The easiest way to understand still remains to leave this code and re-build to see what changed…You should see some lost pixels in the border line which make things ugly on screen… I let you wonder why et enjoy it !
I didn’t take time to optimize this part, but I think it’s one of the easiest part to change.
Going in deeper
Firstly take a look on the main code of the effect:
for(int x = 0; x < back.length; x++){
for(int y = 0; y < back[0].length;y++){
if(x == 0 || x ==back.length-1 || y == 0
|| y ==back[0].length-1 ) {
back[x][y][0] = 0;
back[x][y][1] = 0;
back[x][y][2] = 0;
}
else
{
if (y== 1){
back[x][y][0] = 0;
back[x][y][1] = 0;
back[x][y][2] = 0;
}
for(int c = 0;c<3;c++){
back[x][y+1][c] = (back[x+1][y][c]+
back[x-1][y][c]+
back[x][y+1][c]+
back[x][y-1][c])>>2;/* I put code for the third algo in comments
back[x-1][y-1][c]+
back[x+1][y+1][c]+
back[x+1][y-1][c]+
back[x-1][y+1][c])>>4;*/
}
}
front.setRGB(x,y,new Color(
back[x][y][0],
back[x][y][1],
back[x][y][2]).getRGB());
}
}
gr.drawImage(front, 0, 0, null);
Thing you should notice, if you don’t know how pixel are managed by your computer is why am I doing this :
for(int c = 0;c<3;c++){
back[x][y+1][c] = (back[x+1][y][c]+
back[x-1][y][c]+
back[x][y+1][c]+
back[x][y-1][c])>>2;/* I put code for the third algo in comments
back[x-1][y-1][c]+
back[x+1][y+1][c]+
back[x+1][y-1][c]+
back[x-1][y+1][c])>>4;*/
}
As you can see, I computed each component of the pixel (Red,Green and Blue) instead of computing directly in integer, for intance something like this:
back[x][y+1]= (back[x+1][y]+ back[x-1][y]+ back[x][y+1]+back[x][y-1])/4;
There are two reasons for this, firstly because it doesn’t give you the same result. See the example below:
Difference between integer and component computing
As you can see the rest of division prevent us from computing the right result.
The second reason took its roots from how graphic cards manufacturers managed pixels for years ago. The problem is that they used their own rules, and there are no standard on how stored pixel in memory.
For example for 16 bits depth color, how could we divided properly 16 bits for three components R/G/B ?? Some manufacturers decided to avoid the 16th bit, and stored as {R =5bits ; G = 5bits ; B=5bits} = 15bits. And some others decided to give to green componenent more bits (because human eye is more sensible to green), then we got {R=5bits ; G=6bits;B=5bits}.
Basically RGB integer is stored as below by the main part of manufacturers:
15bit = {5,5,5}
16bit = {5,6,5}
24bit={8,8,8} (8bit == 1byte, simple operation to get RGB component
)
32bit={8,8,8,8 } (the 4th byte is the component Alpha of the pixel, which refers to the transparency of the pixel)
And that’s not all ! Some manufacturers stored component in different order than RGB, for instance BGR ! So how could we manage this? We had to check which configuration is used on the current card, there is no other solution.
Fortunately the class Color in Java, help you to get the right structure for your integer, take a look on the source of the Color class, and you will see bit operations I’m talking about here.
For the best geek of you
You can play to recreate your own Color class and try to optimize it ! To success you have to get some tutorials on bit operations like shifting and bit masking.
Ok enough words now, let’s see the result of all our efforts:

It's better in motion !
Get sources code here! It’s really better in motion !
Now is your turn ! Dance in Java but don’t forget to go out with your girl at Valentine’s day !
Posted by: tuanbach on: January 19, 2009
Did you heard about PhotoSynth ?
First time I heard about it, was 3-4 years ago now… And I have to admit that I were very excited about the idea that you can replace the photo in 3D !!
I tried it few times ago now, and it’s really cool.
You should try it with your own shots if you have some cool time to spend…
Posted by: tuanbach on: September 2, 2008
How to not talk about this buzz today ?
It’s a buzz because it’s about Google, and furthermore the announce was done with a comics book !! You can find it here !!
And you can find this offline version (pdf) here, thanks to my friend BenJ !
Yes, Google’s launching his own Web browser: the Chrome.
So Why ?
Some guys think that Google was nervous about the anonymous mode of Microsoft’s browser, which is a good argument. Of course, this functionality hide your personal data to the others websites, and so prevent Google to show you their ads, or at least their ads no longer fit to user profile.
Others just think that Google’s guys are just boring. Hum…
And finally some others think that is one more bullet which aim to hit Microsoft’s heart, because the border-line between the Browser and the OS is becoming very thin… very very thin, and Google announced it in the first page:
Anyway, let’s be a Google’s fan (for a moment…a minute…maybe just a second…a nano-second?? ) , google is good, and google did it because they love us !!
So How ?
There are the features, highlights list that I did for all of you who don’t like comics:
It seems logic, because tab now appears as window…

(from techcrunch)

first screen, from crunchbase.com
So the naive moment past, and the Why?? is coming back from the bottom of my brain… and my answer is simple as :
Take a moment, and think why do we use every day this OS, Window’s System. Because it was cheap ! And so It’s everywhere !!
Even Google is good, is it normal to use, to store, to trust every actions, every data, everything to ONE UNIQUE company !
So if you followed me well, If you like Chrome, stop using Gmail !