This small application is called lens effect, developed during one of the Easter break without plans.
At that time, I was finishing the first version of the application called File Encoder Application and I needed something beautiful or colourful for the About page ...
The original idea is not mine but I implemented it using an effect that I saw many years ago in one of the legendary DOS demonstrations.
The implementation chosen was to encapsulate the effect inside a JPanel of Java Swing.
Everything inside the panel can be zoomed with the lens, which can be placed in any coordinates inside the panel.
It was especially hard to achieve the refresh of the text components (particularly when the location of the selected text is modified by the programme) without badly affect the visual effect.
Internally, it works in such a way that when the LensJPanel is created, it receives 3 parameters i.e. the content from JPanel which the effect will be made on, the lens radius and the zoom in and out effect.
With this information, a square matrix of two dimensions will be created where each pixel inside the square surrounding the lens.
For each of those pixels (destination pixels), the coordinates of the original image (source pixel) are calculated. This source pixel is the location inside the original image to be set in the destination pixel in the image with the lens effect done.
The transformation is a simple transformation in polar coordinates. I will explain that transformation for the case of the zooming out effect:
For a particular position inside the square surrounding the lens, an origin coordinate is calculated, as explained previously.
This source pixel will also be inside the square surrounding the lens. Besides that, each destination pixel will be translated to polar coordinates (radius and angle, from the center of the lens). The source pixel associated with every destination pixel, will have the same angle but the radius will change. The radius will be always within the range of 0% to 100% for both the destination pixels and source pixels. 100% is the length of the radius of the lens. That is, for every destination pixel, to calculate the source pixel the angle is maintained but the radius is transformed using a function with increasing monotonous derivative, inside the interval between 0 and 1, where f(0) will be 0 and f(1) will be 1, where 0 means 0% of the radius of the lens and 1 is the 100% of the radius of the lens. That means that the radius of the source pixel for a particular destination pixel will have a radius lesser than the destination pixel itself, which will make the lens to zoom out.
Outside the circle of the lens, the transformation will be the identity (that is, the destination pixel and the source pixel will be the same) so that outside the lens no zooming is done.
The explanation is a bit confusing, but if you are interested to know more, please contact me.
I hope it will be useful for someone :-)