18. Sockets and Networking

18.5. Adding Text Network Resources for an Application

The SlideShowFrame illustrates the use of multimedia resources from the web. However, much of the files we may want to retrieve are text based, and we want to be able to use these resources side-by-side with the available multimedia. The next application describes how to do just this.

 

,,

 

 

 

 

 

 

 

 

 

 

 

 

 

J

Figure 15.14: The Timer class.

 

Problem Specification

Suppose a realtor asks you to write a Java application that will allow

customers to view pictures and descriptions of homes from an online Problem statement

database. The application should allow the customer to select a home and should then display both an image of the home and a text description of its features, such as square footage, asking price, and so on.

Suppose that the database of image and text files is kept at a fixed loca- tion on the Web, but the names of the files themselves may change. This will enable the company to change the database as it sells the homes. The company will provide a text file that contains the names of the files for the current selection of homes to input into the program. To simplify matters, both image and text files have the same name but different extensions— for example, ranch.txt and ranch.gif. The data file will store just the names of the files, one per line, giving it the following format:

,,

 

 

J

Downloading a Text File from the Web

This application requires us to solve two new problems:

How do we download a text file of names that we want to use as menu items?

How do we download a text file and display it in a JTextArea?

How do we download and display an image file?

The SlideShowFrame solves the problem of downloading and display- ing an image file. So, the most challenging part of this program is the task

of downloading a Web text file and using its data in the program.Understanding the problem

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

File download algorithm


For this program we must make use of two types of text data down- loaded from the Web. The first will be the names of the image and docu- ment files. We’ll want to read these names and use them as menu items that the user can select. Second, once the user has selected a house to view, we must download and display an image and a text description of the house. Downloading the text is basically the same as downloading the file of names. The only difference is that we need to display this text in a JTextArea. Downloading the image file can be handled in more or less the same way that it was handled in the SlideShowFrame— by using a special Java method to download and display the image file.

Clearly, the problems of downloading a file from the Web and reading a file from the disk are quite similar. Recall that we used streams to handle the I/O operation when reading disk files. The various InputStream and OutputStream classes contained the read() and write() meth- ods needed for I/O. The situation is the same for downloading Web files. Recall that the URL class contains the openStream() method, which opens an InputStream to the resource associated with the URL. Once the stream has been opened, you can read data from the stream just as if it were coming from a file. The program doesn’t care whether the data are coming from a file on the Internet or a file on the disk. It just reads data from the stream. So, to download a data file from the Internet, regardless of whether it’s a text file, image file, audio file, or whatever, you would

use the following general algorithm:

,,

URL ur l ;

Input Stream data ;

t r y {u r l = new URL( file URL ) ;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Text or binary data?


// C r e a t e a URL

data = ur l . openStream ( ) ; // Ope n a s t r e a m t o URL

// READ THE F I L E INTO MEMORY} // R e a d d a t

data . c l o s e ( ) ;// C l o s e t h e s t r e a m

catch ( MalformedURLException e )// T h r o w n b y URL ( )

System . out . p r i n t l n ( e . getMessage ( ) ) ;

catch ( IOException e )

System . out . p r i n t l n ( e . getMessage ( ) ) ;

\} J

The algorithm consists of four basic steps:

Create a URL instance.

Open an InputStream to it.

Read the data.

Close the stream.

Step 3 of this algorithm—read the data—involves many lines of code and has, therefore, been left as a subtask suitable for encapsulation within a method.

Reading the Data

As we saw in the previous chapter, the algorithm for step 3 will depend on the file’s data. If it’s a text file, we would like to read one line at a time, storing the input in a String. If it’s an image or an audio file, we would read one byte at a time.

 

Because our data are contained in a text file, we want to read one line at What library methods can we use?

a time. The BufferedReader class contains a readLine() method that returns either a String storing the line or the value null when it reaches the end of file. The following method shows how you would read a text file into the program’s JTextArea, which is named display:

,,

 

 

 

 

 

 

 

 

 

J

The method is passed the file’s URL and it uses the URL.openStream()

