Java Reference
In-Depth Information
In this section, we have done pure refactoring. We have not changed the functionality of the ap-
plication at all, but have worked exclusively at improving the implementation structure so that
future changes become easier.
Now, after finishing the refactoring, we should test that all existing functionality still works
as expected. In all development projects, we need phases like this. We do not always make
perfect design decisions at the start, and applications grow and requirements change. Even
though our main task in this chapter is to work with GUIs, we needed to step back and refac-
tor our code before proceeding. This work will pay off in the long run by making all further
changes easier.
Sometimes it is tempting to leave structures as they are, even though we recognize that they
are not good. Putting up with a bit of code duplication may be easier in the short term than do-
ing careful refactoring. One can get away with that for a short while, but for projects that are
intended to survive for a longer time, this is bound to create problems. As a general rule: Take
the time; keep your code clean!
Now that we have done this, we are ready to add some more filters.
Exercise 11.38 Add a grayscale filter to your project. The filter turns the image into a
black-and-white image in shades of gray. You can make a pixel any shade of gray by giving all
three color components (red, green, blue) the same value. The brightness of each pixel should
remain unchanged.
Exercise 11.39 Add a mirror filter that flips the image horizontally. The pixel at the top left
corner will move to the top right, and vice versa, producing the effect of viewing the image in
a mirror.
Exercise 11.40 Add an invert filter that inverts each color. “Inverting” a color means replac-
ing each color value x with 255 - x .
Exercise 11.41 Add a smooth filter that “smoothes” the image. A smooth filter replaces
every pixel value with the average of its neighboring pixels and itself (nine pixels in total). You
have to be careful at the image's edges, where some neighbors do not exist. You also have
to make sure to work with a temporary copy of the image while you process it, because
the result is not correct if you work on a single image. (Why is this?) You can easily obtain
a copy of the image by creating a new OFImage with the original as the parameter to its
Exercise 11.42 Add a solarize filter. Solarization is an effect one can create manu-
ally on photo negatives by re-exposing a developed negative. We can simulate this by
replacing each color component of each pixel that has a value v of less than 128 with
255 - v . The brighter components (value of 128 or more) we leave unchanged. (This is
a very simple solarization algorithm—you can find more-sophisticated ones described in
the literature.)
Search WWH ::

Custom Search