660 CHAPTER 12.
ADVANCED GUI PROGRAMMING
component, there is no automatic validation of its container. A custom component such as
MirrorText must call the revalidate() method to indicate that the container that contains
the component should be validated. In the MirrorText class, revalidate() is called in the
setText() method.
12.5 Finishing Touches
In this final section, I will present a program that is more complex and more polished
than those we have looked at previously. Most of the examples in this book have been “toy”
programs that illustrated one or two points about programming techniques. It’s time to put it
all together into a full-scale program that uses many of the techniques that we have covered,
and a few more besides. After discussing the program and its basic design, I’ll use it as an
excuse to talk briefly about some of the features of Java that didn’t fit into the rest of this
book.
The program that we will look at is a Mandelbrot Viewer that lets the user explore the
famous Mandelbrot set. I will begin by explaining what that means. If you have downloaded
the web version of this book, note that the jar file MandelbrotViewer.jar is an executable jar
file that you can use to run the program as a stand-alone application. The jar file is in the
directory c12, which contains all the files for this chapter. The on-line version of this page has
two applet versions of the program. One shows the program running on the web page. The
other applet appears on the web page as a button; clicking the button opens the program in a
separate window.
12.5.1 The Mandelbrot Set
The Mandelbrot set is a set of points in the xy-plane that is defined by a computational
procedure. To use the program, all you really need to know is that the Mandelbrot set can be
used to make some pretty pictures, but here are the mathematical details: Consider the point
that has real-number coordinates (a,b) and apply the following computation:
Let x = a
Let y = b
Repeat:
Let newX = x*x - y*y + a
Let newY = 2*x*y + b
Let x = newX
Let y = newY
As the loop is repeated, the point (x,y) changes. The question is, does (x,y) grow without
bound or is it trapped forever in a finite region of the plane? If (x,y) escapes to infinity (that
is, grows without bound), then the starting point (a,b) is not in the Mandelbrot set. If (x,y)
is trapped in a finite region, then (a,b) is in the Mandelbrot set. Now, it is known that if
x2 + y2 ever becomes strictly greater than 4, then (x,y) will escape to infinity. If x2 + y2
ever becomes bigger than 4 in the above loop, we can end the loop and say that (a,b) is not
in the Mandelbrot set. For a point (a,b) in the Mandelbrot set, this will never happen. When
we do this on a computer, of course, we don’t want to have a loop that runs forever, so we put
a limit on the number of times that the loop is executed:
x = a;
y = b;