Home > Articles > Programming > Java

Oh, So That's How A Java App Works!

  • Print
  • + Share This
  • Explanation of the Example Program

  • Where an Application Starts

  • Runtime Internals: Stack and Heap

  • The Class "Object"

  • Some Light Relief

Ever since The C Programming Language (Kernighan, Brian and Dennis Ritchie, Prentice-Hall) was published in 1978, writers of programming text books have been using the "Hello World" program as an introductory example. Programmers deserve a bit of innovation. Java is the first popular language to support graphics, networking, multimedia, multi-threaded code, and software portability. Surely we can do a bit better than a first example that spits up a bit of text on the screen?

Chapter 1, "What is Java?," introduced an example program that did some GUI work to display a window and to roll some text across it in several directions and colors. What we'll do in this chapter is look at that code in more detail, and explain how it works. Then we'll round off the chapter with a discussion of the stack and the heap and what they do.

If you haven't yet installed the JDK and tried running the myframe example from chapter 1, now would be an excellent time to do that. As the great Arnold Schwarzenegger once said, "You can't get muscles by watching me lift weights."

The source listing appears on the following page with the annotations appearing on the page after that.


Explanation of the Example Program

  1. The import keyword saves the programmer from having to write out the full package names of all the library classes that will be used in this source file. Here we are importing java.awt.* which means all the classes in the java.awt package, just as * means all the files in a directory. It means we can write Frame or Font instead of java.awt.Frame or java.awt.Font. The java.awt package contains basic windowing support.

  2. This box encloses the class we have written. The class is called myframe, and it has some field members (boxes 4 and 5) and two method members (boxes 6 and 7).

  3. To say that we want this class to be a subclass of the Frame class, use extends Frame. Frame is a class in the AWT package that displays a basic window on the screen. By saying we extend Frame, we get all the things that Frame can do, plus we can add our own specializations or variations.

  4. These four fields (x, y, i, LtoR) are declared static so that we can reference them without an instance of the class existing.

  5. These three fields represent the text that we are going to move across the screen, and its color and font. Font and Color are two classes from the java.awt package. We declare a Font variable called fb and create an instance of it using a constructor from the Font class. The variables msg and color are both four-element arrays which we initialize here.

  6. The paint() method is a standard part of many classes in the awt package. The convention is that the Java runtime will call it when the window system needs to update what is on the screen. Since we extended the Frame class, our version of paint() here replaces the basic one in Frame. (This is a piece of OOP that we haven't covered yet).

    When you call setVisible(true) on a Frame, the window system knows it has to display it. It does that by calling our paint method at the right times to put it up on the screen. It will call paint when execution starts and any time after that when we request it. The statements inside paint set the default font and color, and then write some text from msg[i] onto the graphics context argument at location x,y. The window system translates that into pixels on the screen.

  7. The main() method has a special signature, or method name and arguments that is recognized as the place where a program starts executing when you run the class that it is in. More about this in the next section.

  8. The first variable we declare inside the main routine is a myframe. That gives us an instance variable on which we can invoke the ordinary (non-static) methods of myframe and its parent Frame. The first statement is a method call, mf.setSize(200,200), to set mf's size to 200 pixels wide and 200 pixels high. Again, this is a method we inherit by virtue of extending java.awt.Frame.

  9. This is in the body of a loop that is executed a few hundred times. Our first action is to go to sleep and delay the program for 25 milliseconds. Since the method sleep() is a static method of the standard Java runtime class Thread, we can just invoke it with the classname. Animation looks better if you pace it out.

    After the loop delay, we ask for the mf instance of myframe to be scheduled for repainting on the screen. What we are going to do here is change the location where we display the text a little bit each time through the loop. The overall effect will be to make the text appear to glide across the screen.

  10. This is the "else" part of an "if…then…else" statement that is in the body of the big loop. Just for fun, the program alternates between rolling the text across the screen horizontally and vertically. This "if" statement is where the choice is made. If the LtoR variable has the value 1, we execute the "then" part. Otherwise, we execute the "else" part that drops the text vertically.

    First, we increase y by three and check that the result is less than 200, that being the height of the screen. The variable y, of course, is used in paint as one of the coordinates for locating the text message. By changing its value, we change where the text is drawn. If y is less than 200, then continue. That means branch immediately to the head of the loop. If y is greater than 200, then we are at the end of scrolling the text down, and we want to select another message and color, and scroll the other way. We increment i, ensuring it stays in the range 0–3, we reset x,y and we change LtoR so that the "then" part of the "if" statement before 10 will be chosen. That clause is similar to the "else" clause, but increments the x variable, instead of y, to move text horizontally.

Applications Versus Applets Versus Servlets

A Java program can be written to run in these three different ways:

  • As a stand-alone program that can be invoked from the command-line, termed an application. The sample program we were reviewing is an application.

  • As a program embedded in a web page, to be downloaded to the client and run in the browser when the page is browsed, termed an applet. Just as a booklet is a little book, an applet is a little application.

  • As a program invoked from a web server, to be run on the server when a particular URL is browsed, termed a servlet.

The execution vehicles differ in the default execution privileges they have and the way in which they indicate where to start execution. Almost all the code and examples are the same whether you are writing an application, an applet, or a servlet. Only a few trivial startup details differ. We will deal with applications here, and applets in a later chapter. How and why to set up a servlet is a bit too far off-topic for the scope of this text.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.