method to open the input stream.Note that the method throws

IOException, which means that any I/O exceptions that get raised will I/O exceptions

be handled by the calling method.

In this example, the input algorithm reads each line of the file and adds it to the display. For our real estate application, the same basic algo- rithm can be used to read the names of the data files and store them in a menu from which a user makes selections. For example, if we use a JComboBox menu named homeChoice, we would simply add each line to it:

,,

 

 

 

J

Interface Design

The interface for this application is very important. It should provide some means to display a text file and an image. The text file can be displayed in a JTextArea, and the image can be drawn on a JPanel.

Next, let’s consider the types of controls a user might need. The cus-

tomer should be allowed to select a home to view from a menu of options. Because the program will have the list of available homes, it can provide the options in a JComboBox pull-down menu.

To create an appropriate layout, we want to make sure that the controls,

the image, and JTextArea all have their own region of the application’s window. This suggests a BorderLayout, which is the default layout for a JFrame. We can put the JComboBox menu at the “North” border, and the image and text on the “West” and “East” borders, respectively. Figure 15.15 illustrates these various design decisions.

 

Figure 15.16: The RealEstate- Viewer class defines the user in- terface.

 

Figure 15

 

JFrame


JPanelJComboBoxJTextArea


Component Hierarchy JFrame

JComboBox Menu Display JTextArea Canvas JPanel


sign for t

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Problem Decomposition: RealEstateViewer

 

 

The task of downloading and displaying information from the Internet is best handled by two separate classes: One to perform the downloading and user interface tasks and the other to take care of displaying the image. The task of downloading the image and text files from the Web can be handled by the program’s main class, the RealEstateViewer, which will also handle the user interface (Fig. 15.16). As the application’s top- level window, RealEstateViewer will is subclass of JFrame.Be- cause its controls will include a JComboBox, it must implement the

itemStateChanged() method of the ItemListener interface.

What components and other instance variables will we need for this class? According to our interface design, it will need a JComboBox, a JTextArea, and the ImagePanel. Because it will be downloading images, it will need an Image variable.

The constants used by this application include the URL string for the

data file. Also, because all the images and data files will start with the same prefix,

,,

 

J

we should make this a constant in the program. These preliminary de- cisions lead to the initial version of RealEstateViewer shown in Fig- ure 15.17. Note that the main() method merely creates an instance of the application and shows it. Note also that the currentImage variable is declared public. This will let the ImagePanel have direct access to currentImage whenever it needs to display a new image.

 

,,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

J

Figure 15.17: The RealEstateViewer, Version 1.

 

 

The ImagePanel Class

We’ll use a second class, the ImagePanel, to handle displaying the im- age (Figs. 15.18 and 15.19). The reason we use a separate class for this task is that we want the image to appear in its own panel (which appears on the West border of the main window). In addition to its constructor, the only method needed in this class is the paintComponent() method. This method will be called automatically whenever the main window is repainted. Its task is simply to get the current image from its parent frame and display it. Note that a reference to the parent frame is passed to the object in its constructor.

Method Decomposition

The stub methods listed in the initial version of RealEstateViewer (Fig. 15.17) outline the main tasks required by the application. Some of these methods are very simple and even trivial to implement. Others should be broken up into subtasks.


JPanel

 

+paintComponent()

 

 

 

ImagePanel

-frame : RealEstateViewer

+ImagePanel(in f : RealEstateViewer)

+paintComponent()

 

Figure 15.18: An overview of the

ImagePanel class.

 

,,

 

 

 

 

 

 

 

 

 

 

 

J

Figure 15.19: The ImagePanel class.

 

 

The constructor method should be responsible for creating the user in- terface, most of which will involve the routine tasks of registering a lis- tener for the homeChoice menu and setting up an appropriate layout that implements the design we developed for the user interface:

,,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ItemListener


J

Note the last two statements of the method. The first sets up the JComboBox by reading its contents from a file stored in the company’s database. Because that task will require several statements, we define it as a separate method, initHomeChoices(), and defer its develop- ment for now. Similarly, the task of displaying the current menu choice has been organized into the showCurrentSelection() method, whose development we also defer for now.

