home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / Chap15 / Stock / Stock.class (.txt) next >
Encoding:
Java Class File  |  1996-07-29  |  4.3 KB  |  157 lines

  1. import java.applet.Applet;
  2. import java.awt.Component;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.util.Random;
  6.  
  7. public class Stock extends Applet implements Runnable {
  8.    Thread m_Stock;
  9.    int m_fps = 10;
  10.    final String PARAM_fps = "fps";
  11.    private Random m_r = new Random();
  12.    private double[] m_dValue;
  13.    private int m_nSize;
  14.    private int m_nMax;
  15.    private static final int m_PIXELS_PER_POINT = 3;
  16.  
  17.    public void start() {
  18.       if (this.m_Stock == null) {
  19.          this.m_Stock = new Thread(this);
  20.          this.m_Stock.start();
  21.       }
  22.  
  23.    }
  24.  
  25.    private static int IndexToX(int nWidth, int nIndex) {
  26.       return nWidth - 1 - 3 * nIndex;
  27.    }
  28.  
  29.    public String[][] getParameterInfo() {
  30.       String[][] info = new String[][]{{"fps", "int", "Frame rate"}};
  31.       return info;
  32.    }
  33.  
  34.    public void stop() {
  35.       if (this.m_Stock != null) {
  36.          this.m_Stock.stop();
  37.          this.m_Stock = null;
  38.       }
  39.  
  40.    }
  41.  
  42.    private void PaintFrame(Graphics g) {
  43.       Dimension d = ((Component)this).size();
  44.       int nWidth = d.width;
  45.       int nHeight = d.height;
  46.       g.drawString(Integer.toString(this.m_nMax), 5, 10);
  47.       g.drawString("0", 5, d.height - 10);
  48.       g.drawLine(0, nHeight - 1, nWidth, nHeight - 1);
  49.       g.drawLine(0, nHeight - 1, 0, 0);
  50.  
  51.       for(int nMark = 50; nMark < this.m_nMax; nMark += 50) {
  52.          int nL = 3;
  53.          if (nMark % 100 == 0) {
  54.             nL = 6;
  55.          }
  56.  
  57.          int nH = this.ValueToY(nHeight, (double)nMark);
  58.          g.drawLine(0, nH, nL, nH);
  59.       }
  60.  
  61.    }
  62.  
  63.    private void ResizeArray(int nWidth) {
  64.       int nNumPts = XToIndex(nWidth);
  65.       if (nNumPts != this.m_nSize) {
  66.          double[] dNewArray = new double[nNumPts];
  67.          int nNumToCopy = Math.min(nNumPts, this.m_nSize);
  68.  
  69.          for(int i = 0; i < nNumToCopy; ++i) {
  70.             dNewArray[i] = this.m_dValue[i];
  71.          }
  72.  
  73.          this.m_dValue = dNewArray;
  74.          this.m_nSize = nNumPts;
  75.       }
  76.    }
  77.  
  78.    private synchronized void PaintData(Graphics g) {
  79.       Dimension d = ((Component)this).size();
  80.       int nWidth = d.width;
  81.       int nHeight = d.height;
  82.       this.ResizeArray(d.width);
  83.  
  84.       for(int i = 0; i < this.m_nSize; ++i) {
  85.          int x = IndexToX(nWidth, i);
  86.          int y = this.ValueToY(nHeight, this.m_dValue[i]);
  87.          g.drawLine(x - 1, y, x + 1, y);
  88.          g.drawLine(x, y - 1, x, y + 1);
  89.       }
  90.  
  91.    }
  92.  
  93.    public void finalize() {
  94.    }
  95.  
  96.    private synchronized void AddValue(double dValue) {
  97.       for(int i = this.m_nSize - 1; i > 0; --i) {
  98.          this.m_dValue[i] = this.m_dValue[i - 1];
  99.       }
  100.  
  101.       for(this.m_dValue[0] = dValue; dValue >= (double)this.m_nMax; this.m_nMax += 50) {
  102.       }
  103.  
  104.    }
  105.  
  106.    public String getAppletInfo() {
  107.       return "Name: Stock\r\n" + "Author: Stephan R. Davis\r\n" + "Created for Learn Java Now (c)";
  108.    }
  109.  
  110.    private static int XToIndex(int nWidth) {
  111.       return nWidth / 3;
  112.    }
  113.  
  114.    private int ValueToY(int nHeight, double dValue) {
  115.       return nHeight - (int)dValue * nHeight / this.m_nMax;
  116.    }
  117.  
  118.    public void run() {
  119.       int nSleepTime = 1000 / this.m_fps;
  120.       double dValue = this.m_dValue[0];
  121.       if (dValue == (double)0.0F) {
  122.          dValue = (double)50.0F;
  123.       }
  124.  
  125.       while(true) {
  126.          try {
  127.             dValue += (double)2.0F * this.m_r.nextGaussian() + 0.2;
  128.             this.AddValue(dValue);
  129.             ((Component)this).repaint();
  130.             Thread.sleep((long)nSleepTime);
  131.          } catch (InterruptedException var6) {
  132.             this.stop();
  133.          }
  134.       }
  135.    }
  136.  
  137.    public void destroy() {
  138.    }
  139.  
  140.    public void init() {
  141.       String param = ((Applet)this).getParameter("fps");
  142.       if (param != null) {
  143.          this.m_fps = Integer.parseInt(param);
  144.       }
  145.  
  146.       Dimension dim = ((Component)this).size();
  147.       int m_nSize = XToIndex(dim.width);
  148.       this.m_dValue = new double[m_nSize];
  149.       this.m_nMax = 100;
  150.    }
  151.  
  152.    public void paint(Graphics g) {
  153.       this.PaintFrame(g);
  154.       this.PaintData(g);
  155.    }
  156. }
  157.