home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-10-31 | 9.7 KB | 321 lines |
- //******************************************************************************
- // BufferedCanvas.java:
- //
- //******************************************************************************
- import java.awt.*;
-
- abstract class BufferedCanvas extends Canvas
- {
- BufferedCanvas() {
- String version = System.getProperty("java.version");
- jdk11 = version.equals("1.1") ? true : false;
-
- String[] nms = Toolkit.getDefaultToolkit().getFontList();
- fnt = new Font( nms[0], Font.PLAIN, 12 );
- fm = Toolkit.getDefaultToolkit().getFontMetrics( fnt );
- LINE_SIZE = fm.getHeight() + SPACING;
-
- // Does the canvas not contain an integer number of lines?
- int modSize = size().height % LINE_SIZE;
- if( modSize != (BUFFER*2) )
- {
- resize( size().width, size().height - modSize + (BUFFER*2));
- }
- }
-
- public void paint( Graphics g )
- {
- update(g);
- }
-
- // In order to get scrolling & buffering, this class should
- // be overwritten and not paint().
- abstract public void paintBuffer( Graphics g );
-
- public void invalidateBuffer()
- {
- redraw = true;
- }
-
- public void reorient()
- {
- tx = 0;
- ty = 0;
- redraw = true;
- }
-
- public void newGraphics()
- {
- Dimension d = size();
- offDimension = d;
-
- for( int i = 0; i <= 1; i ++ )
- {
- offImage[i] = createImage(d.width, d.height);
- if (offImage[i] == null)
- return; // must be too early.
- offGraphics[i] = offImage[i].getGraphics();
-
- offGraphics[i].setColor(getBackground());
- offGraphics[i].fillRect(0, 0, d.width, d.height);
- offGraphics[i].setColor(Color.black);
-
- offGraphics[i].clipRect(BUFFER,CLIPBUFFER,d.width-BUFFER*2,d.height-CLIPBUFFER*2);
-
- }
- curr = 0;
- bad = 1;
- }
-
- public void checkGraphics()
- {
- Dimension d = size();
-
- // Have we resized the image?
- if( d.width != offDimension.width || d.height != offDimension.height )
- {
- newGraphics();
- redraw = true;
- }
-
- // Does the canvas not contain an integer number of lines (with buffer padding)?
- int modSize = d.height % LINE_SIZE;
- if( modSize != (BUFFER*2) )
- {
- int newHeight = size().height - modSize + (BUFFER*2);
- resize( d.width, newHeight);
- }
-
- }
-
- public void resize( Dimension d )
- {
- resize(d.width,d.height);
- }
-
- public void resize( int w, int h )
- {
- Dimension d1 = size();
- if (d1.width != w || d1.height != h)
- super.resize( w, h );
- }
-
- public void checkEmptySpace()
- {
- // Is there empty space at the bottom of the screen?
- if( ( ty + getCanvasHeight() ) > displayHeight )
- {
- int newty = displayHeight - getCanvasHeight();
- if( newty < 0 )
- {
- newty = 0;
- }
- setVert( newty );
- }
- }
-
- public void update( Graphics g )
- {
- checkGraphics();
-
- if( redraw )
- {
- offGraphics[curr].translate( -tx + BUFFER, -ty + BUFFER );
- offGraphics[curr].setFont( fnt );
- paintBuffer(offGraphics[curr]);
- offGraphics[curr].translate( tx - BUFFER, ty - BUFFER);
-
- redraw = false;
-
- checkEmptySpace();
- }
-
- g.setColor(Color.black);
- g.drawRect( 0,0,size().width-1, size().height-1);
- g.drawRect( 1,1,size().width-3, size().height-3);
- g.clipRect(BUFFER,CLIPBUFFER,size().width-BUFFER*2,size().height-CLIPBUFFER*2);
- g.drawImage(offImage[curr], BUFFER, BUFFER, this);
-
- }
- public int getHorz() {
- return tx;
- }
- public int getHorzLines() {
- return tx / LINE_SIZE;
- }
- public void zeroHorz() {
- setHorz( 0 );
- }
- // setHorz deals with abstract line scrolling
- public void scrollHorz( int scrollx ) {
- int pixelx = scrollx * LINE_SIZE;
- setHorz( tx + pixelx );
- }
- // setHorz deals with absolutes
- protected void setHorz( int newtx ) {
- if( tx != newtx ) {
- if( newtx + getCanvasWidth() > displayWidth )
- newtx = displayWidth - getCanvasWidth();
- if( newtx < 0 )
- newtx = 0;
-
- redraw = true;
- tx = newtx;
- repaint();
- }
- }
-
- public int getVert() {
- return ty;
- }
- public int getVertLines() {
- return ty / LINE_SIZE;
- }
- public void zeroVert() {
- setVert( 0 );
- }
- public void setTopLine( int topline ) {
- int newy = YPOS(topline);
- setVert( newy );
- }
- public void setBottomLine( int botline ) {
- int newy = YPOS(botline) - (getCanvasHeight() - LINE_SIZE);
- setVert( newy );
- }
- // scrollVert deals with abstract line scrolling
- public void scrollVert( int scrolly ) {
- int pixely = scrolly * LINE_SIZE;
- setVert( ty + pixely );
- }
- // setVert deals with absolutes
- private void setVert( int newty )
- {
- if( ty != newty )
- {
- if( newty + getCanvasHeight() > displayHeight )
- newty = displayHeight - getCanvasHeight();
- if( newty < 0 )
- newty = 0;
-
- newty = newty - ( newty % LINE_SIZE );
-
- int diff = newty - ty;
- ty = newty;
-
- if( diff < 0 ) // Scrolled up
- {
- diff *= -1;
-
- if( diff > getCanvasHeight() / 4 )
- {
- redraw = true;
- }
- else
- {
- Graphics tGraphics;
-
- offGraphics[bad].drawImage( offImage[curr],
- 0, diff, this );
-
- tGraphics = offGraphics[bad].create();
- tGraphics.clipRect( BUFFER, CLIPBUFFER, getCanvasWidth(), diff+(BUFFER-CLIPBUFFER) );
- tGraphics.setColor( getBackground() );
- tGraphics.fillRect( BUFFER, CLIPBUFFER, getCanvasWidth(), diff+(BUFFER-CLIPBUFFER) );
- tGraphics.setColor( Color.black );
-
- tGraphics.translate( -tx + BUFFER, -ty + BUFFER);
- tGraphics.setFont( fnt );
- paintBuffer(tGraphics);
-
- bad = ( bad == 1 ) ? 0 : 1;
- curr = ( curr == 1 ) ? 0 : 1;
- }
- }
- else // Scrolled down
- {
- if( diff > getCanvasHeight() / 4 )
- {
- redraw = true;
- }
- else
- {
- Graphics tGraphics;
-
- offGraphics[bad].drawImage( offImage[curr],
- 0, -diff, this );
-
- tGraphics = offGraphics[bad].create();
- tGraphics.clipRect( BUFFER, getCanvasHeight()-diff-LINE_SIZE,
- getCanvasWidth(), diff + (LINE_SIZE * 2) );
- tGraphics.setColor( getBackground() );
- tGraphics.fillRect( BUFFER, getCanvasHeight()-diff-LINE_SIZE,
- getCanvasWidth(), diff + (LINE_SIZE * 2) );
- tGraphics.setColor( Color.black );
-
- tGraphics.translate( -tx + BUFFER, -ty + BUFFER);
- tGraphics.setFont( fnt );
- paintBuffer(tGraphics);
-
- bad = ( bad == 1 ) ? 0 : 1;
- curr = ( curr == 1 ) ? 0 : 1;
- }
- }
- repaint();
- }
- }
-
-
- public int getDisplayHeightLines() {
- return displayHeight / LINE_SIZE;
- }
- public int getDisplayWidthLines() {
- return displayWidth / LINE_SIZE;
- }
- public int getCanvasHeight() {
- return size().height - (BUFFER*2);
- }
- public int getCanvasWidth() {
- return size().width - (BUFFER*2);
- }
- public int getCanvasHeightLines() {
- return ( size().height - (BUFFER*2) ) / LINE_SIZE;
- }
- public int getCanvasWidthLines() {
- return size().width / LINE_SIZE;
- }
-
- public int YPOS( int y ) {
- return( y * LINE_SIZE );
- }
-
- private int tx = 0;
- private int ty = 0;
-
- int displayWidth = 0;
- int displayHeight = 0;
-
- boolean redraw = true;
-
- Dimension offDimension = new Dimension(0,0);
- Image[] offImage = new Image[2];
- Graphics[] offGraphics = new Graphics[2];
- int curr = 0;
- int bad = 1;
-
- public final int DISPLAY_SIZE_CHANGE = 10000;
-
- public final int CLIPBUFFER = 2;
- public final int BUFFER = 5;
-
- public final int INDENT = 5;
- public final int LEVELINDENT = 10;
- public final int SPACING = 3;
- public final int OVAL_SIZE = 10;
- public final int RECT_SIZE = 8;
- public final int FOLDER_SIZE = 15;
- public int LINE_SIZE = 15;
- public FontMetrics fm;
- private Font fnt;
-
- boolean jdk11;
- }