Chapter 12.1.3 Resources | Introduction to Programming Using Java

Chapter 12.1.3 Resources | Introduction to Programming Using Java

 

12.1.3 Resources

 

Throughout this textbook, up until now, we have been thinking of a program as made up entirely of Java code. However, programs often use other types of data, including images, sounds, and text, as part of their basic structure. These data are referred to as resources. An example is the image file, cards.png, that was used in the HighLowWithImages.java program earlier in this section. This file is part of the program.

The program needs it in order to run. The user of the program doesn’t need to know that this file exists or where it is located; as far as the user is concerned, it is just part of the program. The program of course, does need some way of locating the resource file and loading its data.

 

Chapter 12.1.3 Resources | Introduction to Programming Using Java

 

Resources are ordinarily stored in files that are in the same locations as the compiled class files for the program. Class files are located and loaded by something called a class loader, which is represented in Java by an object of type ClassLoader. A class loader has a list of locations where it will look for class files. This list is called the class path. It includes the location where Java’s standard classes are stored.

It generally includes the current directory. If the program is stored in a jar file, the jar file is included on the class path. In addition to class files, a ClassLoader is capable of locating resource files that are located on the class path or in subdirectories of locations that are on the class path.

The first step in using a resource is to obtain a ClassLoader and to use it to locate the resource file. In the HighLowWithImages program, this is done with:

 

Chapter 12.1.3 Resources | Introduction to Programming Using Java

 

The idea of the first line is that in order to get a class loader, you have to ask a class that was loaded by the class loader. Here, HighLowWithImages.class is a name for the object that represents the actual class HighLowWithImages. In other programs, you would just substitute for “HighLowWithImages” the name of the class that contains the call to getClassLoader().

The second line uses the class loader to locate the resource file named cards.png. The return value of cl.getResource() is of type java.net.URL, and it represents the location of the resource rather than the resource itself. If the resource file cannot be found, then the return value is null. The class URL was discussed in Subsection 11.4.1.

Often, resources are stored not directly on the class path but in a subdirectory. In that case, the parameter to getResource() must be a path name that includes the directory path to the resource. For example, suppose that the image file “cards.png” were stored in a directory named images inside a directory named resources, where resources is directly on the class path. Then the path to the file is “resources/images/cards.png” and the command for locating the resource would be

 

Chapter 12.1.3 Resources | Introduction to Programming Using Java

 

Once you have a URL that represents the location of a resource file, you could use a URLConnection, as discussed in Subsection 11.4.1, to read the contents of that file. However, Java provides more convenient methods for loading several types of resources. For loading image resources, a convenient method is available in the class java.awt.Toolkit.

It can be used as in the following line from HighLowWithImages, where cardImages is an instance variable of type Image and imageURL is the URL that represents the location of the image file:

 

Chapter 12.1.3 Resources | Introduction to Programming Using Java

 

This still does not load the image completely—that will only be done later, for example when cardImages is used in a drawImage command.

The Applet and JApplet classes have an instance method that can be used to load an image from a given URL:

 

117 Chapter 12.1.3	Resources | Introduction to Programming Using Java

 

When you are writing an applet, this method can be used as an alternative to the createImage() method in class Toolkit.

More interesting is the fact that Applet and JApplet contain a static method that can be used to load sound resources:

 

118 Chapter 12.1.3	Resources | Introduction to Programming Using Java

 

Since this is a static method, it can be used in any program, simply by calling it as

Applet.newAudioClip(soundURL) or JApplet.newAudioClip(soundURL). (This seems to be the only easy way to use sounds in a Java program; it’s not clear why this capability is only in the applet classes.) The return value is of type java.applet.AudioClip. Once you have an AudioClip, you can call its play() method to play the audio clip from the beginning.

Here is a method that puts all this together to load and play the sound from an audio resource file:

 

Chapter 12.1.3 Resources | Introduction to Programming Using Java

 

This method is from a sample program SoundAndCursorDemo that will be discussed in the next subsection. Of course, if you plan to reuse the sound often, it would be better to load the sound once into an instance variable of type AudioClip, which could then be used to play the sound any number of times, without the need to reload it each time.

The AudioClip class supports audio files in the common WAV, AIFF, and AU formats.

 

 

 

SEE MORE: