Using Java library classes

6.5 Using Java library classes

While reading the code above, in both the Body and Space classes, we have come across the Color class. The second constructor of the Body class expects a parameter of type Color , and the code in the Space class creates Color objects with expressions such as

new Color(248, 160, 86)

The three parameters of the Color constructor are the red, green, and blue components of this particular color. Every color on a computer screen can be described as a composite of these three base colors. (We will discuss color a little more in Chapter 8. There, on page 136, you can also find a table of RGB color values. You can use any good graphics program to experiment with these yourself.)

Concept:

For us, the more pressing question is, where does this class come from? And how can we know what parameters its constructor expects?

The Java class library is a large

A clue to the answer is found near the top of our class, where we can find the line

collection of ready-made

import java.awt.Color; classes, provided with the Java

The class Color is one of the many classes from the Java Standard Class Library. The Java sys-

system. We can

tem comes with a large collection of useful classes which we can just use. Over time, we will get

use these classes by using an

to know many of them.

import statement.

We can see documentation for all the classes in the class library by selecting Java Library Documentation from Greenfoot’s Help menu. This will open the documentation for the Java libraries in a web browser (Figure 6.6).

The bottom left pane in this window shows a list of all classes in the Java library. (There are many of them!) We can look at the documentation for any particular class by finding it in this list and selecting it. When selected, the main part of the window will display the documentation for this class.

Exercise 6.16 Find the class Color in the class list. Select it. Look at the documentation of this class. How many constructors does it have?

Exercise 6.17 Find the description of the constructor we have used (the one with three integers as parameters). What is the legal range for these integer numbers?

You can see that there are literally thousands of classes in the Java library. To get some sort of order into this long list, classes are grouped into packages. A package is a group of logically related classes. At the top of the documentation of any class, we can see what package the class is in. The class Color , for instance, is in a package called java.awt.

When we want to use any of the classes from the Java library in our own scenario, we need to import the class, using an import statement as we have seen above. The import statement names the package and the class we want to use, with a dot in between. Thus, to use the Color class from the java.awt package, we write

import java.awt.Color;

6.6 Adding gravitational force

Figure 6.6 The Java Library Documentation

Importing a class makes it usable within our own scenario, just as if it was one of our own classes. After importing it, we can create objects of this class, call methods, and do anything else we can do with any other class.

The Java library is quite intimidating at first, because it has so many classes. Don’t worry— we shall use only a small number of them, and we shall introduce them one by one when we need them.

One of them, however, we need very soon: in the next section. What we want to do next is to add gravity to this scenario. That is, when we have more than one

body in our space, the gravitational pull between these bodies should change each body’s movement.