Frame/Applet Construct

Bruce Eckel, in this book, Thinking In Java , says:
    Often you'll want to be able to create a class that can be
  invoked as either a window or an applet. To accomplish this, 
  you simply add a main() to your applet that builds an instance
  of the applet inside a Frame.
Here's a simple example of what Eckel is talking about.

You can run this little gem in three different ways:
  1. As an applet on a web page. Just write the HTML code to load the applet as usual and view it in a web browser.
  2. Using appletviewer - Run applet viewer like this:
      appletviewer frameApplet.java
    
  3. At the command line - Type the following at the command line:
      java frameApplet
    
    This is the way that is probably new to you. Read on for a discussion of what it took to make this work.
There were two methods added to the typical applet class which make it possible for the example to function as a command line application:
-------------------------------------------------
static class WL extends WindowAdapter { public void windowClosing(WindowEvent e){ System.exit(0); } } public static void main(String[] args){ frameApplet app = new frameApplet(); Frame aFrame = new Frame("frameApplet"); aFrame.addWindowListener(new WL()); aFrame.add(app, BorderLayout.CENTER); aFrame.setSize(220,100); app.init(); app.start(); aFrame.setVisible(true); } }
-------------------------------------------------
The WL class is actually optional, but if you don't include it the window which displays the applet won't close when you hit the little X in the upper right hand corner. The main method is necessary. It first initiates an instance of the applet class. Then it creates an instance of the Frame class and associates it with the WL class. It then displays the applet and calls a couple of the applet methods. You can inspect the code to figure out exactly how it all works.
-------------------------------------------------
Assignment:
Use the method demonstrated in this lesson to create an applet with a solid red background and six yellow circles arranged in a two by three pattern. In each circle place the name of a state (for instance, Pennsylvania) in black. Be prepared to demonstrate the three ways it can be displayed.