This was an effect I planned to do for a PSSST clone-type entry, inspired by some of the already available flash alternatives. The aim was to create a grass-filled strip of the screen. Now grass is simply a set of curved (quad) or straight lines:

However, the grass achieved using this method only is quite dull. It it is much more interesting if there is some blurring and if the grass has a color gradient. Compare, for example, these two strips of grass:
![]() Unblurred |
Blurred |
Blurring can be done in Java using a convolution filter - an operation that basically modifies each pixel of an image according to the values of a matrix called kernel. If the kernel of a filter is, say :
0 |
1 |
0 |
2 |
4 |
2 |
0 |
1 |
0 |
it means " the current pixel is replaced by its value multiplied by 4 + the north and south pixels neighbouring pixels multiplied by 1 + the east and west neighbouring pixels multiplied by 2".
If the sum of all cells of a filter is different from 1, there will be an overall change in the brightness of the image (the image will be brighter if the sum is > 1 or darker if the sum is < 1). Since usually one does not want this, most filters are normalized, meaning that the starting values of every cell are dividied by the sum of all
The following snippet applies a convolution filter to an image:
BufferedImage filteredImage = new BufferedImage( sourceImage.getWidth(), sourceImage.getHeight(),BufferedImage.TYPE_INT_ARGB);
float[] elements = {
.0f, .1f, .0f,
.3f, .1f, .3f,
.0f, .2f, .0f};
Kernel kernel = new Kernel(3, 3, elements);
ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
cop.filter(sourceImage,filteredImage);
where the second parameter of ConvolveOp specifies what happens to border pixels, and it can be either EDGE_NO_OP (meaning that they are copied directly) or EDGE_ZERO_FILL, meaning that they are set to 0.
There are many interesting filters that you can apply to an image:

Original Image
|
|
|
|||||||||||||||||||||||||||
![]() Emboss |
![]() Box Blur |
![]() Sharpen |
|
FilterDemo.java ( 2 Kb ) |
Grass.java ( 2 Kb ) |
GrassAnimation.java ( 3 Kb ) |