home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / components / example / ImageScroller.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  5.6 KB  |  179 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.applet.*;
  19. import java.net.URL;
  20.  
  21. class ScrollableCanvas extends Canvas {
  22.     Image image;
  23.     int tx = 0;
  24.     int ty = 0;
  25.     Dimension preferredSize;
  26.  
  27.     ScrollableCanvas(Image img, Dimension prefSize) {
  28.         image = img;
  29.         preferredSize = prefSize;
  30.     }
  31.  
  32.     public Dimension minimumSize() {
  33.         return new Dimension(10, 10);
  34.     }
  35.  
  36.     public Dimension preferredSize() {
  37.         return preferredSize;
  38.     }
  39.  
  40.     public void paint(Graphics g) {
  41.         g.translate(-tx, -ty);
  42.         g.drawImage(image, 0, 0, getBackground(), this);
  43.     }
  44. }
  45.  
  46. public class ImageScroller extends Applet {
  47.     Scrollbar vert;
  48.     Scrollbar horz;
  49.     ScrollableCanvas canvas;
  50.     boolean inAnApplet = true;
  51.     String imageFile = "../images/people.gif";
  52.     Dimension imageSize = new Dimension(600, 320);
  53.     Dimension preferredImageSize = new Dimension(300, 100);
  54.  
  55.     //This method assumes this Applet is visible.
  56.     public void init() {
  57.         Image img;
  58.  
  59.         if (inAnApplet) {
  60.             img = getImage(getCodeBase(), imageFile);
  61.         } else {
  62.             img = Toolkit.getDefaultToolkit().getImage(imageFile);
  63.         }
  64.         canvas = new ScrollableCanvas(img, preferredImageSize);
  65.  
  66.         //Create horizontal scrollbar.
  67.         horz = new Scrollbar(Scrollbar.HORIZONTAL);
  68.  
  69.         //Create vertical scrollbar.
  70.         vert = new Scrollbar(Scrollbar.VERTICAL);
  71.  
  72.         //Add Components to the Applet.
  73.         setLayout(new BorderLayout());
  74.         add("Center", canvas);
  75.         add("East", vert);
  76.         add("South", horz);
  77.  
  78.         validate();
  79.  
  80.         //Now that we've validated, then assuming this Applet is
  81.         //visible, the canvas size is valid and we can adjust the
  82.         //scrollbars to match the image area. [CHECK]
  83.         resizeHorz();
  84.         resizeVert();
  85.     }
  86.  
  87.     public boolean handleEvent(Event evt) {
  88.         switch (evt.id) {
  89.           case Event.SCROLL_LINE_UP:
  90.           case Event.SCROLL_LINE_DOWN:
  91.           case Event.SCROLL_PAGE_UP:
  92.           case Event.SCROLL_PAGE_DOWN:
  93.           case Event.SCROLL_ABSOLUTE:
  94.             if (evt.target == vert) {
  95.                 canvas.ty = ((Integer)evt.arg).intValue();
  96.                 canvas.repaint();
  97.             }
  98.             if (evt.target == horz) {
  99.                 canvas.tx = ((Integer)evt.arg).intValue();
  100.                 canvas.repaint();
  101.             }
  102.         }
  103.         return super.handleEvent(evt);
  104.     }
  105.  
  106.     //Don't call this until the canvas size is valid.
  107.     void resizeHorz() {
  108.         int canvasWidth = canvas.size().width;
  109.  
  110.         if (canvasWidth <= 0) {
  111.             System.out.println("Canvas has no width; can't resize scrollbar");
  112.             return;
  113.         }
  114.  
  115.         //Shift everything to the right if we're displaying empty space
  116.         //on the right side.
  117.         if ((canvas.tx + canvasWidth) > imageSize.width) {
  118.             int newtx = imageSize.width - canvasWidth;
  119.             if (newtx < 0) {
  120.                 newtx = 0;
  121.             }
  122.             canvas.tx = newtx;
  123.         }
  124.  
  125.         horz.setValues(//draw the part of the image that starts at this x:
  126.                        canvas.tx, 
  127.                        //amount to scroll for a "page":
  128.                        (int)(canvasWidth * 0.9), 
  129.                        //minimum image x to specify:
  130.                        0,
  131.                        //maximum image x to specify:
  132.                        imageSize.width - canvasWidth);
  133.         //"visible" arg to setValues() has no effect after scrollbar is visible.
  134.         horz.setPageIncrement((int)(canvasWidth * 0.9));
  135.         return;
  136.     }
  137.  
  138.     //Don't call this until the canvas size is valid.
  139.     void resizeVert() {
  140.         int canvasHeight = canvas.size().height;
  141.  
  142.         if (canvasHeight <= 0) {
  143.             System.out.println("Canvas has no height; can't resize scrollbar");
  144.             return;
  145.         }
  146.  
  147.         //Shift everything downward if we're displaying empty space
  148.         //on the bottom.
  149.         if ((canvas.ty + canvasHeight) > imageSize.height) {
  150.             int newty = imageSize.height - canvasHeight;
  151.             if (newty < 0) {
  152.                 newty = 0;
  153.             }
  154.             canvas.ty = newty;
  155.         }
  156.  
  157.         vert.setValues(//initially draw part of image starting at this y:
  158.                        canvas.ty, 
  159.                        //visible arg--amount to scroll for a "page":
  160.                        (int)(canvasHeight * 0.9), 
  161.                        //minimum image y to specify:
  162.                        0,
  163.                        //maximum image y to specify:
  164.                        imageSize.height - canvasHeight);
  165.  
  166.         //"visible" arg to setValues() has no effect after scrollbar is visible.
  167.         vert.setPageIncrement((int)(canvasHeight * 0.9));
  168.         return;
  169.     }
  170.  
  171.     public void paint(Graphics g) {
  172.         //This method probably was called due to applet being resized.
  173.         resizeHorz();
  174.         resizeVert();
  175.  
  176.         return;
  177.     }
  178. }
  179.