// scrlimg - Image Scrolling
// Compiled under JDK 1.2.2. NT 4.0, SP5
// Tested:  1.1.8 and 1.2.2, app and with appletviewer
// Joe Sam Shirah  jshirah@ibm.net
// © Autumn Software, 1999

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;


public class scrlimg   extends    Applet
                       implements ActionListener,
                                  WindowListener
{  
  boolean isApplet = true;

  Button bSelect,
         bScroll;

  FileDialog myFD = null;
  Frame myFrame;
        
  Image selImage = null;

  Panel pSouth;
        
  MediaTracker myMT;

  ScrollPane sp;

  TextField taX,
            taY;


  public scrlimg()  // constructor
  {
    myFrame = new Frame("Scroll Image");
    myFrame.setBackground(Color.lightGray);

    bSelect = new Button( "Select" );
    bSelect.addActionListener(this);
    bScroll = new Button( "Scroll( x, y )" );
    bScroll.addActionListener(this);

    pSouth = new Panel();          
    sp = new ScrollPane();
    taX = new TextField( "0", 2 );
    taY = new TextField( "0", 2 );

    myMT = new MediaTracker(this);
  }  // end constructor


  public void start()
  {
    sp.add( this );
    pSouth.add( bSelect );
    pSouth.add( bScroll );
    pSouth.add( taX );
    pSouth.add( taY );
    myFrame.add( sp, BorderLayout.CENTER );
    myFrame.add( pSouth, BorderLayout.SOUTH );

    myFrame.addWindowListener( this );
    myFrame.pack();
    Dimension dim = myFrame.getSize();
    myFrame.setSize( dim.width, 200 );
    myFrame.setLocation( 100, 100 );
    myFrame.show();
           
    return;
 }  // end start


  void loadImage( String myImgFile )
  {
    if( selImage != null )
    {
      myMT.removeImage( selImage );
      selImage = null;
    }

    selImage = Toolkit.getDefaultToolkit().getImage( myImgFile );
    myMT.addImage(selImage, 1 );
    try { myMT.waitForID( 1 ); }
    catch(InterruptedException e) {}
  }  // end loadImage


  public void update(Graphics g)
  {
    paint( g );
  }  // end update
  

  public void paint(Graphics g)
  {
    if( selImage != null )
    {
      g.drawImage( selImage, 0, 0, this);
    }
  }  // end paint


  public Dimension getPreferredSize()
  {
     if( selImage != null )
    {
      return new Dimension( selImage.getWidth( this ), 
                            selImage.getHeight( this ) );
    }
    return super.getPreferredSize();
  } // end getPreferredSize


  // ActionListener Event
  public void actionPerformed(ActionEvent event)
  {
    Object source = event.getSource();
       
    if ( (source == bSelect) )
    {
      if( myFD == null )
      {
        myFD = new FileDialog( myFrame, 
                   "Select Image File", FileDialog.LOAD );
      }
      myFD.show();
          
      String myImgFile = myFD.getFile();
      if( myImgFile != null )
      {
        myImgFile = myFD.getDirectory() + myFD.getFile();
        loadImage( myImgFile );

        setSize( selImage.getWidth( this ), 
                 selImage.getHeight( this ) );
        sp.doLayout();
      }  // end if myImgFile != null
      myFD.dispose();
      return;
    }  // end if source is bSelect

    if ( (source == bScroll) )
    {
      sp.setScrollPosition( Integer.parseInt( taX.getText() ),
                            Integer.parseInt( taY.getText() ));
      return;
    } // end if source is bScroll
  }  // end actionPerformed


  // Window Listener Implementation
public void windowOpened(WindowEvent e){};
  public void windowClosing(WindowEvent e)
  {
    myFrame.dispose();
    if( !isApplet ) { System.exit(0); }
  }  // end windowClosing
public void windowClosed(WindowEvent e){};
public void windowIconified(WindowEvent e){};
public void windowDeiconified(WindowEvent e){};
public void windowActivated(WindowEvent e){};
public void windowDeactivated(WindowEvent e){};
  // end Window Listener Implementation


  public static void main(String[] args)
  {
    // create class instance
    scrlimg myApp = new scrlimg();
    // set instance variable
    myApp.isApplet = false; // set flag to application

    myApp.init();
    myApp.start();
  }  // end main 

} // End Class scrlimg


