home *** CD-ROM | disk | FTP | other *** search
/ Chip Special: HTML & Java / Chip-Special_1997-01_HTML-a-Java.bin / javasdk / sdk-java.exe / SDKJava.cab / Samples / native / SieveDemo2 / SieveDemo.java < prev   
Encoding:
Java Source  |  1996-10-10  |  2.2 KB  |  82 lines

  1. //----------------------------------------------------------------------------
  2. //
  3. //----------------------------------------------------------------------------
  4. import java.awt.*;
  5. import java.applet.Applet;
  6.  
  7. //----------------------------------------------------------------------------
  8. public class SieveDemo extends Applet
  9. {
  10.     byte[] mabFlags = new byte[32768*2];
  11.     int mhBar = 10;
  12.     int mhGap = 2;
  13.     int mwGap = 2;
  14.     int mx;
  15.     int my;
  16.     int mhBarMax;
  17.     Rectangle mrect;
  18.     int mc;
  19.     int mhText;
  20.     int mwTextMax;
  21.     long mTTotal;
  22.     long mtLast;
  23.     
  24.     public void init()
  25.     {
  26.         mrect = bounds();
  27.         Graphics g = getGraphics();
  28.         if (g != null)
  29.         {
  30.             FontMetrics fm = g.getFontMetrics();
  31.             mhText = fm.getHeight();
  32.             mwTextMax = fm.stringWidth("9999")+mwGap;
  33.             g.dispose();
  34.         }
  35.  
  36.         mhBarMax = Math.max(mhText, mhBar) + mhGap;
  37.         mtLast = System.currentTimeMillis();
  38.         
  39.         repaint();
  40.     }
  41.  
  42.     public void update(Graphics g)
  43.     {
  44.         // Calc bar.
  45.         Sieve.CountPrimes(mabFlags);
  46.         long t = System.currentTimeMillis()-mtLast;
  47.         mtLast = System.currentTimeMillis();
  48.         mTTotal += t;        
  49.         // System.out.println(mt);
  50.  
  51.         // Scroll if needed.    
  52.         if (my + mhBarMax > mrect.height)
  53.         {            
  54.             g.copyArea(0, mhBarMax, mrect.width, mrect.height-mhBarMax, 0, -mhBarMax);
  55.             my -= mhBarMax;
  56.         }   
  57.  
  58.         DrawBar(g, (int)t);
  59.  
  60.         // Update the position.
  61.         mc++;
  62.         my += mhBarMax;
  63.  
  64.         // Average on status bar.
  65.         showStatus("Average: " + mTTotal/mc);
  66.         
  67.         // Do it again.
  68.         repaint();
  69.     }
  70.  
  71.     void DrawBar(Graphics g, int t)
  72.     {
  73.         int w = (int)((mrect.width-mwTextMax)*(1000-t))/1000;
  74.         g.setColor(Color.lightGray);
  75.         g.fillRect(mx, my, mrect.width, mhBarMax);
  76.         g.setColor(Color.black);
  77.         g.drawString(Integer.toString(mc), mx, mhText+my+((mhBarMax-mhText)/2)-1);
  78.         g.setColor(Color.blue);
  79.         g.fillRect(mx+mwTextMax, my+((mhBarMax-mhBar)/2), w, mhBar);
  80.     }
  81. }
  82.