Technical Support
Discussion Forum
Online Training
Read About Java
Java In-Depth
Product Discounts
Membership Information JDC Resources DukeDollars Early Access

Java Cup Logo

JDC Applets
(chat, newsreader, calendar)

Log Out

Online Training
shadowSearchFAQFeedback

Course Notes Table of Contents
Exercises | Online Training Index

Help: Getting Resources with Clara Barton
MageLang Institute

Help is available for each task, or you can go straight to the solution source code.

Task 1

Find the resource file 'Barton.resources' and open an InputStream to it.
The Class.getResourceAsStream will suffice in doing this.
InputStream is = getClass().getResourceAsStream(
  "Barton.resources");

Task 2

Load the properties.
Use the Property.load method to load them from the InputStream.
p.load (is);
A property list is a set of key-value pairs. When you ask about the contents of a key (Property.getProperty), it returns its value (if it exists). This exercise can use three. For example:
message.font=SansSerif-bold-24
The Font.decode method enables you to decode strings of this format (name-style-height).
The program uses this property as the font for the following message.
message.text=Volunteers Needed
The program uses this property as a message to display
image.file=redcross.gif
The program uses this property as the name of another resource to display as an image.

Task 3

Get the 'message.font' property. If not found, use the default fontDefault. Once acquired, decode it into a Font.
Use the Property.getProperty method to find the value. Use the Font.decode method to convert the text string into a Font.
f = Font.decode(
  p.getProperty("message.font", fontDefault));

Task 4

Get the 'message.text' property. If not found, use the default msgDefault. This provides a short text message/slogan to display.
msg = p.getProperty ("message.text", msgDefault);

Task 5

Get the 'image.file' property. This provides the name of a file to display. Find the resource file specified as the property and load the image.
Use Property.getProperty to get the name of the image resource file. Use Class.getResource to get the resource as a URL. Use Toolkit.getImage to get the image.
String imageFile = p.getProperty ("image.file");
URL url = getClass().getResource (imageFile);
im = getToolkit().getImage (url);

Task 6

Compile and run the Barton program. All the work to display everything has been done for you. Since there is no resource file yet, all the default behavior will be used.
javac Barton.java
java Barton

Task 7

Place the Barton.resources file in the current directory, or somewhere in your CLASSPATH. Make sure the redcross.gif file is also in the current directory, or somewhere in your CLASSPATH. Then, rerun the program.


Copyright © 1997 MageLang Institute. All Rights Reserved
May-97 Copyright © 1996, 1997 Sun Microsystems Inc. All Rights Reserved