Classes

In the last lesson we talked briefly about inner classes. In this lesson we will create a single applet which actually uses three classes: 1) An applet class, 2) An event class, 3) A class which deals with data. Here's the first example:
Applet class: peaks2.java

Event class: KB_peaks2.java

Data class: Data_peaks2.java


As you can see the applet looks and functions the same as the applet in the previous lesson, but the code is organized in a much different way. Reorganizing code is sometimes referred to as refactoring. The idea of refactoring, of course, is to actually improve the code. In this case, it's hard to make a good argument for this reorganization as being an improvement, but it does illustrate how a single program can be put together using more than one class.

You will notice that it actually takes some extra lines of code to facilitate the division of the original applet into three separate classes. This is because each class must have some sort of a connection to the applet class in order for communication between them to occur. Most of these lines are fairly easy to understand, but the line

    KB = new KB_peaks2(this);
may be slightly difficult to understand. The keyword this is a way of referring to the current class. It is necessary that the keyboard class have a reference to the applet class so that the repaint method of the applet class can be called from the keyboard class. Otherwise the applets display will not be properly updated!
- - - - - - - - - - - - - - - - - - - - - - - - -
Assignment:
Take the applet you wrote for your last assignment and refactor it so that it is properly divided into three classes as illustrated in this lesson.