
package morgan;


import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;


public class MorganGreenClass extends JComponent implements ChangeListener
{
protected GeneralPath gridLinePath;
protected GeneralPath polyLinePath;
protected double xPos = 0.0;
protected double yPos = 0.0;
protected int size = 10;
protected double factor = 1.0;

public MorganGreenClass( )
	{
	setDoubleBuffered( true );
	}

public void paint( Graphics g )
	{
	update( g );
	}

public void update( Graphics g )
	{
	Rectangle visrect = getVisibleRect();
	g.setColor( Color.white );
	g.fillRect( visrect.x , visrect.y , visrect.width , visrect.height );
	drawGraph( g );
	}

public boolean isOpaque( )
	{ return( true ); }


protected void drawGraph(Graphics g)
	{
	Graphics2D g2d = (Graphics2D)g;
	int i;
	int j;
	AffineTransform at = new AffineTransform();
	g2d.translate(xPos, yPos);
	at.setToScale(factor, factor);
	g2d.transform(at);
	g2d.translate(-xPos, -yPos);

	// Draw Grid
	g2d.setColor(Color.red);
	g2d.draw( gridLinePath );

	g2d.setColor(Color.blue);

	// Draw Line
	g2d.draw( polyLinePath );
	}


public void stateChanged( ChangeEvent e )
	{
	JSlider slide = (JSlider)( e.getSource() );
	int val = slide.getValue();
	factor = val / 10.0;
	repaint();
	}


public void initVals( )
	{
	int count;
	GeneralPath MyPath = new GeneralPath();
	polyLinePath = MyPath;
	MyPath.reset();
	
	for( count = 0 ; count < 1000 ; count++ )
		{
		int index = count;
		double xval1 = ( count ) / 10.0;
		double yval1 = 100.0 * ( Math.cos( ( count ) / 100.0 ) + 1.0 );
		double xval2 = ( count + 1 ) / 10.0;
		double yval2 = 100.0 * ( Math.cos( ( count+ 1 ) / 100.0 ) + 1.0 );
		if( count == 0 )
			MyPath.moveTo( (float) ( xval1 ) , (float) ( 19 * size + 1 - yval1 ) );
			else MyPath.lineTo( (float) ( xval2 ) , (float) ( 19 * size + 1 - yval2 ) );
		}

	for( count = 0 ; count < 1000 ; count++ )
		{
		int index = count + 1000;
		double xval1 = ( count ) / 10.0;
		double yval1 = 100.0 * ( Math.sin( ( count ) / 100.0 ) + 1.0 );
		double xval2 = ( count + 1 ) / 10.0;
		double yval2 = 100.0 * ( Math.sin( ( count + 1 ) / 100.0 ) + 1.0 );
		if( count == 0 )
			MyPath.moveTo( (float) ( xval1 ) , (float) ( 19 * size + 1 - yval1 ) );
			else MyPath.lineTo( (float) ( xval2 ) , (float) ( 19 * size + 1 - yval2 ) );
		}


	MyPath = new GeneralPath();
	gridLinePath = MyPath;
	int i;

	for(i=0; i<20; ++i)
		{
		MyPath.moveTo( i * size , 0 );
		MyPath.lineTo( i * size , ( 19 * size ) );
		}

	for(i=0; i<20; ++i)
		{
		MyPath.moveTo( 0 , i * size );
		MyPath.lineTo( ( 19 * size ) , i * size );
		}

	}


public static void main( String[] in )
	{
	JFrame fr = new JFrame();
	MorganGreenClass morg = new MorganGreenClass();
	morg.initVals();
	morg.setMinimumSize( new Dimension( 400 , 400 ) );
	morg.setPreferredSize( new Dimension( 400 , 400 ) );
	JSlider slide = new JSlider( 10 , 100 , 10 );
	slide.addChangeListener( morg );
	Container pane = fr.getContentPane();
	pane.setLayout( new BorderLayout( 0 , 0 ) );
	pane.add( "Center" , morg );
	pane.add( "South" , slide );
	fr.pack();
	fr.show();
	}


} /* MorganGreenClass*/