The itemStateChanged() method is called automatically when the user selects a home from the JComboBox menu. Its task is to download and display information about the current menu selection. To do this, it can simply call the showCurrentSelection() method:

,,

 

 

 

J

 

Downloading the Menu Items

Recall that according to our specification, the real estate firm stores its current listing of homes in a text file, one home per line. The initHomeChoices() method downloads the text and uses its contents to set up the items in the homeChoice JComboBox menu:

,,

 

 

 

 

 

 

 

 

 

 

 

 

J

It uses the algorithm we developed earlier for downloading a text file. Each line of the text file represents a menu item, so, as each line is read by readLine(data), it is added to the JComboBox menu.

Downloading and Displaying Home Information

The showCurrentSelection() method is responsible for download- ing and displaying images and text files whenever the user selects a home to view. Recall that our specification called for using the name of the menu item as a basis for constructing the name of its corresponding text file and image file. Therefore, the basic algorithm we need is

 

 

Get the user’s home choice.

Create a URL for the associated text file.

Download and display the associated text file.

Create a URL for the associated GIF file.

Download and display the image.

 

 

 

Because downloading a text document requires stream processing, we should handle that in a separate method. The task of downloading an

image file is also a good candidate for a separate method. Both of theseMethod decomposition

methods will use a URL, so we can leave that task up to showCurrent- Selection() itself. The showCurrentSelection() method will cre-

 

ate the URLs and then invoke the appropriate methods to download and display the resources:

,,

 

 

 

 

 

 

 

 

 

 

 

 

 

J

Note that we have also elected to handle both the MalformedURLException and IOException in this method. The advantage of this design is that it separates exception handling from the normal algorithm and organizes it into one method. Finally, note how string concatenation is used to build

the URL specifications, each of which consists of three parts: the baseURL, the user’s choice, and the file extension.

The task of reading the text file and displaying its contents has been en- capsulated into the readTextIntoDisplay() method. This private utility method performs a standard file-reading algorithm using the readLine() method that we developed earlier. Figure 15.20 provides a view of the program’s appearance as it is displaying information to a user. Figure 15.21 provides the complete implementation of this program.

 

 

Figure 15.20: The RealEstate- Viewer program downloads im- ages and documents over the Web.


 

 

Reusing Code

As in other examples we have developed, our discovery and use of the

javax.imageio.ImageIO.read() method and other classes from the

 

 

 

 

,,

import j ava . awt . ;

import j ava . awt . event . ;

import j ava . net . ;

import j ava . io . ;

import j avax . swing . ;

public c l a s s Real Estate Viewer extends JFrame

implements I tem Listener

public s t a t i c f i n a l in t WIDTH=400 ,HEIGHT= 200 ;

private f i n a l S t r i n g dataFileURL =

http :// j ava . t r i n c o l l . edu/˜ j j j a v a /homes/homes . t x t ;

private f i n a l S t r i n g baseURL =

http :// j ava . t r i n c o l l . edu/˜ j j j a v a /homes/” ;

private JText Area display = new JText Area ( 2 0 , 2 0 ) ;

private JComboBox homeChoice = new JComboBox ( ) ;

private ImagePanel imagePanel = new ImagePanel ( t h i s ) ;

public Image current Image = null ;

 

public Real Estate Viewer ( )

super ( ”Home Viewer Application ) ; // S e t wi n do w t i t l e

homeChoice . add Item Listener ( t h i s ) ;

t h i s . get Content Pane ( ) . add ( North” , homeChoice ) ;

t h i s . get Content Pane ( ) . add ( East , display ) ;

t h i s . get Content Pane ( ) . add ( Center , imagePanel ) ; display . setLineWrap ( t rue ) ;

initHomeChoices ( ) ;// S e t up t h e c h o i c e b o x

show Current Selection ( ) ;// D i s p l a y t h e c u r r e n t home

} // R e a l E s t a t e V i e w e r ( )

