Standalone Applications

All NetCharts applets can be executed from within a standalone Java application without any functionality restrictions. This allows developers to embed NetCharts applets into larger applications, or to combine multiple NetCharts applets in one display window.

When using NetCharts applets within an application, each applet should be constructed, initialized and added to an appropriate Frame or Panel. After the applet init() and start() methods are called, the application needs to specify a source of parameter definitions, in order to define the graph. Currently, the following sources are supported:

Through a combination of these sources, applications can initialize and modify embedded applets using a wide variety of methods. The examples below show a few common methods for imbedding applets and controlling their display parameters.

License File Processing

When NetCharts applets are embedded in a standalone application, a valid NFLicense.dat file must be found somewhere along the CLASSPATH as defined by each user's environment variables. If the license file is not found, the applets will display by a message banner.

Example 1: Simple Applet Embedding

The following code displays an applet within a Frame and defines its parameters using the loadParams(String) method. The loadParams() method can be called at any time after the applet has been initialized. If the chart is already displayed, an "Update" command should be sent to the applet via loadParams() in order to update the display.

public static void main (String args[])
{
	Frame frame = new Frame ("Standalone Application");
	frame.setLayout (new BorderLayout());

	try {
		NFBarchartApp bar = new NFBarchartApp();

		bar.init ();

		bar.start ();

		bar.loadParams (
			"Header     = ('StandAlone Application');"+
			"DataSets   = ('Fred', blue), ('Sally', red);"+
			"DataSet1   = 0, -200, 300, 400, 200;"+
			"DataSet2   = 40, 30, 20, 10, 500;"+
			"Update;"
		);

		frame.add ("Center", bar);

	} catch (Exception e) {
		System.out.println (e.getMessage());
	}

	frame.resize (300, 300);
	frame.show ();
}

Example 2: URL-based Parameter Definition

The following code initializes the applet from the "barchart.dat" file, which is assumed to be in the current directory, and adds it to a frame:

public static void main (String args[])
{
	Frame   frame = new Frame ("Application Test");

	frame.setLayout (new GridLayout(1,1));

	try { 
		NFBarchartApp bar = new NFBarchartApp ();
		bar.init ();
		bar.start ();
		bar.loadParams (NFParam.getFileURL("bartest.dat"));
		bar.loadParams ("Update");
		frame.add (bar);

	} catch (Exception e) {  
		System.out.println (e.toString()); 
	} 

	frame.resize (900, 400);
	frame.show();
}

Notice how the applet is initialized and added to the frame, prior to the frame being shown. When the frame is shown, the applet will automatically be displayed via its paint() method.

The file designated in the loadParams() method should contain a valid set of parameter expressions for the given applet. The syntax for such files is identical to the NFParamScript parameter supported in an HTML file.

Example 3: Embedding Multiple Applets

This example shows how to initialize two applets within the same frame, loading both of them from local data files:

public static void main (String args[])
{
	Frame   frame = new Frame ("Application Test");

	frame.setLayout (new GridLayout(1,2));

	try { 
		NFBarchartApp bar = new NFBarchartApp ();
		bar.init ();
		bar.start ();
		bar.loadParams (NFParam.getFileURL("bartest.dat"));
		bar.loadParams ("Update");
		frame.add (bar);

		NFPiechartApp pie = new NFPiechartApp ();
		pie.init ();
		pie.start ();
		pie.loadParams (NFParam.getFileURL("pietest.dat"));
		pie.loadParams ("Update");
		frame.add (pie);

	} catch (Exception e) {  
		System.out.println (e.toString()); 
	} 

	frame.resize (900, 400);
	frame.show();
}

Example 4: Multiple Parameter Definition Sources

The following code initializes the applet using a collection of String expressions, and then establishes a connection to a Parameter Server.

public static void main (String args[])
{
	Frame   frame = new Frame ("Application Test");

	frame.setLayout (new GridLayout(1,1));

	try { 
		NFBarchartApp bar = new NFBarchartApp ();

		bar.init ();

		bar.start ();

		bar.loadParams ("Background = (red)");

		bar.loadParams (
			  "Header = ('Hello World');"
			+ "HeaderBox = (blue, SHADOW, 4)");

		bar.loadParams ("Update");

		bar.loadParams ("hostname", 2341, "Arg String");

		frame.add (bar);

	} catch (Exception e) {  
		System.out.println (e.toString()); 
	} 

	frame.resize (300, 300);
	frame.show();
}

Example 5: Dynamic Parameter Modification

The following code modifies the background color of the applet after waiting a few seconds in a separate thread.

public class apptest implements Runnable
{
	static NFBarchartApp	bar;

	public static void main (String args[])
	{
		Frame   frame = new Frame ("Application Test");

		frame.setLayout (new GridLayout(1,1));
 
		try { 
			bar = new NFBarchartApp ();
			bar.init ();
			bar.start ();
			bar.loadParams ("Background = (red)");
			bar.loadParams ("Update");
			frame.add (bar);
 
		} catch (Exception e) {  
			System.out.println (e.toString()); 
		} 

		frame.resize (300, 300);
		frame.show();

		apptest bg = new apptest ();
		Thread serverThread = new Thread(bg);
		serverThread.start();
	}

	public void run ()
	{
		try {
			Thread.sleep (2000);
			bar.loadParams ("Background = (blue)");
			bar.loadParams ("Update");
		} catch (Exception e) {  
			System.out.println (e.toString()); 
		} 
	}
}