home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / Chap15 / Stock3 / Stock.class (.txt) next >
Encoding:
Java Class File  |  1996-07-29  |  5.2 KB  |  188 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.awt.Image;
  7. import java.awt.image.ImageObserver;
  8. import java.util.Random;
  9.  
  10. public class Stock extends Applet implements Runnable {
  11.    Thread m_Stock;
  12.    int m_fps = 10;
  13.    final String PARAM_fps = "fps";
  14.    private Random m_r = new Random();
  15.    private double[] m_dValue;
  16.    private int m_nSize;
  17.    private int m_nMax;
  18.    private Image m_image;
  19.    private Graphics m_g;
  20.    Dimension m_dimImage;
  21.    private static final int m_PIXELS_PER_POINT = 3;
  22.  
  23.    public void start() {
  24.       if (this.m_Stock == null) {
  25.          this.m_Stock = new Thread(this);
  26.          this.m_Stock.start();
  27.       }
  28.  
  29.    }
  30.  
  31.    private static int IndexToX(int nWidth, int nIndex) {
  32.       return nWidth - 1 - 3 * nIndex;
  33.    }
  34.  
  35.    public String[][] getParameterInfo() {
  36.       String[][] info = new String[][]{{"fps", "int", "Frame rate"}};
  37.       return info;
  38.    }
  39.  
  40.    public void stop() {
  41.       if (this.m_Stock != null) {
  42.          this.m_Stock.stop();
  43.          this.m_Stock = null;
  44.       }
  45.  
  46.    }
  47.  
  48.    private void PaintFrame(Graphics g) {
  49.       Dimension d = ((Component)this).size();
  50.       int nWidth = d.width;
  51.       int nHeight = d.height;
  52.       g.drawString(Integer.toString(this.m_nMax), 5, 10);
  53.       g.drawString("0", 5, d.height - 10);
  54.       g.drawLine(0, nHeight - 1, nWidth, nHeight - 1);
  55.       g.drawLine(0, nHeight - 1, 0, 0);
  56.  
  57.       for(int nMark = 50; nMark < this.m_nMax; nMark += 50) {
  58.          int nL = 3;
  59.          if (nMark % 100 == 0) {
  60.             nL = 6;
  61.          }
  62.  
  63.          int nH = this.ValueToY(nHeight, (double)nMark);
  64.          g.drawLine(0, nH, nL, nH);
  65.       }
  66.  
  67.    }
  68.  
  69.    private void ResizeArray(int nWidth) {
  70.       int nNumPts = XToIndex(nWidth);
  71.       if (nNumPts != this.m_nSize) {
  72.          double[] dNewArray = new double[nNumPts];
  73.          int nNumToCopy = Math.min(nNumPts, this.m_nSize);
  74.  
  75.          for(int i = 0; i < nNumToCopy; ++i) {
  76.             dNewArray[i] = this.m_dValue[i];
  77.          }
  78.  
  79.          this.m_dValue = dNewArray;
  80.          this.m_nSize = nNumPts;
  81.       }
  82.    }
  83.  
  84.    private synchronized void PaintData(Graphics g) {
  85.       Dimension d = ((Component)this).size();
  86.       int nWidth = d.width;
  87.       int nHeight = d.height;
  88.       this.ResizeArray(d.width);
  89.  
  90.       for(int i = 0; i < this.m_nSize; ++i) {
  91.          int x = IndexToX(nWidth, i);
  92.          int y = this.ValueToY(nHeight, this.m_dValue[i]);
  93.          g.drawLine(x - 1, y, x + 1, y);
  94.          g.drawLine(x, y - 1, x, y + 1);
  95.       }
  96.  
  97.    }
  98.  
  99.    private void ResizeImage() {
  100.       Dimension dim = ((Component)this).size();
  101.       int nWidth = dim.width;
  102.       int nHeight = dim.height;
  103.       if (this.m_dimImage == null || this.m_dimImage.width != nWidth || this.m_dimImage.height != nHeight) {
  104.          this.m_dimImage = new Dimension(nWidth, nHeight);
  105.          this.m_image = ((Component)this).createImage(nWidth, nHeight);
  106.          this.m_g = this.m_image.getGraphics();
  107.       }
  108.    }
  109.  
  110.    public void finalize() {
  111.    }
  112.  
  113.    private synchronized void AddValue(double dValue) {
  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.       if (this.m_image != null) {
  171.          g.drawImage(this.m_image, 0, 0, (ImageObserver)null);
  172.       }
  173.  
  174.    }
  175.  
  176.    public void update(Graphics g) {
  177.       this.ResizeImage();
  178.       Color colFG = ((Component)this).getForeground();
  179.       Color colBG = ((Component)this).getBackground();
  180.       this.m_g.setColor(colBG);
  181.       this.m_g.fillRect(0, 0, this.m_dimImage.width, this.m_dimImage.height);
  182.       this.m_g.setColor(colFG);
  183.       this.PaintFrame(this.m_g);
  184.       this.PaintData(this.m_g);
  185.       this.paint(g);
  186.    }
  187. }
  188.