home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-06-06 | 6.4 KB | 258 lines |
- // Stock1 - this applet displays a scrolling graph of
- // data similar to that produced by plotting
- // stock price against time. this version
- // flickers badly when the frame rate is too
- // high.
- import java.applet.*;
- import java.awt.*;
- import java.util.Random;
-
- public class Stock extends Applet implements Runnable
- {
- Thread m_Stock = null;
-
- int m_fps = 10;
-
- final String PARAM_fps = "fps";
-
- // use the random object to create stock movements
- private Random m_r = new Random();
-
- // m_dValue[] - contains the closing price of the stock
- // for day that is visible. [0] is today
- // day, [1] yesterday, etc.
- private double[] m_dValue;
- private int m_nSize; // length of value array
- private int m_nMax; // maximum value in value array
-
- public Stock()
- {
- }
-
- public void finalize()
- {
- }
-
- public String getAppletInfo()
- {
- return "Name: Stock\r\n" +
- "Author: Stephan R. Davis\r\n" +
- "Created for Learn Java Now (c)";
- }
-
- public String[][] getParameterInfo()
- {
- String[][] info =
- {
- { PARAM_fps, "int", "Frame rate" },
- };
- return info;
- }
-
- public void init()
- {
- String param;
-
- param = getParameter(PARAM_fps);
- if (param != null)
- m_fps = Integer.parseInt(param);
-
- // resize(600, 240);
-
- // initialize the m_dValue array to the current
- // width of the window
- Dimension dim = size();
- int m_nSize = XToIndex(dim.width);
- m_dValue = new double[m_nSize];
- m_nMax = 100;
- }
-
- public void destroy()
- {
- }
-
- public void paint(Graphics g)
- {
- // paint the frame
- PaintFrame(g);
-
- // paint the data
- PaintData(g);
- }
-
- private void PaintFrame(Graphics g)
- {
- // get the dimensions of the window
- Dimension d = size();
- int nWidth = d.width;
- int nHeight = d.height;
-
- // put up axis along the left and bottom
- g.drawString(Integer.toString(m_nMax), 5, 10);
- g.drawString("0", 5, d.height - 10);
- g.drawLine(0, nHeight - 1, nWidth, nHeight - 1);
- g.drawLine(0, nHeight - 1, 0, 0);
- int nMark = 50;
- while (nMark < m_nMax)
- {
- int nL = 3;
- if ((nMark % 100) == 0)
- {
- nL = 6;
- }
- int nH = ValueToY(nHeight, nMark);
- g.drawLine(0, nH, nL, nH);
- nMark += 50;
- }
- }
-
- synchronized private void PaintData(Graphics g)
- {
- // get the dimensions of the window
- Dimension d = size();
- int nWidth = d.width;
- int nHeight = d.height;
-
- // check to see if the array needs resizing
- ResizeArray(d.width);
-
- // repaint the data
- for (int i = 0; i < m_nSize; i++)
- {
- int x = IndexToX(nWidth, i);
- int y = ValueToY(nHeight, m_dValue[i]);
- g.drawLine(x - 1, y, x + 1, y);
- g.drawLine(x, y - 1, x, y + 1);
- }
- }
-
- public void start()
- {
- if (m_Stock == null)
- {
- m_Stock = new Thread(this);
- m_Stock.start();
- }
- }
-
- public void stop()
- {
- if (m_Stock != null)
- {
- m_Stock.stop();
- m_Stock = null;
- }
- }
-
- public void run()
- {
- // calculate the proper time to delay
- int nSleepTime = 1000 / m_fps;
-
- // start with the first value stored.
- // if it's zero then this must be
- // the first time into the applet -
- // start with an initial value of 50.
- double dValue = m_dValue[0];
- if (dValue == 0.0)
- {
- dValue = 50.0;
- }
-
- while (true)
- {
- try
- {
- // create a new stock value
- // this is the part that's random
- // (here I assume that movement of plus
- // or minus two points a day or so is
- // reasonable - the 0.2 offset gives it
- // a slight upward drift)
- dValue += 2 * m_r.nextGaussian() + 0.2;
- AddValue(dValue);
-
- // now repaint the window
- repaint();
-
- // repaint at the fps speed
- Thread.sleep(nSleepTime);
- }
- catch (InterruptedException e)
- {
- stop();
- }
- }
- }
-
- // ResizeArray - resize the array of stock prices
- // (if necessary)
- private void ResizeArray(int nWidth)
- {
- // if the window is the same size...
- int nNumPts = XToIndex(nWidth);
- if (nNumPts == m_nSize)
- {
- // ...then ignore it
- return;
- }
-
- // otherwise, we need to resize the array:
- // allocate room
- double[] dNewArray = new double[nNumPts];
-
- // now copy the points from the old, smaller
- // array to the new, larger array
- int nNumToCopy = Math.min(nNumPts, m_nSize);
- for (int i = 0; i < nNumToCopy; i++)
- {
- dNewArray[i] = m_dValue[i];
- }
-
- // finally, make the new array our own
- m_dValue = dNewArray;
- m_nSize = nNumPts;
- }
-
- // AddValue - add a value to the array
- synchronized private void AddValue(double dValue)
- {
- // move everything over one to make
- // room for the new data item
- for (int i = m_nSize - 1; i > 0; i--)
- {
- m_dValue[i] = m_dValue[i - 1];
- }
-
- // add the new data item as the
- // the first entry
- m_dValue[0] = dValue;
-
- // make sure to keep track of the max
- while (dValue >= m_nMax)
- {
- m_nMax += 50;
- }
- }
-
- // XToIndex - convert the display window dimension
- // to the corresponding array index
- private static final int m_PIXELS_PER_POINT = 3;
- static private int XToIndex(int nWidth)
- {
- return nWidth / m_PIXELS_PER_POINT;
- }
- // IndexToX - convert the array index to the window
- // x offset
- static private int IndexToX(int nWidth, int nIndex)
- {
- return (nWidth - 1) - (m_PIXELS_PER_POINT * nIndex);
- }
- // ValueToY - convert the stock value into the y
- // offset in the window to plot the point
- private int ValueToY(int nHeight, double dValue)
- {
- return nHeight - ((int)dValue * nHeight) / m_nMax;
- }
- }
-