import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Font;
import java.applet.Applet;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class NervousText extends Applet implements Runnable, AdjustmentListener //, MouseListener,
{
    String banner;		// The text to be displayed
    char bannerChars[];		// The same text as an array of characters
    Thread runner = null;	// The thread that is displaying the text
    boolean threadSuspended;	// True when thread suspended (via mouse click)
	int mag=1;
	int maxValue=255;
	int minValue=-255;
	int maxY=255;
	int hz=440;
	final double cvtDegToRad = Math.PI/180;
    public void init() 
    {		
		banner = getParameter("text");
		if (banner == null) banner = "HotJava";
		int bannerLength = banner.length();
		bannerChars =  new char[bannerLength];
        banner.getChars(0, banner.length(), bannerChars, 0);
        threadSuspended = false;
		resize(15*(bannerLength + 1), 50);
		setFont(new Font("TimesRoman", Font.BOLD, 36));
		//addMouseListener(this);
		
		setLayout(new BorderLayout() );
		Scrollbar sb = new Scrollbar(Scrollbar.HORIZONTAL);
		sb.setValue(1);
		sb.addAdjustmentListener(this);
		add(sb, BorderLayout.SOUTH);
    }
	public void adjustmentValueChanged(AdjustmentEvent ae) {
		mag = ae.getValue();
		if(mag==0)mag=1;
		repaint();
	}
	
	public void setMag(int m)
    {
    	mag=m;
    	//repaint();
    }
    public void setMax(int m)
    {
    	maxValue=m;
    	maxY=m;
    	repaint();
    }
    public void setMin(int m)
    {
    	minValue=m;
    	//repaint();
    }
    public void setHz(int m)
    {
    	hz=m;
    	//repaint();
    }
    public void destroy()
    {
        //removeMouseListener(this);
    }

    public void start()
    {
        runner = new Thread(this);
        runner.start();
    }

    public synchronized void stop()
    {
		runner = null;
        if (threadSuspended)
        {
            threadSuspended = false;
            notify();
        }
    }

    public void run()
    {
        Thread me = Thread.currentThread();
        while (runner == me)
        {
            try
            {
                Thread.sleep(100);
                synchronized(this)
                {
                    while (threadSuspended)
                    {
                        wait();
                    }
                }
            }
            catch (InterruptedException e)
            {
            }
            //repaint();
        }
    }

    public void paint(Graphics g)
    {
    	int y, oldX, oldY;
		double radians;
		double degrees;
    	Dimension d = this.getSize();
    	int axis = (int)(d.height/2);
    	
    
    	
    	//int hz=360*10*2;
		//int [] sndBuffer = new int [hz];
		boolean forward=true;
		int inc=1, maxValue2=maxValue, minValue2=minValue, temp=minValue2;
		g.setPaintMode();
		//setBackground(Color.green);
		//for(int ii=0, length = banner.length(); ii<1; ii++)
       	//{
           	//repaint();
           	for (int i=0;i<hz;i++)
			{
				if (forward) temp+=inc;
				if (!forward) temp-=inc;
				if (temp>=maxValue2) forward=false;
				if (temp<=minValue2) forward=true;
				//sndBuffer[i]=temp;
				g.drawOval(i/mag,temp+255,1,1);
				g.setColor(Color.black);
				if(i%3==0)g.setColor(Color.green);
			}
		 
			 
			 oldX = 0;
			 oldY = 0;
			 for (int x=0; x<hz; x++)
			 {
				radians = x*cvtDegToRad;
				y = (int)(maxY*Math.sin(radians) );
				g.drawOval(oldX/mag, oldY+axis, 1, 1);
				oldY = y;
				oldX = x;
				g.setColor(Color.black);
				if(x%2==0)g.setColor(Color.white);
			}
		//}
	}    
	    //setMag(10);i/mag
    	//mag=Integer.parseInt(getParameter("mag"));
    	/*if (Math.random()<.5)sndBuffer[i]=(int)Math.log(temp)*10;
	     g.drawString("Here are a selection of blank shapes.",20,40);
	     g.drawChars(bannerChars, i, 1, x, y);
         g.draw3DRect(10, i, 60, i, true);
	     g.drawString("Here are the filled equivalents.",20,140);
	     g.drawLine(20,140,200,140);
	     g.fillRect(100,150,32,55);
	     g.fillOval(150,146,60,60);
	     g.setColor(Color.darkGray);
	     g.fillArc(230,150,65,50,30,270);*/    

    public synchronized void mousePressed(MouseEvent e)
    {
        e.consume();
        threadSuspended = !threadSuspended;
        if (!threadSuspended) notify();
    }
    public void mouseReleased(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mouseClicked(MouseEvent e){}
	public String getAppletInfo()
    {
    	return "Title: sndWaveForm Demo\nAuthor: RatJed\nNot much yet.";
    }
    public String[][] getParameterInfo()
    {
        String pinfo[][] =
        {
            {"text", "string", "Text to display"},
    	};
        return pinfo;
    }
}//class NervousText
