home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / native / SieveDemo / SieveDemo.java < prev   
Encoding:
Java Source  |  1998-03-05  |  2.6 KB  |  109 lines

  1. //
  2. //  SieveDemo.java
  3. //
  4. //  Copyright (c) 1997-1998, Microsoft Corporation.  All rights reserved.
  5. //
  6.  
  7. import java.awt.*;
  8. import java.applet.Applet;
  9.  
  10. //----------------------------------------------------------------------------
  11. public class SieveDemo extends Applet
  12. {
  13.     byte[] mabFlags = new byte[32768*2];
  14.     int mhBar = 10;
  15.     int mhGap = 2;
  16.     int mwGap = 2;
  17.     int mx;
  18.     int my;
  19.     int mhBarMax;
  20.     Rectangle mrect;
  21.     int mc;
  22.     int mhText;
  23.     int mwTextMax;
  24.     long mTTotal;
  25.     long mtLast;
  26.     
  27.     public void init()
  28.     {
  29.         mrect = bounds();
  30.         Graphics g = getGraphics();
  31.         if (g != null)
  32.         {
  33.             FontMetrics fm = g.getFontMetrics();
  34.             mhText = fm.getHeight();
  35.             mwTextMax = fm.stringWidth("9999")+mwGap;
  36.             g.dispose();
  37.         }
  38.  
  39.         mhBarMax = Math.max(mhText, mhBar) + mhGap;
  40.         mtLast = System.currentTimeMillis();
  41.         
  42.         repaint();
  43.     }
  44.  
  45.     public void update(Graphics g)
  46.     {
  47.         // Calc bar.
  48.         Sieve.CountPrimes(mabFlags);
  49.         long t = System.currentTimeMillis()-mtLast;
  50.         mtLast = System.currentTimeMillis();
  51.         mTTotal += t;        
  52.  
  53.         // Scroll if needed.    
  54.         if (my + mhBarMax > mrect.height)
  55.         {            
  56.             g.copyArea(0, mhBarMax, mrect.width, mrect.height-mhBarMax, 0, -mhBarMax);
  57.             my -= mhBarMax;
  58.         }   
  59.  
  60.         DrawBar(g, (int)t);
  61.  
  62.         // Update the position.
  63.         mc++;
  64.         my += mhBarMax;
  65.  
  66.         // Average on status bar.
  67.         showStatus("Average: " + mTTotal/mc);
  68.         
  69.         // Do it again.
  70.         repaint();
  71.     }
  72.  
  73.     void DrawBar(Graphics g, int t)
  74.     {
  75.         int w = (int)((mrect.width-mwTextMax)*(1000-t))/1000;
  76.         g.setColor(Color.lightGray);
  77.         g.fillRect(mx, my, mrect.width, mhBarMax);
  78.         g.setColor(Color.black);
  79.         g.drawString(Integer.toString(mc), mx, mhText+my+((mhBarMax-mhText)/2)-1);
  80.         g.setColor(Color.blue);
  81.         g.fillRect(mx+mwTextMax, my+((mhBarMax-mhBar)/2), w, mhBar);
  82.     }
  83. }
  84.  
  85. //----------------------------------------------------------------------------
  86. class Sieve
  87. {
  88.     static int CountPrimes(byte[] abFlags)
  89.     {
  90.         int count = 0;
  91.  
  92.         for (int i = 0; i < abFlags.length; i++)
  93.             abFlags[i] = 1;
  94.  
  95.         for (int i = 2; i < abFlags.length; i++)
  96.         {
  97.             if (abFlags[i] != 0)
  98.             {
  99.                 for (int k = i + i; k < abFlags.length; k += i)
  100.                     abFlags[k] = 0;
  101.  
  102.                 count++;
  103.             }
  104.         }
  105.  
  106.         return count;
  107.     }
  108. }
  109.