home *** CD-ROM | disk | FTP | other *** search
/ Internet Gallery / INTERGAL.bin / intergal / prgs / idv21 / data.z / MyCanvas.java < prev    next >
Text File  |  1996-04-23  |  2KB  |  78 lines

  1. import java.awt.*;
  2.  
  3. class MyCanvas extends Canvas implements Runnable {
  4.     Thread thread = null;
  5.     int Refresh   = 50;
  6.     
  7.     protected Image img     = null;   
  8.     protected Graphics grps = null;  
  9.  
  10.     Class    c  = null;                        
  11.     MyWidget mw = null; 
  12.     
  13.     MyCanvas( String classname ) {
  14.         try{
  15.             c = Class.forName( classname );
  16.         }    
  17.         catch ( ClassNotFoundException e) {
  18.             System.err.println("Sorry - but i cannot find a class named \"" + classname + "\"");
  19.             System.exit(-1);            
  20.         }
  21.         
  22.         try {
  23.             mw = (MyWidget) c.newInstance();
  24.         }
  25.         catch ( Exception e) {
  26.             System.err.println("Creating an instance of \"" + classname + "\" have failed.");
  27.             System.exit(-1);            
  28.         }
  29.     }    
  30.  
  31.     public String getWidgetName() {
  32.         return mw.getClass().getName();
  33.     }
  34.     
  35. // start/stop animation
  36.     
  37.     public void start()    {
  38.         if(thread==null) {                            
  39.             thread = new Thread( this, "");     // thread starten, falls noch nicht getan
  40.             thread.start();
  41.     }    }
  42.  
  43.     public void stop() { thread = null; }
  44.     
  45.     public void run() {
  46.         while( thread != null ) 
  47.         {
  48.             repaint();
  49.  
  50.             try { thread.sleep(Refresh); }
  51.             catch( InterruptedException e ) {}
  52.     }    }
  53.     
  54.     public void paint(Graphics g) {
  55.         if(img!=null) {
  56.             g.drawImage( img, 0, 0, this);
  57.     }    }
  58.     
  59.     public void update(Graphics g) {
  60.         if( grps==null )
  61.         {
  62.             img = createImage( MyWidget.LEN, MyWidget.LEN );
  63.             grps = img.getGraphics();
  64.         }
  65.     
  66.         mw.repaint( grps );
  67.         
  68.         g.drawImage( img, 0, 0, this);
  69.     }
  70.     
  71.     public Dimension minimumSize() {    return new Dimension( MyWidget.LEN, MyWidget.LEN ); }
  72.     
  73.     public Dimension preferredSize() { return minimumSize(); } 
  74. }
  75.  
  76.  
  77.  
  78.