import java.awt.*; import java.awt.event.*; import java.lang.reflect.*; import javax.swing.*; // import java.text.*; import com.genlogic.*; ////////////////////////////////////////////////////////////////////////// public class GlgElectricCircuit extends GlgJBean implements ActionListener { static final long serialVersionUID = 0; ////////////////////////////////////////////////////////////////////////// // The main demo class ////////////////////////////////////////////////////////////////////////// // Main class parameters. boolean IsReady = false; static boolean StandAlone = false; // If true, the resource path is used to animate resources of the drawing. // If false, stored resource ID is used to set resource directly with // null path using the Extended API. // Alternatively, tags may be used instead of resources. // final boolean USE_RESOURCE_PATH = false; Timer timer = null; final int UpdateInterval = 500; // milliceconds boolean UpdateData = true; // Array of resources to update, queried from the drawing. GlgObject ResourceList; // Keeps info of a resource to update class SimulationResource { GlgObject object; int type; double range; String resource_path; } ////////////////////////////////////////////////////////////////////////// public GlgElectricCircuit() { super(); SetDResource( "$config/GlgSwingUsage", 1. ); } ////////////////////////////////////////////////////////////////////////// // Stops the update thread ////////////////////////////////////////////////////////////////////////// synchronized public void stop() { StopUpdate(); IsReady = false; super.stop(); } ////////////////////////////////////////////////////////////////////////// public void StartUpdate() { if( timer == null ) { timer = new Timer( UpdateInterval, this ); timer.setRepeats( true ); timer.start(); } } ////////////////////////////////////////////////////////////////////////// public void StopUpdate() { if( timer != null ) { timer.stop(); timer = null; } } ////////////////////////////////////////////////////////////////////////// public void ToggleUpdates() { UpdateData = !UpdateData; } ////////////////////////////////////////////////////////////////////////// // Starts the update thread ////////////////////////////////////////////////////////////////////////// synchronized public void ReadyCallback() { if( GetJavaLog() ) PrintToJavaConsole( "Debug: Ready\n" ); super.ReadyCallback(); InitializeSimulation(); StartUpdate(); IsReady = true; } ////////////////////////////////////////////////////////////////////////// // For use as a stand-alone java demo. // Optional command-line arguments: [-drawing filename] ////////////////////////////////////////////////////////////////////////// public static void main( final String arg[] ) { SwingUtilities. invokeLater( new Runnable(){ public void run() { Main( arg ); } } ); } public static void Main( String arg[] ) { String filename = "electric_circuit.g"; class DemoQuit extends WindowAdapter { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } int arg_length = Array.getLength( arg ); int i = 0; // Parse options while( i < arg_length ) { if( arg[ i ].equals( "-drawing" ) ) { ++i; if( i >= arg_length ) error( "Missing filename after -drawing option", true ); filename = arg[i]; } } GlgObject.Init(); Frame frame = new Frame(); frame.setResizable( true ); frame.setSize( 800, 600 ); frame.addWindowListener( new DemoQuit() ); GlgElectricCircuit circuit = new GlgElectricCircuit(); GlgElectricCircuit.StandAlone = true; frame.add( circuit ); frame.show(); // Setting the viewport triggers the ReadyCallback which starts // the update thread. // circuit.SetViewportExt( GlgObject.LoadWidget( filename, GlgObject.FILE ) ); } ////////////////////////////////////////////////////////////////////////// // Updates display with simulated data. ////////////////////////////////////////////////////////////////////////// void UpdateCircuit() { if( ResourceList == null ) return; int size = ResourceList.GetSize(); for( int i=0; i