⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hier.java

📁 java编程的一些Applets例子。值得深入研究一下。
💻 JAVA
字号:
/* HISTORY
 * Attempt at hierarchy representation with expand/contract display
 * 12/22 - split NodeMem out to NodeMem.java
 * 12/23, 24 - adding goodies
 * 12/25 - split HierCanvas out
 * 01/02/96 - Try using BufferedInputStream() for speed
 Note: format for tags is very restrictive because we are parsing them
 with a StreamTokenizer - see the run() routine where we set the
   "whitespaceChars()" - you have to change these to use different separators
   and "wordChars()"
 * 01/05 - still trying to figure out why it doesn't show first time 
 *        in Netscape
 * 01/12 - GIF display working!
 * 01/15 - modify handleEvent() to pass Ctrl down events so TextArea
 *         will see Ctrl-C and copy selected text
 * 01/16 - use return super.handleEvent( evt )  in handleEvent
 *       this is supposedly the best general approach.
 * 01/18 - mods to AltDisplay.paint() to put image down and inset some
 *        for some reason, image at 0,0 gets clipped by label.  Unclear
 *        why AltDisplay gets clipped and TextArea doesn't.
 * 02/04 - remove label, add update() to reduce flashing
*/

/*  Public Notice
 *  Hier.java, HierCanvas.java and NodeMem.java are copyright (c) 1996
 *  by William B. Brogden - wbrogden@bga.com
 *  130 Woodland Trail, Leander, TX  78641
 *  These classes can be used for private or commercial applications
 *  without restriction, but I would appreciate it if you let me know
 *  what you are using them for & if you make improvements.
 */

import java.applet.Applet;
import java.awt.*;
import java.io.* ;
import java.net.URL;
import java.util.Vector ;

// alternate to TextArea display on right - show graphics
class AltDisplay extends Canvas {
  Image  tmpImg ;
  int state ;

 public AltDisplay( int wide, int hi ) { 
   tmpImg = null ;
   resize(wide,hi) ;
   state = 0 ;
 }

   // assumes that rPanel now has AltDisplay rDisp
 void ShowGif( Image newI ) {
  //  System.out.println("ShowGif start "  ) ;
    if( newI == null ) { System.out.println("null to ShowGif");
    }
    else {
     tmpImg = newI ; state = 0 ;
     repaint();
    }
 }

 public void setSize( int wide, int hi ) { resize( wide, hi ) ; }

 public void cleanUp() {
   if( tmpImg == null ) return ;
   tmpImg.flush();
   tmpImg = null ;
 }

 public void update( Graphics g ) {
   if( state == 0 ) super.update( g ) ;
   else {
     paint( g ) ;
     state = 1 ;
   }
 }

 public void paint( Graphics g ) {
   int y ;
   if( tmpImg == null ) return ;
  // System.out.println("AltDisplay paint" + g.getClipRect() );
   g.drawImage( tmpImg ,4,23, this );
 }


} // end AltDisplay


// 
public class Hier extends Applet implements Runnable {
   String hierfile ;
   Thread hier_t = null ; // thread to build everything
   Vector nlist = null ;
   Panel lPanel  ; // left side, canvas and scroll bar
   Panel rPanel  ; // right side,text display
   GridLayout layout ; 
   Scrollbar scB ;
   TextArea  txT ;  // std text area
   AltDisplay  rDisp ; // for painting an image on right panel
   int rType ;     // currently attached to rPanel 1 = Text, 2 = canvas
   int appWidth, appHeight ;
   int state ;
   //
   HierCanvas hc ;
   int     nLines ; // may be specified by applet
   int     nodect ;
   int     depth ; // number of layers in hierarchy

