home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML in Action / Dynamicke-HTML-v-akci-covermount.bin / XML / PARSER / XMLINST.EXE / viewer / BufferedCanvas.java < prev    next >
Encoding:
Java Source  |  1997-10-31  |  9.7 KB  |  321 lines

  1. //******************************************************************************
  2. // BufferedCanvas.java:    
  3. //
  4. //******************************************************************************
  5. import java.awt.*;
  6.  
  7. abstract class BufferedCanvas extends Canvas
  8. {
  9.     BufferedCanvas() {
  10.         String version = System.getProperty("java.version");
  11.         jdk11 = version.equals("1.1") ? true : false;
  12.  
  13.         String[] nms = Toolkit.getDefaultToolkit().getFontList();            
  14.         fnt = new Font( nms[0], Font.PLAIN, 12   );
  15.         fm  = Toolkit.getDefaultToolkit().getFontMetrics( fnt );
  16.         LINE_SIZE = fm.getHeight() + SPACING;
  17.  
  18.         // Does the canvas not contain an integer number of lines?
  19.         int modSize = size().height % LINE_SIZE;
  20.         if( modSize != (BUFFER*2) )
  21.         {
  22.             resize( size().width, size().height - modSize + (BUFFER*2));
  23.         }
  24.     }
  25.  
  26.     public void paint( Graphics g )
  27.     {
  28.         update(g);
  29.     }
  30.  
  31.     // In order to get scrolling & buffering, this class should
  32.     // be overwritten and not paint().
  33.     abstract public void paintBuffer( Graphics g );
  34.     
  35.     public void invalidateBuffer()
  36.     {
  37.         redraw = true;
  38.     }
  39.  
  40.     public void reorient()
  41.     {
  42.         tx = 0;
  43.         ty = 0;
  44.         redraw = true;
  45.     }
  46.  
  47.     public void newGraphics()
  48.     {
  49.         Dimension d = size();
  50.         offDimension = d;        
  51.  
  52.         for( int i = 0; i <= 1; i ++ )
  53.         {
  54.             offImage[i] = createImage(d.width, d.height);
  55.             if (offImage[i] == null)
  56.                 return; // must be too early.
  57.             offGraphics[i] = offImage[i].getGraphics();
  58.                
  59.             offGraphics[i].setColor(getBackground());       
  60.             offGraphics[i].fillRect(0, 0, d.width, d.height);        
  61.             offGraphics[i].setColor(Color.black);    
  62.  
  63.             offGraphics[i].clipRect(BUFFER,CLIPBUFFER,d.width-BUFFER*2,d.height-CLIPBUFFER*2);
  64.  
  65.         }
  66.         curr = 0;
  67.         bad = 1;
  68.     }
  69.  
  70.     public void checkGraphics()
  71.     {
  72.         Dimension d = size();
  73.  
  74.         // Have we resized the image?
  75.         if( d.width != offDimension.width || d.height != offDimension.height )
  76.         {
  77.             newGraphics();
  78.             redraw = true;
  79.         }
  80.  
  81.         // Does the canvas not contain an integer number of lines (with buffer padding)?
  82.         int modSize = d.height % LINE_SIZE;
  83.         if( modSize != (BUFFER*2) )
  84.         {
  85.             int newHeight = size().height - modSize + (BUFFER*2);
  86.             resize( d.width, newHeight);
  87.         }
  88.  
  89.     }
  90.  
  91.     public void resize( Dimension d )
  92.     {
  93.         resize(d.width,d.height);
  94.     }
  95.  
  96.     public void resize( int w, int h )
  97.     {
  98.         Dimension d1 = size();
  99.         if (d1.width != w || d1.height != h)
  100.             super.resize( w, h );
  101.     }
  102.  
  103.     public void checkEmptySpace()
  104.     {
  105.         // Is there empty space at the bottom of the screen?
  106.         if( ( ty + getCanvasHeight() ) > displayHeight )         
  107.         {
  108.             int newty = displayHeight - getCanvasHeight();
  109.             if( newty < 0 )             
  110.             {         
  111.                 newty = 0;
  112.             }             
  113.             setVert( newty );
  114.         }
  115.     }
  116.  
  117.     public void update( Graphics g ) 
  118.     {
  119.         checkGraphics();
  120.  
  121.         if( redraw )
  122.         {
  123.             offGraphics[curr].translate( -tx + BUFFER, -ty + BUFFER );
  124.             offGraphics[curr].setFont( fnt );
  125.             paintBuffer(offGraphics[curr]);
  126.             offGraphics[curr].translate( tx - BUFFER, ty - BUFFER);
  127.  
  128.             redraw = false;
  129.  
  130.             checkEmptySpace();
  131.         }
  132.  
  133.         g.setColor(Color.black);
  134.         g.drawRect( 0,0,size().width-1, size().height-1);
  135.         g.drawRect( 1,1,size().width-3, size().height-3);
  136.         g.clipRect(BUFFER,CLIPBUFFER,size().width-BUFFER*2,size().height-CLIPBUFFER*2);       
  137.         g.drawImage(offImage[curr], BUFFER, BUFFER, this);
  138.  
  139.     }    
  140.     public int getHorz() {
  141.         return tx;
  142.     }
  143.     public int getHorzLines() {
  144.         return tx / LINE_SIZE;
  145.     }
  146.     public void zeroHorz() {
  147.         setHorz( 0 );
  148.     }
  149.     // setHorz deals with abstract line scrolling
  150.     public void scrollHorz( int scrollx ) {
  151.         int pixelx = scrollx * LINE_SIZE;
  152.         setHorz( tx + pixelx );
  153.     }
  154.     // setHorz deals with absolutes
  155.     protected void setHorz( int newtx ) {
  156.         if( tx != newtx ) {
  157.             if( newtx + getCanvasWidth() > displayWidth ) 
  158.                 newtx = displayWidth - getCanvasWidth();
  159.             if( newtx < 0 )
  160.                 newtx = 0;            
  161.             
  162.             redraw = true;
  163.             tx = newtx;    
  164.             repaint();
  165.         }
  166.     }
  167.  
  168.     public int getVert() {
  169.         return ty;
  170.     }   
  171.     public int getVertLines() {
  172.         return ty / LINE_SIZE;
  173.     }
  174.     public void zeroVert() {
  175.         setVert( 0 );
  176.     }
  177.     public void setTopLine( int topline ) {
  178.         int newy = YPOS(topline);
  179.         setVert( newy );
  180.     }
  181.     public void setBottomLine( int botline ) {
  182.         int newy = YPOS(botline) - (getCanvasHeight() - LINE_SIZE);
  183.         setVert( newy );
  184.     }
  185.     // scrollVert deals with abstract line scrolling
  186.     public void scrollVert( int scrolly ) {
  187.         int pixely = scrolly * LINE_SIZE;
  188.         setVert( ty + pixely );
  189.     }
  190.     // setVert deals with absolutes
  191.     private void setVert( int newty ) 
  192.     {
  193.         if( ty != newty ) 
  194.         {
  195.             if( newty + getCanvasHeight() > displayHeight ) 
  196.                 newty = displayHeight - getCanvasHeight();
  197.             if( newty < 0 )
  198.                 newty = 0;
  199.  
  200.             newty = newty - ( newty % LINE_SIZE );
  201.  
  202.             int diff = newty - ty;
  203.             ty = newty;  
  204.  
  205.             if( diff < 0  )  // Scrolled up
  206.             {                                    
  207.                 diff *= -1;
  208.  
  209.                 if( diff > getCanvasHeight() / 4 ) 
  210.                 {                 
  211.                     redraw = true;
  212.                 }                    
  213.                 else
  214.                 {
  215.                     Graphics tGraphics;
  216.  
  217.                     offGraphics[bad].drawImage( offImage[curr],
  218.                                                  0, diff, this );
  219.                     
  220.                     tGraphics = offGraphics[bad].create();
  221.                     tGraphics.clipRect( BUFFER, CLIPBUFFER, getCanvasWidth(), diff+(BUFFER-CLIPBUFFER) );
  222.                     tGraphics.setColor( getBackground() );
  223.                     tGraphics.fillRect( BUFFER, CLIPBUFFER, getCanvasWidth(), diff+(BUFFER-CLIPBUFFER) );
  224.                     tGraphics.setColor( Color.black );
  225.  
  226.                     tGraphics.translate( -tx + BUFFER, -ty + BUFFER);
  227.                     tGraphics.setFont( fnt );
  228.                       paintBuffer(tGraphics);
  229.  
  230.                     bad = ( bad == 1 ) ? 0 : 1;
  231.                     curr = ( curr == 1 ) ? 0 : 1;
  232.                 }
  233.             }             
  234.             else // Scrolled down
  235.             {    
  236.                 if( diff > getCanvasHeight() / 4 ) 
  237.                 {                        
  238.                     redraw = true;
  239.                 }                    
  240.                 else
  241.                 {
  242.                     Graphics tGraphics;
  243.  
  244.                     offGraphics[bad].drawImage( offImage[curr],
  245.                                                  0, -diff, this );
  246.                     
  247.                     tGraphics = offGraphics[bad].create();
  248.                     tGraphics.clipRect( BUFFER, getCanvasHeight()-diff-LINE_SIZE, 
  249.                                         getCanvasWidth(), diff + (LINE_SIZE * 2) );
  250.                     tGraphics.setColor( getBackground() );
  251.                     tGraphics.fillRect( BUFFER, getCanvasHeight()-diff-LINE_SIZE, 
  252.                                         getCanvasWidth(), diff + (LINE_SIZE * 2) );
  253.                     tGraphics.setColor( Color.black );
  254.  
  255.                     tGraphics.translate( -tx + BUFFER, -ty + BUFFER);
  256.                     tGraphics.setFont( fnt );
  257.                     paintBuffer(tGraphics);
  258.  
  259.                     bad = ( bad == 1 ) ? 0 : 1;
  260.                     curr = ( curr == 1 ) ? 0 : 1;
  261.                 }
  262.             }             
  263.             repaint();
  264.         }
  265.     }        
  266.     
  267.  
  268.     public int getDisplayHeightLines() {
  269.         return displayHeight / LINE_SIZE; 
  270.     }
  271.     public int getDisplayWidthLines() {
  272.         return displayWidth / LINE_SIZE;
  273.     }
  274.     public int getCanvasHeight() {
  275.         return size().height - (BUFFER*2);
  276.     }
  277.     public int getCanvasWidth() {
  278.         return size().width - (BUFFER*2);
  279.     }
  280.     public int getCanvasHeightLines() {
  281.         return ( size().height - (BUFFER*2) ) / LINE_SIZE;
  282.     }
  283.     public int getCanvasWidthLines() {
  284.         return size().width / LINE_SIZE;
  285.     }
  286.  
  287.     public int YPOS( int y ) {
  288.         return( y * LINE_SIZE );
  289.     }
  290.  
  291.     private int tx = 0;
  292.     private int ty = 0;
  293.     
  294.     int displayWidth = 0;
  295.     int displayHeight = 0;
  296.  
  297.     boolean redraw = true;
  298.  
  299.     Dimension offDimension = new Dimension(0,0);
  300.     Image[] offImage = new Image[2];
  301.     Graphics[] offGraphics = new Graphics[2];
  302.     int curr = 0;
  303.     int bad = 1;
  304.  
  305.     public final int DISPLAY_SIZE_CHANGE = 10000;
  306.  
  307.     public final int CLIPBUFFER   = 2;
  308.     public final int BUFFER       = 5;
  309.  
  310.     public final int INDENT       = 5;
  311.     public final int LEVELINDENT  = 10;
  312.     public final int SPACING      = 3;
  313.     public final int OVAL_SIZE    = 10;
  314.     public final int RECT_SIZE    = 8;
  315.     public final int FOLDER_SIZE  = 15;
  316.     public       int LINE_SIZE    = 15;
  317.     public FontMetrics  fm; 
  318.     private Font fnt;
  319.  
  320.     boolean jdk11;
  321. }