Home Segments Top Top Previous Next

844: Mainline

Finally, you are ready to look at the definition of the MovieAuxiliaries class, which defines two methods: one for reading movie descriptions from text files, and one for reading images from image files.

Note that several packages are loaded, including java.net and java.awt.image.

Note also that the method that reads movie descriptions is much like the method defined in Segment 613, except that it uses a resource locator to create a file stream. The method that reads images is used in Chapter 47:

import java.io.*;   import java.awt.*; 
import java.net.*;  import java.util.*; 
import java.awt.image.*; // Provides ImageProducer 
public class MovieAuxiliaries { 
 public static Vector readMovieFile(String fileName) {               
  Vector v = new Vector();  
  try { 
   URL url = MovieAuxiliaries.class.getResource(fileName);  
   if (url == null) {return null;} 
   InputStream stream = (InputStream) (url.getContent()); 
   if (stream == null) {return null;} 
   InputStreamReader reader = new InputStreamReader(stream); 
   StreamTokenizer tokens = new StreamTokenizer(reader);                
   tokens.quoteChar((int) '"'); tokens.eolIsSignificant(true);  
   while (tokens.nextToken() != tokens.TT_EOF) { 
    Movie m; 
    String nameString = tokens.sval; 
    tokens.nextToken(); int x = (int) tokens.nval; 
    tokens.nextToken(); int y = (int) tokens.nval; 
    tokens.nextToken(); int z = (int) tokens.nval; 
    if (tokens.nextToken() == tokens.TT_EOL) { 
     m = new Movie(x, y, z, nameString); 
    } 
    else { 
     String posterString = tokens.sval; 
     m = new Movie(x, y, z, nameString, posterString); 
     tokens.nextToken(); 
    } 
    v.addElement(m); 
   } 
   return v; 
  } 
  catch (IOException e) {System.out.println(e);} 
  return null; 
 } 
 public static Image readMovieImage(String fileName) {               
  try { 
   URL url = MovieAuxiliaries.class.getResource(fileName);                       
   if (url == null) {return null;} 
   ImageProducer producer = (ImageProducer) (url.getContent()); 
   if (producer == null) {return null;} 
   Toolkit tk = Toolkit.getDefaultToolkit(); 
   Image image = tk.createImage(producer); 
   return image; 
  } 
  catch (IOException e) {System.out.println(e);}; 
  return null; 
}}