import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/** DemoScrollPane - a program that puts an image in a scroll pane.<p>
*
* Screen snap...<br>
* <center><img src="DemoScrollPane-1.gif"></center><p>
*
* @see <a href="DemoScrollPane.java">source code</a>
* @author Scott MacKenzie, 2002
*/
public class DemoScrollPane
{
   public static void main(String[] args)
   {
      // use Win32 look and feel
      try {
         UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
      } catch (Exception e) {}

      DemoScrollPaneFrame frame = new DemoScrollPaneFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("DemoScrollPane");
      frame.pack(); // determine required size of window
      frame.show();
   }
}

class DemoScrollPaneFrame extends JFrame 
{
   JLabel img;
   JScrollPane scrollPane;

   // constructor

   public DemoScrollPaneFrame()
   {
      final ImageIcon IMAGE = new ImageIcon("map.gif");

      // -------------------------------
      // create and configure components
      // -------------------------------

      // instantiate the image object (it's just a JLabel!)
      img = new JLabel();

      // for in-class demo...
      //System.out.println("trace1 : " + img.getPreferredSize());

      // put the image in the JLabel
      img.setIcon(IMAGE);

      // for in-class demo...
      //System.out.println("trace2 : " + img.getPreferredSize());

      // instantiate the scroll pane
      scrollPane = new JScrollPane();

      // for in-class demo...
      //System.out.println("trace3 : " + scrollPane.getPreferredSize());

      // make the image the scroll pane's client
      scrollPane.setViewportView(img);

      // for in-class demo...
      //System.out.println("trace4 : " + scrollPane.getPreferredSize());
      
      // make the scroll pane smaller than the image (therefore,
      // scrollbars are necessary)
      scrollPane.setPreferredSize(new Dimension(200, 200));

      // for in-class demo...
      //System.out.println("trace5 : " + scrollPane.getPreferredSize());

      // Note: The following allows the scroll pane to have focus!
      scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, new JButton());

      scrollPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      scrollPane.setBackground(new Color(222, 222, 222));

      // ------------------
      // arrange components
      // ------------------

      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());
      contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      contentPane.setBackground(new Color(222, 222, 222));
      contentPane.add(scrollPane, "Center");
      this.setContentPane(contentPane);
   }
}

