The last chapter was rooted solidly in the 1970’s. It used techniques often referred to as “structured” or “procedural programming” which were popular then. (We skipped right over the most popular innovation of the 60’s and the Basic programmer, spaghetti code). Certain programmers are sometimes said to “Write Fortran in any language,” and that’s more or less what we did. You now have the knowledge to accomplish with Java anything that can be done within the bounds of ANSI-standard Fortran 77.
In this chapter we’re going to move into the 1980’s. In particular we’re going to work with event driven programming. This style of programming should be very familiar to Macintosh and Windows programmers. In those environments program logic doesn’t flow from the top to the bottom of the program as it does in most procedural code. Rather the operating system collects events and the program responds to them. These events may be mouse clicks, keypresses, network data arriving on the Ethernet port, or any of about two dozen other possibilities. The operating system looks at each event, determines what program it was intended for, and places the event in the appropriate program’s event queue.
Every application program has an event loop. This is just a while loop which loops continuously. On every pass through the loop the application retrieves the next event from its event queue and responds accordingly.
Java applets behave similarly. However the runtime environment (i.e. the browser) takes care of the event loop for the applet so there’s no need to write one explicitly. Rather you need to have methods in your applet subclass that respond to each kind of event you want to process.
This is all fairly abstract until you see some concrete examples. Let’s begin with a simple one.