import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
import java.io.*;
import java.util.*;
import corejava.*;

public class Reader4 extends corejava.CloseableFrame
   implements ActionListener
{  public Reader4()
   {  setTitle("Reader");

      MenuBar mbar = new MenuBar();
      Menu m = new Menu("File");
      MenuItem m1 = new MenuItem("Open");
      m1.addActionListener(this);
      m.add(m1);            
      MenuItem m2 = new MenuItem("Exit");
      m2.addActionListener(this);
      m.add(m2);            
      mbar.add(m);
      addWindowListener(new WindowAdapter() 
      {  public void windowClosing(WindowEvent e) 
         { System.exit(0); } 
      } );
      setMenuBar(mbar);
      setSize(600, 200);
      show();
   }

   public void actionPerformed(ActionEvent evt)
   {  String arg = evt.getActionCommand();
      if (arg.equals("Open"))
      {  FileDialog d = new FileDialog(this,
            "Open image file", FileDialog.LOAD);
         d.setFile("*.txt");
         d.setDirectory(lastDir);
         d.show();
         String f = d.getFile();
         lastDir = d.getDirectory();
         if (f != null)
            image = Toolkit.getDefaultToolkit().getImage(lastDir + f);
         repaint();
      }
      else if(arg.equals("Exit")) System.exit(0);
   }   

   public int centerText(String s, int clientWidth) {
      int w1 = fm.stringWidth(s);
      int cx = (clientWidth - w1) / 2;          // System.out.println(cx);
      return cx;
   }
   
   public void setFonts(Graphics g) {
      Font f = new Font("Dialog", Font.PLAIN, 64);
      g.setFont(f);
      fm = g.getFontMetrics(f);
   }
   
   public void fillWordArray() {
     int k=st.countTokens();
     StringArray = new String[k];               // System.out.println(k);
     int j=0;
     while (st.hasMoreTokens()) {
         StringArray[j] = st.nextToken();       // System.out.println(st.nextToken());
         j++;
     } 
   }
   
   public void paint(Graphics g) {
      fillWordArray();      // load up the words
      setFonts(g);          // setting fonts

      // initizialize positioning
      Dimension d = getSize();
      Insets in  = getInsets();
      int h1 = fm.getMaxAscent();
      int clientWidth = d.width - in.right - in.left;
      int clientHeight = d.height - in.bottom - in.top;
      int cy = (clientHeight + h1) / 2 + in.top;
      int cx = 0;
      
      g.drawRect(in.left, in.top, clientWidth-1, clientHeight-1);    // set drawing space

      // display words
      int len = StringArray.length;
      for (int j=0; j<len; j++) {
        g.clearRect(50,50,clientWidth-50,clientHeight-40);  // clear drawing window
        String s1 = StringArray[j];                 // load word for display
        cx = centerText(s1,clientWidth);            // calculate position
        g.drawString(s1, cx, cy);                   // draw
        try { Thread.sleep(delay); }                // sleep
        catch (InterruptedException e) {}
      }
   }

   
   public static void main(String args[]) {
      // MyThread = Thread.currentThread();
      new Reader4();
   }

   // initializing variables
   private String bigS = "Radio DJ breaks Guinness record - - - - - - - - - - - - BY ASSOCIATED PRESS May 29, 2001 | JERSEY CITY, N.J. --  Glenn Jones has talked and talked and talked and talked.  As of 6:30 a.m. EDT Tuesday, the disc jockey had been talking for about 93 hours, easily shattering the record for the longest continuous radio broadcast. And he was still talking.  'It's been a test of wills, a test of determination,' Jones said of his achievement.s";

   private StringTokenizer st = new StringTokenizer(bigS);
   private int delay = 200;              // # milliseconds between words
   private String[] StringArray;
   private Image image = null;
   private String lastDir = "";
   private FontMetrics fm;
}





