Java Packages

Home | Graphical Version | Table of Contents | Next Chapter, "Event-Driven Programming" | Previous Chapter, "More on the Class"



Most programming languages have a set of commands you can use to accomplish things. Java does also, but it is unlike any other. Java's commands are all inside of classes that are contained in Java's packages.
  1. More on Compiler Directives
  2. The Packages
  3. Important Classes
    1. java.awt.Graphics
    2. java.awt.Image
    3. java.lang.Math
    4. java.net.URL

More on Compiler Directives

Remember the first statement of the Hello World Program (In the first chapter - "
Introduction to Java")? Compiler directives all begin with "import" and they tell the compiler what parts of the Java programming api to load. For example, "import java.awt.Graphics;" tells the compiler to load the graphics class. You can also use the asterisk (*) wild card character. "import java.awt.*;" will include all classes from the "java.awt" package. The compiler will not include any unused classes in the compiled applet.

The Packages

The classes in the Java API, are contained in "Packages." The packages are "java.lang," "java.io," "java.util," "java.net," "java.awt," and "java.applet." "java.awt" has two sub-packages called "java.awt.peer" and "java.awt.image." It would be a good idea to peruse the Java API (I remember suggesting you download this earlier...) to see what some of the important classes are. The "java.lang" package is special because the classes contained in it are automatically in your program. They do not need an import statement.

Important Classes

java.awt.Graphics

You will use this class for drawing on your applet. It represents a graphics context, or "surface" for drawing. The class has several methods for drawing on the "surface" it represents such as "drawString," "drawRect," and "fillRect." All of these methods can be found in the API reference. It is important to note that the Graphics class is abstract so you may not use the "new" operator with it. You may only fill variables of type "Graphics" with methods that have the return type "Graphics."

java.awt.Image

This class is obvious enough; it holds a gif or jpeg picture. It contains several methods for manipulating the image it stores. As with the graphics class, it is abstract and an "Image" variable may only be obtained from a method with return type "Image."

java.lang.Math

This calss contains important mathematical functions such as square roots and logarithms. You do not need to declare a variable of type "Math" to use the methods of the class. Here is an example:

double N = 60;
double SINN = Math.sin(N);

Because it is in "java.lang," the math class is part of the Java language and needs no import statement.

java.net.URL

This class contains a Universal Resouce Locator(URL), or the location of a document on the Internet. An example URL would be "HTTP://Java.Sun.Com" the home of the makers of Java. The URL of this page is probably written in a text box at the top of your browser window.



You may have noticed that in your first programming endeavor, "Hello World" (in the first chapter Introduction to Java) the class "HelloWorld" was derived from the "Applet" class. This is what allows the user's interactions with the applet to control your program. In short, it allows event-driven programming (yet another buzzword) which is the subject of our next chapter creatively entitled "Event-Driven Programming."

Home | Top | Table of Contents | Next Chapter, "Event-Driven Programming" | Previous Chapter, "More on the Class"