private void initHomeChoices ( )

t r y

URL ur l = new URL( dataFileURL ) ;

Buffered Reader data = new Buffered Reader (

new InputStreamReader ( ur l . openStream ( ) ) ) ; S t r i n g l i n e = data . readLine ( ) ;

while ( l i n e ! = null ) homeChoice . addItem ( l i n e ) ; l i n e = data . read Line ( ) ;

data . c l o s e ( ) ;

catch ( MalformedURLException e )

System . out . p r i n t l n ( ”ERROR: + e . getMessage ( ) ) ;

catch ( IOException e )

System . out . p r i n t l n ( ”ERROR: + e . getMessage ( ) ) ;

}

// i n i t H o m e C h o i c e s ( )

\J

Figure 15.21: The RealEstateViewer class, Part I.

 

 

 

 

,,

private void read Text Into Display (URL ur l ) throws IOException BufferedReader data

= new BufferedReader (

new InputStreamReader ( ur l . openStream ( ) ) ) ;

display . s e t T e xt ( ”” ) ;// R e s e t t h e t e x t a r e a

S t r i n g l i n e = data . readLine ( ) ;

while ( l i n e ! = null )// R e a d e a c h l i n e

display . append ( l i n e + ” n” ) ;// And a d d i t t o t h e d i s p l a y

l i n e = data . read Line ( ) ;

} data . c l o s e ( ) ;

}// r e a d T e x t I n t o D i s p l a y ( )

private void show Current Selection ( )

URL ur l = null ;// G e t u s e r s c h o i c e

S t r i n g choice = homeChoice . get Selected I tem ( ) . t o S t r i n g ( ) ;

t r y

ur l = new URL( baseURL + choice + . t x t ) ;// C r e a t e URL read Text Into Display ( ur l ) ; // D o w n l o a d a n d d i s p l a y t e x t f i l e ur l = new URL( baseURL + choice + . g i f ) ;// C r e a t e URL

// D o w n l o a d i m a g e

current Image = ImageIO . read ( ur l ) ;

T o o lkit . ge t De fa ult T o o lk i t ( ) . beep ( ) ;// A l e r t t h e u s e r

r e pa int ( ) ;

catch ( MalformedURLException e )

System . out . p r i n t l n ( ”ERROR: + e . getMessage ( ) ) ;

catch ( IOException e )

System . out . p r i n t l n ( ”ERROR: + e . getMessage ( ) ) ;

}

}// s h o w C u r r e n t S e l e c t i o n ( )

public void itemState Changed ( ItemEvent evt ) show Current Selection ( ) ;

} // i t e m S t a t e C h a n g e d ( )

public s t a t i c void main ( S t r i n g args [ ] )

Real Estate Viewer viewer = new Real Estate Viewer ( ) ; viewer . s e t S i z e ( viewer .WIDTH, viewer . HEIGHT ) ;

viewer . s e t V i s i b l e ( t rue ) ;

viewer . addWindowListener ( new WindowAdapter ( )

public void windowClosing ( WindowEvent e ) System . e x i t ( 0 ) ;// Q u i t t h e a p p l i c a t i o n

}

} ) ;

}// m a i n ( )

// R e a l E s t a t e V i e w e r

\J

Figure 15.21: (continued) RealEstateViewer, Part II.

 

Java class library illustrate an important principle of object-oriented pro- gramming.

 

 

 

 

 

An important step in designing object-oriented programs is making ap- propriate use of existing classes and methods. In some cases, you want to directly instantiate a class and use its methods to perform the desired tasks. In other cases, it is necessary to create a subclass (inheritance) or im- plement an interface (inheritance) in order to gain access to the methods you need.

Of course, knowing what classes exist in the libraries is something that comes with experience. There’s no way that a novice Java programmer would know about, say, the ImageIO.read() method. However, one skill or habit that you should try to develop is always to ask yourself the question: “Is there a method that will do what I’m trying to do here?” That question should be the first question on your search through the libraries and reference books.

,,

 

J