home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / ui / tree / scrollPanel.java < prev    next >
Text File  |  1997-01-02  |  5KB  |  242 lines

  1. package ui.tree;
  2.  
  3. import java.awt.*;
  4. import ui.tree.*;
  5.  
  6. public class scrollPanel extends Panel
  7. {
  8.  
  9.   protected treeCanvas  tCanvas;
  10.   protected Scrollbar   vertBar ;
  11.   protected Scrollbar   horzBar ;
  12.  
  13.   protected int         imgWidth ;
  14.   protected int         imgHeight ;
  15.  
  16.   protected int         imgX ;
  17.   protected int         imgY ;
  18.  
  19.   protected int         page ;
  20.  
  21.   public scrollPanel (TreeTool tool)
  22.   {
  23.  
  24.         setBackground(Color.white);
  25.  
  26.         imgX = 0 ;
  27.         imgY = 0 ;
  28.  
  29.         vertBar = new Scrollbar( Scrollbar.VERTICAL ) ;
  30.         horzBar = new Scrollbar( Scrollbar.HORIZONTAL ) ;
  31.  
  32.         tCanvas = new treeCanvas(tool) ;
  33.         tCanvas.startObserving();
  34.         while ((imgHeight = tCanvas.getImageHeight()) == -1 )
  35.         {
  36.             // loop until image loaded
  37.         }
  38.  
  39.         while ((imgWidth  = tCanvas.getImageWidth()) == -1 )
  40.         {
  41.             // loop until image loaded
  42.         }
  43.         //System.out.println("image height = " + imgHeight + " Image width = " + imgWidth);
  44.         GridBagLayout gridbag = new GridBagLayout();
  45.  
  46.         setLayout( gridbag ) ;
  47.  
  48.         GridBagConstraints c = new GridBagConstraints();
  49.  
  50.         c.fill       = GridBagConstraints.BOTH ;
  51.         c.weightx    = 1.0;
  52.         c.weighty    = 1.0;
  53.         gridbag.setConstraints(tCanvas, c);
  54.         add( tCanvas ) ;
  55.  
  56.         c.fill       = GridBagConstraints.VERTICAL ;
  57.         c.gridwidth  = GridBagConstraints.REMAINDER ;
  58.         gridbag.setConstraints(vertBar, c);
  59.         add( vertBar ) ;
  60.  
  61.         c.fill       = GridBagConstraints.HORIZONTAL ;
  62.         c.gridwidth  = 1 ;
  63.         gridbag.setConstraints(horzBar, c);
  64.         add( horzBar ) ;
  65.  
  66.     }
  67.  
  68.     // used to clear the screen of all nodes except root node
  69.     public void clearScreen(TreeNode n)
  70.     {
  71.         tCanvas.removeNode(n);
  72.     }
  73.  
  74.     /**
  75.      * Creates a new SelfValidatingTextField.
  76.      *
  77.      * @parm x      new x pos
  78.      * @parm y      new y pos
  79.      * @parm width  new width
  80.      * @parm height new height
  81.      */
  82.     public synchronized void reshape(int x,
  83.                                      int y,
  84.                                      int width,
  85.                                      int height) {
  86.  
  87.         super.reshape( x, y, width, height ) ;
  88.         Dimension d = tCanvas.getImageSize();
  89.         imgWidth = d.width;
  90.         imgHeight = d.height;
  91.         if ( width > imgWidth + vertBar.bounds().width ) {
  92.  
  93.             horzBar.disable() ;
  94.  
  95.         } else {
  96.  
  97.             horzBar.enable() ;
  98.  
  99.             Rectangle bndRect = bounds() ;
  100.  
  101.             int barWidth = vertBar.preferredSize().width ;
  102.  
  103.             int max = imgWidth - (bndRect.width - barWidth);
  104.             page = max/10 ;
  105.  
  106.             int oldMax = horzBar.getMaximum() ;
  107.  
  108.             if ( oldMax == 0) {
  109.  
  110.                 imgX = 0 ;
  111.  
  112.             } else {
  113.  
  114.                 imgX = (int)(((float)imgX/(float)oldMax) * (float)max) ;
  115.             }
  116.  
  117.             horzBar.setValues( imgX, page, 0, max ) ;
  118.             horzBar.setPageIncrement( page ) ;
  119.  
  120.         }
  121.  
  122.         if (height > imgHeight + horzBar.bounds().height) {
  123.  
  124.             vertBar.disable() ;
  125.  
  126.         } else {
  127.  
  128.             vertBar.enable() ;
  129.  
  130.             Rectangle bndRect = bounds() ;
  131.  
  132.             int barHeight = horzBar.preferredSize().height ;
  133.  
  134.             int max = imgHeight - (bndRect.height -    barHeight) ;
  135.             page = max/10 ;
  136.  
  137.             int oldMax = vertBar.getMaximum() ;
  138.  
  139.             if ( oldMax == 0) {
  140.  
  141.                 imgY = 0 ;
  142.  
  143.             } else {
  144.  
  145.                 imgY = (int)(((float)imgY/(float)oldMax) * (float)max) ;
  146.             }
  147.  
  148.             vertBar.setValues( imgY, page, 0, max ) ;
  149.             vertBar.setPageIncrement( page ) ;
  150.  
  151.         }
  152.     }
  153.  
  154.     /**
  155.      * Over rise Compnent.handleEvent
  156.      *
  157.      * @parm e  Event to be handled
  158.      */
  159.     public boolean handleEvent(Event e) {
  160.  
  161.         if ( e.target == horzBar ) {
  162.  
  163.             switch( e.id ) {
  164.                 case Event.SCROLL_PAGE_UP:
  165.                 case Event.SCROLL_LINE_UP:
  166.  
  167.                 case Event.SCROLL_ABSOLUTE:
  168.  
  169.                 case Event.SCROLL_LINE_DOWN:
  170.                 case Event.SCROLL_PAGE_DOWN:
  171.  
  172.                 imgX = horzBar.getValue() ;
  173.  
  174.                 tCanvas.repaint();
  175.  
  176.                 return true ;
  177.  
  178.             }
  179.  
  180.  
  181.         } else if ( e.target == vertBar ) {
  182.  
  183.             switch( e.id ) {
  184.                 case Event.SCROLL_PAGE_UP:
  185.                 case Event.SCROLL_LINE_UP:
  186.  
  187.                 case Event.SCROLL_ABSOLUTE:
  188.  
  189.                 case Event.SCROLL_LINE_DOWN:
  190.                 case Event.SCROLL_PAGE_DOWN:
  191.  
  192.                 imgY = vertBar.getValue() ;
  193.  
  194.                 tCanvas.repaint();
  195.  
  196.                 return true ;
  197.  
  198.             }
  199.  
  200.         }
  201.  
  202.         return super.handleEvent(e) ;
  203.  
  204.     }
  205.  
  206.  
  207. };
  208.  
  209. /**
  210.  * A class that draws an Image
  211.  *
  212.  * @version 1.0 96/04/29
  213.  * @author  David Chung
  214.  */
  215. class ImageCanvas extends Canvas {
  216.  
  217.  
  218.     Image canvasImg ;
  219.  
  220.     /**
  221.      * Create an ImageCanvas object
  222.      *
  223.      * @parm img Image to be displayed
  224.      */
  225.     public ImageCanvas( Image img ) {
  226.         canvasImg = img ;
  227.     }
  228.  
  229.     /**
  230.      * Paint the Canvas
  231.      *
  232.      * @parm g Graphics context
  233.      */
  234.     public void paint(Graphics g) {
  235.  
  236.         g.drawImage( canvasImg,
  237.             -1 * ((scrollPanel)getParent()).imgX,
  238.             -1 * ((scrollPanel)getParent()).imgY,
  239.             this ) ;
  240.  
  241.     }
  242. }