/*	Trivial applet that illustrates how to use Leigh Brookshaw's Graph Package	to plot random data.*/import java.awt.*;import java.applet.Applet;import java.awt.event.*;import java.io.*;import graph.*;import java.net.URL;public class RunnableApplet extends Applet implements Runnable{	boolean			runningSafe;		// if true, we don't have access to the file system	boolean			running;			// are we currently running?	Thread			relaxer;			// magic used to implement the Runnable interface		// User interface objects		Panel			settings;			// controls go into this Panel	ValueControl	timeStep;			// see ValueControl.java	TextArea		messages;	Checkbox		fixedStep;	Button			starter;		Graph2D 		graph;				// major feature of the GUI	DataSet 		dataSet;	Axis			xaxis, yaxis;	double			data[];	Markers 		markers;	URL 			markersURL;	Graphics		osg = null;			// these are used to implement double-buffering of the graph	Image			osi = null;			// to make updates look smooth	int				iwidth = 0;	int				iheight = 0;	int				updateInterval = 10;	int				count = 0;			// how many times through the iterate function			// methods required to implement the Runnable interface	public void start() {		relaxer = new Thread( this );		relaxer.start();	}		public void stop() {		relaxer.stop();	}	public void run( ) {		while ( true ) {			try {				if ( running )					{ iterate();	}				try { Thread.sleep( 1 ); }				catch (InterruptedException e) { break;	}			}			catch ( Exception ee ) {				System.out.println( "Caught exception " + ee );				break;			}		}	}	public void init() {		setSize(800, 600);		running = false;				// Do we have access to the file system?		try {			File f = new File( System.getProperty( "user.dir" ), "junk" );			runningSafe = false;		}		catch ( Exception e ) {			runningSafe = true;		}						// We will use the most powerful layout manager to arrange items in		// our applet panel. With the GridBagLayout, you add items by passing		// a GridBagConstraints object, which describes where to put each item.				setLayout( new GridBagLayout( ) );		GridBagConstraints gbc = new GridBagConstraints();		// Initialize common fields of the GridBagConstraints		gbc.fill = gbc.BOTH;					// components grow horizontally		gbc.insets = new Insets(2, 2, 2, 2);	// 2-pixel margins on all sides		gbc.gridx = 0;		gbc.gridy = 0;		gbc.gridwidth = 2;		gbc.gridheight = 2;		gbc.weightx = gbc.weighty = 0.0;				// Prepare the graph		graph = new Graph2D();		graph.borderTop = 10;		graph.borderRight = 10;		graph.borderLeft = 10;		graph.square = true;		graph.setSize( 350, 350 );		add( graph, gbc );			// add the graph at the top left								/*		**      Load a file containing Marker definitions		*/        try {           markersURL = new URL(getDocumentBase(), "marker.txt" );           markers = new Markers(markersURL);        } catch(Exception e) {           System.out.println("Failed to create Marker URL!");           System.out.println( "Markers file was supposed to be marker.txt" );        }         graph.setMarkers(markers);						settings = new Panel();		settings.setLayout( new GridBagLayout() );		gbc.gridx = 0;		gbc.gridy = 2;		gbc.gridwidth = 4;		gbc.gridheight = 1;		add( settings, gbc );			// add the panel to the applet				gbc.fill = gbc.NONE;			// components don't grow		gbc.gridx = 0;		gbc.gridy = 1;		gbc.gridwidth = 1;		gbc.gridheight = 1;		gbc.weightx = gbc.weighty = 0.0;		starter = new Button( "Start" );		settings.add( starter, gbc );		starter.setActionCommand( "start" );	// I don't think this is necessary, but...				starter.addActionListener( new ActionListener() {			public void actionPerformed( ActionEvent e ) {				startStop();			// when pressed, call this function			}});		gbc.gridx++;					// move to next position in the grid		fixedStep = new Checkbox( "Fix step size" );		settings.add( fixedStep, gbc );		fixedStep.setState( true );		// make sure the box is checked initially				timeStep = new ValueControl( this, "Time step (y)", 0.0, 1, 0.02 );		timeStep.setAction( new ValueControlAction() {			public void action( ValueControl vc ) {				SetTimeStep( vc.value() );			}} );		gbc.gridx++;		settings.add( timeStep, gbc );		messages = new TextArea( "", 4, 30, TextArea.SCROLLBARS_NONE );		messages.setFont( new Font( "SansSerif", Font.PLAIN, 9 ) );		gbc.gridx++;		gbc.gridy = 0;		gbc.gridheight = 3;		settings.add( messages, gbc );	}		public void paint( Graphics g ) {	}		public void startStop() {		if ( running )	starter.setLabel( "Start" );		else starter.setLabel( "Stop" );				if ( !running )	// we are stopped and need to start up			startUp();	// this will set running to true		else			running = false;	}		public void startUp() {		double [] data = new double[2];		double [] newpoint = new double[2];						double [] initial1 = { 1.0, 1.0, 5.0, 5.0 };				try {			if ( dataSet == null ) {								dataSet = graph.loadDataSet( initial1, 2 );	// two points				dataSet.linestyle = 1;				dataSet.linecolor = new Color( 255, 0, 0 );				dataSet.marker = 1;				dataSet.drawLastOnly = true;					// modification to the DataSet class								xaxis = graph.createAxis( Axis.BOTTOM );				xaxis.attachDataSet( dataSet );				xaxis.setTitleText( "X axis" );								yaxis = graph.createAxis( Axis.LEFT );				yaxis.attachDataSet( dataSet );				yaxis.setTitleText( "Y axis" );			}			else		// we're restarting			{				dataSet.deleteData();				dataSet.append( initial1, 2 );			}		}		catch ( Exception e ) {			System.out.println( "Caught error setting data: " + e );			e.printStackTrace();		}		count = 2;		running = true;		iterate();	}			public void iterate() {		count++;		if ( count > 100000 )	// we're getting full!			startStop();		try {			double [] newpoint = new double[2];			newpoint[0] = Math.random() * 5;			newpoint[1] = Math.random() * 5;			dataSet.append( newpoint, 1 );	// add the point		}		catch ( Exception e ) {			System.out.println( e );		}					if ( count % updateInterval == 0 ) {			Graphics g = graph.getGraphics();			if ( osi == null || iwidth != graph.size().width || iheight !=				graph.size().height ) {				iwidth = graph.size().width;				iheight = graph.size().height;				osi = graph.createImage( iwidth, iheight );				osg = osi.getGraphics();			}						osg.setColor(this.getBackground());			osg.fillRect(0,0,iwidth,iheight);	 		osg.setColor(g.getColor());			osg.clipRect(0,0,iwidth,iheight);			graph.update(osg);			g.drawImage(osi,0,0,graph);			String s = "count = " + count;			messages.replaceRange( s, 0, 500 );	// set the information message		}	}	public void SetTimeStep( double dt ) {		if ( running )			System.out.println( "I should set the time step to " + dt );	}}