home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.util.Random;
-
- public class Stock extends Applet implements Runnable {
- Thread m_Stock;
- int m_fps = 10;
- final String PARAM_fps = "fps";
- private Random m_r = new Random();
- private double[] m_dValue;
- private int m_nSize;
- private int m_nMax;
- private int m_nOffset;
- private boolean m_bRepaintAll = true;
- private Dimension m_dimWin = new Dimension(0, 0);
- private static final int m_PIXELS_PER_POINT = 3;
-
- public void start() {
- if (this.m_Stock == null) {
- this.m_Stock = new Thread(this);
- this.m_Stock.start();
- }
-
- }
-
- private static int IndexToX(int nWidth, int nIndex) {
- return nWidth - 1 - 3 * nIndex;
- }
-
- public String[][] getParameterInfo() {
- String[][] info = new String[][]{{"fps", "int", "Frame rate"}};
- return info;
- }
-
- public void stop() {
- if (this.m_Stock != null) {
- this.m_Stock.stop();
- this.m_Stock = null;
- }
-
- }
-
- private void PaintFrame(Graphics g) {
- Dimension d = ((Component)this).size();
- int nWidth = d.width;
- int nHeight = d.height;
- this.ResizeArray(nWidth);
- g.drawString(Integer.toString(this.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);
-
- for(int nMark = 50; nMark < this.m_nMax; nMark += 50) {
- int nL = 3;
- if (nMark % 100 == 0) {
- nL = 6;
- }
-
- int nH = this.ValueToY(nHeight, (double)nMark);
- g.drawLine(0, nH, nL, nH);
- }
-
- }
-
- private void ResizeArray(int nWidth) {
- int nNumPts = XToIndex(nWidth);
- if (nNumPts != this.m_nSize) {
- double[] dNewArray = new double[nNumPts];
- int nNumToCopy = Math.min(nNumPts, this.m_nSize);
-
- for(int i = 0; i < nNumToCopy; ++i) {
- dNewArray[i] = this.m_dValue[i];
- }
-
- this.m_dValue = dNewArray;
- this.m_nSize = nNumPts;
- }
- }
-
- private synchronized void PaintData(Graphics g) {
- Dimension d = ((Component)this).size();
- int nWidth = d.width;
- int nHeight = d.height;
- if (this.m_nOffset != 0) {
- Color bg = ((Component)this).getBackground();
- Color fg = ((Component)this).getForeground();
- int nMax = this.m_nSize - this.m_nOffset - 1;
-
- for(int i = 0; i < nMax; ++i) {
- int x = IndexToX(nWidth, i);
- int y = this.ValueToY(nHeight, this.m_dValue[i + this.m_nOffset]);
- g.setColor(bg);
- g.drawLine(x - 1, y, x + 1, y);
- g.drawLine(x, y - 1, x, y + 1);
- y = this.ValueToY(nHeight, this.m_dValue[i]);
- g.setColor(fg);
- g.drawLine(x - 1, y, x + 1, y);
- g.drawLine(x, y - 1, x, y + 1);
- }
-
- this.m_nOffset = 0;
- }
- }
-
- public void finalize() {
- }
-
- private synchronized void AddValue(double dValue) {
- ++this.m_nOffset;
-
- for(int i = this.m_nSize - 1; i > 0; --i) {
- this.m_dValue[i] = this.m_dValue[i - 1];
- }
-
- for(this.m_dValue[0] = dValue; dValue >= (double)this.m_nMax; this.m_nMax += 50) {
- }
-
- }
-
- public String getAppletInfo() {
- return "Name: Stock\r\n" + "Author: Stephan R. Davis\r\n" + "Created for Learn Java Now (c)";
- }
-
- private static int XToIndex(int nWidth) {
- return nWidth / 3;
- }
-
- private int ValueToY(int nHeight, double dValue) {
- return nHeight - (int)dValue * nHeight / this.m_nMax;
- }
-
- public void run() {
- int nSleepTime = 1000 / this.m_fps;
- double dValue = this.m_dValue[0];
- if (dValue == (double)0.0F) {
- dValue = (double)50.0F;
- }
-
- while(true) {
- try {
- dValue += (double)2.0F * this.m_r.nextGaussian() + 0.2;
- this.AddValue(dValue);
- ((Component)this).repaint();
- Thread.sleep((long)nSleepTime);
- } catch (InterruptedException var6) {
- this.stop();
- }
- }
- }
-
- public void destroy() {
- }
-
- public void init() {
- String param = ((Applet)this).getParameter("fps");
- if (param != null) {
- this.m_fps = Integer.parseInt(param);
- }
-
- Dimension dim = ((Component)this).size();
- int m_nSize = XToIndex(dim.width);
- this.m_dValue = new double[m_nSize];
- this.m_nMax = 100;
- }
-
- public void paint(Graphics g) {
- Dimension dim = ((Component)this).size();
- g.clearRect(0, 0, dim.width, dim.height);
- this.PaintFrame(g);
- this.PaintData(g);
- this.m_bRepaintAll = false;
- }
-
- public void update(Graphics g) {
- Dimension dim = ((Component)this).size();
- if (dim.width != this.m_dimWin.width || dim.height != this.m_dimWin.height) {
- this.m_bRepaintAll = true;
- this.m_dimWin = new Dimension(dim.width, dim.height);
- }
-
- if (this.m_bRepaintAll) {
- this.paint(g);
- } else {
- this.PaintData(g);
- }
-
- }
- }
-