home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-10-31 | 5.6 KB | 201 lines |
- //******************************************************************************
- // Scroller.java:
- //
- //******************************************************************************
- import java.awt.*;
- import BufferedCanvas;
-
-
- class Scroller extends Panel
- {
- Scrollbar vert;
- Scrollbar horz;
- BufferedCanvas canvas;
-
- final int HORZ = 0;
- final int VERT = 1;
-
- public Scroller(BufferedCanvas canvas)
- {
- this.canvas = canvas;
-
- //Create horizontal scrollbar.
- horz = new Scrollbar(Scrollbar.HORIZONTAL);
-
- //Create vertical scrollbar.
- vert = new Scrollbar(Scrollbar.VERTICAL);
-
- //Add Components to the Applet.
- setLayout(new BorderLayout());
- add("Center", canvas);
- add("East", vert);
- add("South", horz);
-
- validate();
-
- //Now that we've validated, then assuming this Applet is
- //visible, the canvas size is valid and we can adjust the
- //scrollbars to match the image area. [CHECK]
- resizeHorizontal();
- resizeVertical();
- }
-
- private void quickScroll( Object target, int scrollAmt )
- {
- if( target == vert )
- {
- canvas.scrollVert( scrollAmt );
- resizeVertical();
- }
- else if( target == horz )
- {
- canvas.scrollHorz( scrollAmt );
- resizeHorizontal();
- }
- }
-
- private void quickScroll( int dir, int scrollAmt )
- {
- if( dir == VERT ) // vert
- {
- canvas.scrollVert( scrollAmt );
- resizeVertical();
- }
- if( dir == HORZ ) // horz
- {
- canvas.scrollHorz( scrollAmt );
- resizeHorizontal();
- }
- }
-
- public boolean handleEvent( Event evt )
- {
- switch( evt.id )
- {
- case Event.SCROLL_LINE_UP:
- case Event.SCROLL_LINE_DOWN:
- case Event.SCROLL_ABSOLUTE:
- {
- int newPos = ( ( Integer ) evt.arg ).intValue();
- if( evt.target == vert )
- {
- canvas.scrollVert( newPos - canvas.getVertLines() );
- resizeVertical();
- }
- else if( evt.target == horz )
- {
- canvas.scrollHorz( newPos - canvas.getHorzLines() );
- resizeHorizontal();
- }
- return true;
- }
- case Event.SCROLL_PAGE_UP:
- case Event.SCROLL_PAGE_DOWN:
- {
- if( evt.target == vert )
- {
- resizeVertical();
- }
- else if( evt.target == horz )
- {
- resizeHorizontal();
- }
- return true;
- }
- case canvas.DISPLAY_SIZE_CHANGE:
- {
- update();
- return true;
- }
- }
-
- return super.handleEvent( evt );
- }
-
- public boolean keyDown( Event evt, int key )
- {
- switch( evt.key )
- {
- case Event.UP:
- quickScroll( VERT, -1 );
- break;
- case Event.PGUP:
- quickScroll( VERT, -1 * canvas.getCanvasHeightLines() );
- break;
- case Event.DOWN:
- quickScroll( VERT, 1 );
- break;
- case Event.PGDN:
- quickScroll( VERT, canvas.getCanvasHeightLines() );
- break;
- case Event.RIGHT:
- quickScroll( HORZ, 1 );
- break;
- case Event.LEFT:
- quickScroll( HORZ, -1 );
- break;
- }
- return false;
- }
-
- void resizeHorizontal()
- {
- int canvasWidthLines = canvas.getCanvasWidthLines();
- int displayWidthLines = canvas.getDisplayWidthLines();
-
- if( canvasWidthLines <= 0 ) {
- return;
- }
-
- horz.setValues( canvas.getHorzLines(),
- (int)(canvasWidthLines * 0.9),
- 0,
- displayWidthLines-1 );
-
- horz.setPageIncrement( ( int ) ( canvasWidthLines * 0.9 ));
- return;
- }
-
- void resizeVertical()
- {
- int canvasHeightLines = canvas.getCanvasHeightLines();
- int displayHeightLines = canvas.getDisplayHeightLines();
-
- // To keep things simpler, the vertical scrollbar will always
- // appear, regardless of need.
-
- if( canvasHeightLines <= 0 || canvasHeightLines > displayHeightLines) {
- vert.setValues( 0,
- 1,
- 0,
- 1);
- }
-
- vert.setValues( canvas.getVertLines(),
- canvasHeightLines,
- 0,
- displayHeightLines-1);
-
- vert.setPageIncrement( canvasHeightLines );
- return;
- }
-
- public void update( Graphics g )
- {
- resizeHorizontal();
- resizeVertical();
- }
-
- public void update()
- {
- resizeHorizontal();
- resizeVertical();
- }
-
- public void paint( Graphics g )
- {
- resizeHorizontal();
- resizeVertical();
- }
- }
-