home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Java / Java.zip / rdb084r2.zip / ScrollPanel.java < prev    next >
Text File  |  1997-02-10  |  5KB  |  205 lines

  1. /*
  2.  * @(#)ScrollPanel.java        0.1 96/01/09  Jesse Hammons
  3.  *
  4.  * Copyright (c) 1996 Jesse Hammons
  5.  *
  6.  *   This program is distributed in the hope that it will be useful,
  7.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9.  *   
  10.  * Contact author for use policy on software and documentation
  11.  */
  12. // package edu.princeton.jhammons.awt;
  13.  
  14.  
  15. import java.awt.Panel;
  16. import java.awt.GridBagLayout;
  17. import java.awt.GridBagConstraints;
  18. import java.awt.Dimension;
  19. import java.awt.Scrollbar;
  20. import java.awt.Image;
  21. import java.awt.Graphics;
  22. import java.awt.Component;
  23. import java.awt.Event;
  24. /**
  25.   * ScrollPanel is a generic Panel with scrollbars.  If a client Component is 
  26.   * assigned to a ScrollPanel, The ScrollPanel will set up the scrollbars so 
  27.   * that the Component can be scrolled around if the size of the client is 
  28.   * larger than the inside of the ScrollPanel. 
  29.   *
  30.   * @version 0.1, 09 Jan 1996
  31.   * @author Jesse Hammons
  32.   */
  33. public class ScrollPanel extends Panel {
  34.   RepaintScrollbar horiz, vert;
  35.   Component client;
  36.   int prevx=0, prevy=0;
  37.   boolean first = true;
  38.   GridBagLayout g;
  39.   GridBagConstraints c;
  40.   NoPaintPanel clientPanel;
  41.   Image osimg = null;
  42.   Image biggest = null;
  43.   Dimension bigSize;
  44.   Graphics off;
  45.   Dimension imSize;
  46.   
  47.   /**
  48.     * Creates a ScrollPanel with no client Component.
  49.     */
  50.   public ScrollPanel() {
  51.     this(null);
  52.   }
  53.   
  54.   /**
  55.     * Creates a ScrollPanel with the specified client Componenent.
  56.     * @param client the client Component
  57.     */
  58.   public ScrollPanel(Component client) {
  59.     super();
  60.     this.client = client;
  61.     int gap = 0;
  62.     horiz = new RepaintScrollbar(this, Scrollbar.HORIZONTAL);
  63.     vert = new RepaintScrollbar(this, Scrollbar.VERTICAL);
  64.     clientPanel = new NoPaintPanel();
  65.     clientPanel.setLayout(null);
  66.  
  67.     setLayout(new java.awt.BorderLayout());
  68.     add("West", vert);
  69.     add("South", horiz);
  70.     add("Center", clientPanel);
  71.     setClient(client);
  72.   }
  73.   
  74.   /**
  75.     * Sets the client of this ScrollPanel to client
  76.     * @param client the client Component to be scrolled
  77.     */
  78.   public void setClient(Component client) {
  79.     this.client = client;
  80.     if (client != null) {
  81.       clientPanel.add("Center",  client);
  82.     }
  83.   }
  84.   
  85.   /*
  86.    * returns the client of this ScrollPanel
  87.    * @return Component the current client of this ScrollPanel
  88.    */
  89.   public Component getClient() {
  90.     return client;
  91.   }
  92.   /**
  93.     * Paints the ScrollPanel the the Graphics context g.
  94.     * @param g the Graphics context to paint to
  95.     */
  96.   public void paint(Graphics g) {
  97.     Dimension s = size();
  98.     Dimension d;
  99.     int x = horiz.getValue();
  100.     int y = vert.getValue();
  101.     int dx = x - prevx;
  102.     int dy = y - prevy;
  103.     if (client != null) {
  104.       client.move(-x, -y);
  105.       d = client.size();
  106.       if (osimg == null || imSize.width < d.width || 
  107.       imSize.height < d.height) {
  108.     if (biggest != null && bigSize.width >= d.width && 
  109.         bigSize.height >= d.height) {
  110.       osimg = biggest;
  111.       imSize = d;
  112.     } else {
  113.       try {
  114.         osimg = createImage(d.width, d.height);
  115.         off = osimg.getGraphics();
  116.         imSize = d;
  117.         biggest = osimg;
  118.         bigSize = d;
  119.       } catch(Exception e) { dbg(e.toString()); return; }
  120.     }
  121.       }
  122.       if (osimg != null) {
  123.     int vw = vert.size().width;
  124.     int hh = horiz.size().height;
  125.     off.clipRect(vw, 0, size().width-vw, size().height-hh);
  126.     clientPanel.paint(off);
  127.     g.copyArea(prevx, prevy, s.width-dx, s.height-dy, dx, dy);
  128.     g.clipRect(-x, -y, dx, dy);
  129.     g.drawImage(osimg, -x, -y, this);
  130.       }
  131.     }
  132.     prevx = x;
  133.     prevy = y;
  134.   }// paint()
  135.   /**
  136.     * Updates the ScrollPanel using the Graphics context g.
  137.     * equivalent to paint(g).
  138.     * @param g the Graphics context to use
  139.     * @see #paint 
  140.     */
  141.   public void update(Graphics g) {
  142.     paint(g);
  143.   }
  144.  
  145.   private void dbg(String s) {
  146.     System.out.println(s);
  147.   }
  148.   
  149.   /**
  150.     * Reshapes the ScrollPanel to the specified bounding box, and resets
  151.     * the Scrollbars to the appropriate maximum values, page sizes, etc.
  152.     * @param x    the X coordinate 
  153.     * @param y    the Y coordinate
  154.     * @param w  the width of the bounding box
  155.     * @param h    the height of the bounding box
  156.     */
  157.   public void reshape(int x, int y, int w, int h) {
  158.     super.reshape(x, y, w, h);
  159.     Dimension s = size();
  160.     Dimension cs;
  161.     int vpage, hpage;
  162.     if (client != null) {
  163.       cs = client.size();
  164.       
  165.       vpage = s.height;
  166.       hpage = s.width;
  167.       vert.setValues(vert.getValue(), vpage, 0, cs.height-s.height);
  168.       horiz.setValues(horiz.getValue(), hpage, 0, cs.width-s.width);
  169.     }
  170.   } //reshape()
  171. } // ScrollPanel    
  172. class RepaintScrollbar extends Scrollbar {
  173.   Component callBack = null;
  174.   
  175.   public RepaintScrollbar(Component cb, int orientation) {
  176.     super(orientation);
  177.     callBack = cb;
  178.   }
  179.   public RepaintScrollbar(int orientation) {
  180.     super(orientation);
  181.   }
  182.   public void setCallBack(Component cb) {
  183.     callBack = cb;
  184.   }
  185.   
  186.   public Component getCallBack() {
  187.     return callBack;
  188.   }
  189.   public boolean handleEvent(Event e) {
  190.     if (callBack != null)
  191.       callBack.repaint();
  192.     return true;
  193.   }
  194. }
  195.   
  196. class NoPaintPanel extends Panel {
  197.   public NoPaintPanel() {
  198.     super();
  199.   }
  200.   public void paint(Graphics g) {
  201.   }
  202.   public void update(Graphics g) {
  203.   }
  204. }
  205.