Friday, February 11, 2011

Friday Fun Component V

It's friday again...which means it's time for some fun...

Last wednesday at the Java Roundup of the Java User Group Muenster i decided to create a little fun component again.
Every morning on my way to the office i see one of the following traffic signs and what should i say...i like it somehow...



So i asked myself if it would be hard to create something like this in Swing. 
The first idea was to only create this single sign but after a few more days passing by the traffic sign the idea came to my mind that it would be fun to load some image, scale it to the number of led's in the sign and create the led's with the appropriate color of the image pixels.
But let me give you a more detailed description of what i've done...

These are the steps it takes to create the component:
  1. Create a panel of 250x250 px
  2. Fill the panel with led components of a fixed size (10x10 px) with a rasterStep of 6 px (this is the distance between each led in x and y direction).
  3. Now you could load an image into the component via the setSymbol(fileName) method.
  4. The image file will be loaded, scaled to the size of the raster (no. of led's in x and y direction).
  5. Get the color of each pixel in the image and set the related led in the raster to the same color.
  6. If the brightness of the pixel is smaller than 50% the color of the led will be set to black and it will be switched off.
  7. Draw all the led's in a buffered image and draw the image to the panel.

To create a traffic sign like the one on the image above i created a little png file that looks like this...


This image has the size of 41 x 41 px which is exactly the number of led's that i have in the default raster (ImageWidth / rasterStep => 250px / 6px = 41.666 => 41). I do not use antialiasing in the image because every pixel in the image should be represented by one led in the raster later on. You could vary the rasterStep variable (distance between the led's) in a range of 5...10 px.

And this is the result after i load the image into the component...


Looks not too bad...

You can imagine that it takes some time to create all the led objects and set them to the right color (set the color will create a new led image with the selected color). So this component is not very "fast" but that was not the intention. 
It's really just a quick hack to get the idea out of my head and has nothing to do with production code etc...
The component is ideal to visualize monochrome icons or symbols that are not too big (because they will be downsized to the raster size).

Here are some more images i created...





I have no idea if this is of use for someone but if you would like to have the code...no problem...here we go, as always as a netbeans project:

    Source: FridayFunV_Source.zip

    Binary:  FridayFunV.zip

So enjoy the upcoming weekend and keep coding...


UPDATE:
Modified the file a bit in the way that it caches the led's which leads to a bit better performance...

No comments:

Post a Comment