//*******************************************************************
//
//   Zoom.java          In Text          Applet
//
//   Authors:  Lewis and Loftus
//
//   Classes:  Zoom
//             Zoom_Listener
//
//*******************************************************************

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

//-------------------------------------------------------------------
//
//  Class Zoom demonstrates the use of a scrollbar.  The applet
//  allows the user to zoom in and out on an image.
//
//  Methods:
//
//     public void init()
//     public void zoom_image (int size)
//     public void paint (Graphics page)
//
//-------------------------------------------------------------------

public class Zoom extends Applet {

   private final int SIZE = 300;

   private Scrollbar bar =
      new Scrollbar(Scrollbar.HORIZONTAL, 10, 64, 10, SIZE);

   private Zoom_Listener listener = new Zoom_Listener (this);

   private int current_size = 10;
   private Image picture;

   //===========================================================
   //  Sets up the applet and retrieves the image.
   //===========================================================
   public void init() {

      setLayout (new BorderLayout());
      setBackground (Color.white);

      bar.addAdjustmentListener (listener);
      picture = getImage (getDocumentBase(), "owl.gif");
      //      picture = getImage ("file:\A\ch10\owl.gif");
      add (bar, "South");

      //      setSize (SIZE, SIZE);
       //      setVisible (true);
    show(true);
    }  // method init

    //===========================================================
    //  Repaints the image after adjusting its size.
    //===========================================================
    public void zoom_image (int size) {
       current_size = size;
       repaint();
    }  // method zoom_image

    //===========================================================
    //  Paints the image, scaled to the current size.
    //===========================================================
    public void paint (Graphics page) {

      page.drawImage (picture, SIZE/2-current_size/2,
        SIZE/2-current_size/2, current_size, current_size, this);
      page.drawString(getDocumentBase().toString(), 25, 300);
   }  // method draw

}  // class Zoom

//-------------------------------------------------------------------
//
//  Class Zoom_Listener is the event listener for the scrollbar
//  in the Zoom applet.
//
//  Constructors:
//
//     public Zoom_Listener (Zoom zoom_applet)
//
//  Methods:
//
//     public void adjustmentValueChanged (AdjustmentEvent event)
//
//-------------------------------------------------------------------

class Zoom_Listener implements AdjustmentListener {

   private Zoom applet;

   //===========================================================
   //  Sets up the listener object by setting a reference to
   //  the applet.
   //===========================================================
   public Zoom_Listener (Zoom zoom_applet) {
      applet = zoom_applet;
   }  // constructor Zoom_Listener

   //===========================================================
   //  Changes the image size in response to a scrollbar
   //  adjustment event.
   //===========================================================
   public void adjustmentValueChanged (AdjustmentEvent event) {
      applet.zoom_image (event.getValue());
   }  // method adjustmentValueChanged

}  // class Zoom_Listner