   public void init() {
     //
     setBackground( Color.white );
     hierfile = getParameter("hierfile" );
     if( hierfile == null )
     { hierfile = "breeds.lst" ;   // you could put an error msg here
     }
     else System.out.println("Found name " + hierfile ) ;
     String tmp = getParameter("DEPTH" );
     if( tmp == null ) depth = 4 ; 
     else depth =  ( new Integer( tmp ) ).intValue() ;
     if( depth <= 1 ) depth = 4 ;
     tmp = getParameter("NLINES" );
     if( tmp == null )  nLines = 10 ;
     else nLines = ( new Integer( tmp ) ).intValue();
     tmp = getParameter("HEIGHT" );
     if( tmp == null ){ appHeight = 100 ;
          System.out.println( "Default Height" ) ; }
     else appHeight =  ( new Integer( tmp ) ).intValue() ;    
     tmp = getParameter("WIDTH" );
     if( tmp == null ) appWidth = 400 ; 
     else appWidth =  ( new Integer( tmp ) ).intValue() ;    
     layout = new GridLayout(1,2 ) ;
     setLayout( layout ) ; // for applet
     hc = new HierCanvas( appHeight );
     constructCtrls(); // panels built & added to applet
     }  // end init()

  public void start() {
    state = 0 ;
    if(hier_t == null )
    { hier_t = new Thread( this);
      hier_t.start() ;
    }
   }  // end start()

   // since we implement the "Runnable" interface we need a run routine
   public void run() {
    int ret, nc ;
    InputStream inS = null ; 
    BufferedInputStream BinS = null ;
    NodeMem nm ;
    if( nlist != null )
    { return ;  // ??
    }
    nlist = new Vector(50, 10 ) ;
    nc = 0 ; // count obj added to list
    try { 
      inS = new URL( getDocumentBase(), hierfile).openStream() ;
      BinS = new BufferedInputStream( inS );
      StreamTokenizer st = new StreamTokenizer( BinS ) ;
      st.eolIsSignificant( true ) ;
      st.whitespaceChars( 44, 46 ) ;
      st.whitespaceChars( 58, 59 ) ;
      st.wordChars( 32, 32 );
      st.wordChars( 95, 95 ) ; // underscore
      st.quoteChar( 34 ) ;
      ret = StreamTokenizer.TT_EOL ;
      while( ret != StreamTokenizer.TT_EOF ) {
        nm = new NodeMem( 3, st ) ;
        ret = st.ttype ;
        if(( ret != StreamTokenizer.TT_EOF) && ( nm != null )) {
          nlist.addElement((Object) nm ); nc++ ;
        }
        
      }
      inS.close();  // close the input stream
    } catch(Exception e ) {
      System.out.println( e.toString());
    }
    if( nc > 0 )
    { nodect = nc ;
      System.out.println("List has " + nodect + " items" ) ;
      // we have a vector of data, pass it to canvas
      hc.SetVector( nlist, nc, depth, nLines  );
      scB.setValues( 0,10,0, nodect + 10 ) ; 
      hc.calculateHier() ;  
      resize( appWidth, appHeight );
      hc.repaint();   // first repaint, hc state is 1
      try { hier_t.sleep(50 ) ;
      } catch(Exception e ) {}
      hc.repaint() ;
  
    }
    else
    { System.out.println("No data found");
    }
   } // end run

  
   // build the right & left panels
   void constructCtrls(){
      lPanel = new Panel() ; // for canvas
      lPanel.setLayout( new BorderLayout() );
      scB = new Scrollbar( Scrollbar.VERTICAL,0,10,0, 100 ) ;
      lPanel.add("West", scB ) ;
      lPanel.add("Center", hc ) ;  
      add( lPanel ) ; // to applet - grid layout
      rPanel = new Panel() ;
      rPanel.setLayout( new BorderLayout() ) ;
     // following option could be used to put a label above right panel
     // rPanel.add( "North",new Label( hierfile ) ) ;
      txT = new TextArea("Text Area") ;
      txT.setEditable( false ) ;
      rDisp = new AltDisplay(appWidth / 2, appHeight ) ;

      hc.AddParent( this ) ;  // so canvas can notify us of text chg
      hc.AddScrollbar( scB ) ; // so canvas can make sc track
      rPanel.add("Center", txT ) ;
      rType = 1 ;     // start with text
      add( rPanel ) ;          // to applet
   } // end constructCtrls


