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

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