//******************************************************************************
// fern.java: Applet
//
//******************************************************************************
import java.applet.*;
import java.awt.*;
import java.util.Random;
//==============================================================================
// Main Class for applet fern
//
//==============================================================================
public class fern extends Applet implements Runnable
{
// THREAD SUPPORT:
// m_fern is the Thread object for the applet
//--------------------------------------------------------------------------
private Thread m_fern = null;
// PARAMETER SUPPORT:
// Parameters allow an HTML author to pass information to the applet;
// the HTML author specifies them using the <PARAM> tag within the <APPLET>
// tag. The following variables are used to store the values of the
// parameters.
//--------------------------------------------------------------------------
// Members for applet parameters
// <type> <MemberVar> = <Default Value>
//--------------------------------------------------------------------------
private int m_fps = 10;
// Parameter names. To change a name of a parameter, you need only make
// a single change. Simply modify the value of the parameter string below.
//--------------------------------------------------------------------------
private final String PARAM_fps = "fps";
//random object
private Random m_random = new Random();
private double[] m_xValue;
private double[] m_yValue;
private int m_nSize; //length of arrays
private int m_nMax; //maximum value in arrays
int left = 30;
int w = 300;
int w1 = w+left;
double e1 = 0.5 * w;
double e2 = 0.57 * w;
double e3 = 0.408 * w;
double e4 = 0.1075 * w;
double f1 = 0 * w;
double f2 = -0.036 * w;
double f3 = 0.0893 * w;
double f4 = 0.27 * w;
double x = e1;
double y = 0;
// fern Class Constructor
//--------------------------------------------------------------------------
public fern()
{
// TODO: Add constructor code here
}
// APPLET INFO SUPPORT:
// The getAppletInfo() method returns a string describing the applet's
// author, copyright date, or miscellaneous information.
//--------------------------------------------------------------------------
public String getAppletInfo()
{
return "Name: fern\r\n" +
"Author: Krishnan Eswaran\r\n" +
"Created with Microsoft Visual J++ Version 1.1";
}
// PARAMETER SUPPORT
// The getParameterInfo() method returns an array of strings describing
// the parameters understood by this applet.
//
// fern Parameter Information:
// { "Name", "Type", "Description" },
//--------------------------------------------------------------------------
public String[][] getParameterInfo()
{
String[][] info =
{
{ PARAM_fps, "int", "Frames per sec" },
};
return info;
}
// The init() method is called by the AWT when an applet is first loaded or
// reloaded. Override this method to perform whatever initialization your
// applet needs, such as initializing data structures, loading images or
// fonts, creating frame windows, setting the layout manager, or adding UI
// components.
//--------------------------------------------------------------------------
public void init()
{
// PARAMETER SUPPORT
// The following code retrieves the value of each parameter
// specified with the <PARAM> tag and stores it in a member
// variable.
//----------------------------------------------------------------------
String param;
// fps: Frames per sec
//----------------------------------------------------------------------
param = getParameter(PARAM_fps);
if (param != null)
m_fps = Integer.parseInt(param);
// If you use a ResourceWizard-generated "control creator" class to
// arrange controls in your applet, you may want to call its
// CreateControls() method from within this method. Remove the following
// call to resize() before adding the call to CreateControls();
// CreateControls() does its own resizing.
//----------------------------------------------------------------------
//resize(600, 240);
Dimension dim = size();
int m_nSize = XToIndex(dim.width)*50;
m_xValue = new double[m_nSize];
m_yValue = new double[m_nSize];
m_nMax = 600;
// TODO: Place additional initialization code here
}
// Place additional applet clean up code here. destroy() is called when
// when you applet is terminating and being unloaded.
//-------------------------------------------------------------------------
public void destroy()
{
// TODO: Place applet cleanup code here
}
// fern Paint Handler
//--------------------------------------------------------------------------
public void paint(Graphics g)
{
// TODO: Place applet paint code here
//g.drawString("Running: " + Math.random(), 10, 20);
PaintData(g);
}
synchronized private void PaintData(Graphics g) {
//get window dimensions
Dimension d = size();
int nWidth = d.width;
int nHeight = d.height;
//check to see if array needs resizing
ResizeArray(d.width);
//repaint data
for (int i = 0; i < m_nSize; i++) {
int xpt = ValueToY(nWidth, m_xValue);
int ypt = ValueToY(nHeight, m_yValue);
g.drawLine(xpt-100,ypt-100, xpt-100, ypt-100);
//g.drawLine(xpt,ypt-1, xpt, ypt+1);
}
}
// The start() method is called when the page containing the applet
// first appears on the screen. The AppletWizard's initial implementation
// of this method starts execution of the applet's thread.
//--------------------------------------------------------------------------
public void start()
{
if (m_fern == null)
{
m_fern = new Thread(this);
m_fern.start();
}
// TODO: Place additional applet start code here
}
// The stop() method is called when the page containing the applet is
// no longer on the screen. The AppletWizard's initial implementation of
// this method stops execution of the applet's thread.
//--------------------------------------------------------------------------
public void stop()
{
if (m_fern != null)
{
m_fern.stop();
m_fern = null;
}
// TODO: Place additional applet stop code here
}
// THREAD SUPPORT
// The run() method is called when the applet's thread is started. If
// your applet performs any ongoing activities without waiting for user
// input, the code for implementing that behavior typically goes here. For
// example, for an applet that performs animation, the run() method controls
// the display of images.
//--------------------------------------------------------------------------
public void run()
{
int nSleepTime = 1000 / m_fps;
double rValue;
while (true)
{
try
{
rValue = m_random.nextDouble();
AddCoord(rValue);
//repaint window
repaint();
// TODO: Add additional thread-specific code here
Thread.sleep(nSleepTime);
}
catch (InterruptedException e)
{
// TODO: Place exception-handling code here in case an
// InterruptedException is thrown by Thread.sleep(),
// meaning that another thread has interrupted this one
stop();
}
}
}
//ResizeArray
private void ResizeArray(int nWidth) {
int nNumPts = XToIndex(nWidth);
if (nNumPts == m_nSize) {
return;
}
double[] xNewArray = new double[nNumPts];
double[] yNewArray = new double[nNumPts];
int nNumToCopy = Math.min(nNumPts, m_nSize);
for (int i = 0; i < nNumToCopy; i++) {
xNewArray = m_xValue;
yNewArray = m_yValue;
}
m_xValue = xNewArray;
m_yValue = yNewArray;
m_nSize = nNumPts;
}
//AddCoord
synchronized private void AddCoord (double rValue) {
//move everything over 1 for new data item
for (int i = m_nSize - 1; i > 0; i--) {
m_xValue = m_xValue[i-1];
m_yValue = m_yValue[i-1];
}
//determine coordinates
//add new data item
if (rValue > 0.3) {
m_xValue[0] = 0.781 * x + 0.034 * y + e4-100;
m_yValue[0] = -0.032 * x + 0.739 * y + f4-100;
}
else if (rValue > 0.17) {
m_xValue[0] = 0.17 * x - 0.215 * y + e3-100;
m_yValue[0] = 0.222 * x + 0.176 * y + f3-100;
}
else if (rValue > 0.02) {
m_xValue[0] = -0.139 * x + 0.263 * y + e2-100;
m_yValue[0] = 0.246 * x + 0.224 * y + f2-100;
}
else {
m_xValue[0] = 0 * x + 0 * y + e1-100;
m_yValue[0] = 0 * x + 0.27 * y + f1-100;
}
while (rValue >= m_nMax) {
m_nMax += 50;
}
x = m_xValue[0]+100;
y = m_yValue[0]+100;
}
// TODO: Place additional applet code here
// XTo Index
private static final int m_PIXELS_PER_POINT = 1;
static private int XToIndex(int nWidth) {
return nWidth / m_PIXELS_PER_POINT;
}
//IndexToX
static private int IndexToX (int nWidth, int nIndex) {
return (nWidth - 1) - (m_PIXELS_PER_POINT * nIndex);
}
//ValueToY
private int ValueToY (int nHeight, double rValue) {
return nHeight - ((int) rValue * nHeight) / m_nMax;
}
}
<_< :unsure: