home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / native / SieveDemo2 / SieveDemo.java < prev    next >
Encoding:
Java Source  |  1998-03-05  |  2.1 KB  |  85 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.         // System.out.println(mt);
  53.  
  54.         // Scroll if needed.    
  55.         if (my + mhBarMax > mrect.height)
  56.         {            
  57.             g.copyArea(0, mhBarMax, mrect.width, mrect.height-mhBarMax, 0, -mhBarMax);
  58.             my -= mhBarMax;
  59.         }   
  60.  
  61.         DrawBar(g, (int)t);
  62.  
  63.         // Update the position.
  64.         mc++;
  65.         my += mhBarMax;
  66.  
  67.         // Average on status bar.
  68.         showStatus("Average: " + mTTotal/mc);
  69.         
  70.         // Do it again.
  71.         repaint();
  72.     }
  73.  
  74.     void DrawBar(Graphics g, int t)
  75.     {
  76.         int w = (int)((mrect.width-mwTextMax)*(1000-t))/1000;
  77.         g.setColor(Color.lightGray);
  78.         g.fillRect(mx, my, mrect.width, mhBarMax);
  79.         g.setColor(Color.black);
  80.         g.drawString(Integer.toString(mc), mx, mhText+my+((mhBarMax-mhText)/2)-1);
  81.         g.setColor(Color.blue);
  82.         g.fillRect(mx+mwTextMax, my+((mhBarMax-mhBar)/2), w, mhBar);
  83.     }
  84. }
  85.