   public void TextChange( String newtx ) {
     boolean bgif = newtx.startsWith("GIF=");
     switch( rType ) {
       case 1 :     // rType == 1 means in text mode
        if( bgif ){ 
          //  System.out.println("tx to image transition" );
          Rectangle rPr = rPanel.bounds() ;
          rPanel.remove( txT ) ; 
          // allow for label at top of rPanel
          rDisp.setSize( rPr.width, rPr.height - 23 ) ;
          rPanel.add( "center", rDisp ) ;
          rDisp.show() ;  // ensure visible
          rType = 2 ;
          rDisp.ShowGif( getImage( getCodeBase(),newtx.substring( 4 ))) ;
        }
        else txT.setText( newtx ) ;
        break ;
       case 2 :   // rType == 2 means in graphics mode
        if( bgif ) { // System.out.println(" get and display image");
          rDisp.ShowGif( getImage( getCodeBase(),newtx.substring( 4 ))) ;
          rPanel.repaint();
        }
        else { // System.out.println("Type 2 img to txt " + newtx ) ; 
          rDisp.hide();
          rPanel.remove(rDisp ) ; rPanel.add( txT ) ;
          rDisp.cleanUp();
          rType = 1 ;
          txT.setText( newtx ) ;
        }
        break ;
     }
   }

    // handle all keyboard and scroll bar events from the Applet
   public boolean handleEvent( Event evt ) {
     boolean ret = false ;
     switch( evt.id ) {
      case Event.KEY_ACTION : // apparently just control keys
         // enable the following to watch what the events contain
         // System.out.println( "Key action" + evt.toString() ) ;
        switch( evt.key ) {
          case Event.UP :    hc.upSel( 1 ) ; ret = true ; break ;
          case Event.DOWN :  hc.dnSel( 1 ) ; ret = true ; break ;
          case Event.RIGHT : hc.expand()   ; ret = true ; break ;
          case Event.LEFT :  hc.contract() ; ret = true ; break ;
          case  Event.F1 :  // testing
           if( rType == 2 ) {
             // a handy place to put debugging statements  
             System.out.println("rDisp " + rDisp.toString() );
           }
           break ;
        }
        break ;
      case Event.KEY_PRESS  : 
         if(evt.controlDown()) { ret = false ; // let TextArea see ctrl-C
         }
         else {
           hc.toggle(); ret = true ;
         } // normal keypress toggles the current selection
        break ;
      case  Event.GOT_FOCUS :
       hc.setFocus( true ) ; hc.repaint(); // dont set ret = true
       break ;                            // because we want system to see
      case  Event.LOST_FOCUS :            // the focus change
       hc.setFocus( false ) ; hc.repaint(); 
       break ;
      case  Event.SCROLL_LINE_DOWN : hc.dnSel( 1 ) ; ret = true ;
       break ;
      case  Event.SCROLL_PAGE_DOWN : hc.dnSel( 2 ) ; ret = true ;
       break ;
      case  Event.SCROLL_LINE_UP   : hc.upSel( 1 ) ; ret = true ;
       break ;
      case  Event.SCROLL_PAGE_UP   : hc.upSel( 2 ) ; ret = true ;
       break ;
      case  Event.SCROLL_ABSOLUTE  : hc.absSel( scB.getValue() ) ; 
       ret = true ; break ;
 
     }
     if( ret ){ hc.repaint(); return ret ;}
     return super.handleEvent( evt ) ;
   }

   public void update( Graphics g ) {
      if( state == 0 ) super.update( g ) ;
      else {  // avoid extra clear after first one
         paint( g ) ; state = 1 ;
      }
   }

   public void stop() {
   }

} // end class Hier

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -