Java Reference
In-Depth Information
It updates the graphical user interface every 100 milliseconds. In par-
ticular, it updates the paint level of each tank and the slider buttons that
open and close the pumps.
package scada.simulator;
public class Simulator extends JFrame implements Runnable{
Tank redTank, greenTank, blueTank, mixerTank; // the tanks
Pump redInputPump, redOutputPump; // the pumps
Pump greenInputPump, greenOutputPump;
Pump blueInputPump, blueOutputPump;
Pump drainPump, sprayPump;
private int DT # 100; // interval in milliseconds (0.1 s)
Thread thread # new Thread( this );
public Simulator() { // initializes tanks and pumps
// Note that the initial values of paint level are
// hard-coded. It is easy to modify this constructor in
// such a way that the values are read from a
// configuration file.
redTank # new Tank(colorRed, 20);
greenTank # new Tank(colorGreen, 50);
blueTank # new Tank(colorBlue, 40);
mixerTank # new Tank(colorWhite, 10);
redInputPump # new Pump( null , redTank);
redOutputPump # new Pump(redTank, mixerTank);
greenInputPump # new Pump( null , greenTank);
greenOutputPump # new Pump(greenTank, mixerTank);
blueInputPump # new Pump( null , blueTank);
blueOutputPump # new Pump(blueTank, mixerTank);
drainPump # new Pump(mixerTank, null );
sprayPump # new Pump(mixerTank, null );
initGUI(); // initializes the graphical user interface
}
public void run() {
while ( true )
try {
thread.sleep(DT); // sleeps for DT milleseconds
// requests the pumps to flush a micro-volume of paint
// according to their output flow
redInputPump.flush(DT); redOutputPump.flush(DT);
greenInputPump.flush(DT); greenOutputPump.flush(DT);
blueInputPump.flush(DT); blueOutputPump.flush(DT);
drainPump.flush(DT); sprayPump.flush(DT);
this .repaint(); // repaints the graphical interface
} catch (InterruptedException ie) {ie.printStackTrace();}
}
Search WWH ::




Custom Search