SwingLights

ftComputing : Programme für die fischertechnik-Interfaces und -konstruktionskästen
  
ftComputing.de
Home
Back
Sitemap
Index
Links
Impressum
Mail

 

Example : The Lights with Swing

Ampel mit Swing

Simple traffic lights with lamps Red(Rot) = 3, Yellow(Gelb) = 2 and Green(Grün) and a switch on E1.

The program is constructed with JBuilder5 and SDK1.3 (It will surely run with other environments because of only simple elements are used).
It contains out of the automatic generated start program Haup.java and the JFrame Form.java which contains the real application. It uses the package ftcomputing.JavaFish and FishThread. Below some details of interest (the complete source is contained in JavaFish.ZIP ) :

Structure

Declaration of an object JavaFish ft = new JavaFish(); and a field Ampel ampel; (situated an class level).

Start of the application with button START (cmdAction), internaly the thread ampel is started.

End the application with button ENDE/HALT (cmdEnde) or with pressing the Escape key / Switch E1.

The Thread Ampel ampel contains the TrafficLights program itself. Using a thread for this (running it direct with cmdAction is possible too) was choosen, to enable a reaction of GUI while Ampel is running. In this case : The HALT button can be used, the round number (Runde) can be displayed.

The Button START 

void cmdAction_actionPerformed(ActionEvent e) {
String Portname;
int res;

ft.setAnalogScan(true);
Portname = txtPortname.getText().toUpperCase();
txtPortname.setText(Portname);
if(ft.openInterface(Portname) == ft.FT_ERROR) {
lblStatus.setText("Is Nich");
return;
};

ampel = new Ampel(ft);
ampel.start();
cmdAction.setEnabled(false);
cmdEnde.setText("HALT");
cmdEnde.requestFocus();
}

  1. ft.openInterface : choose the interface and connect to it.
  2. ampel = new Ampel(ft); ... : Instancing and starting the thread ampel.
  3. cmdAction.setEnabled(false) : Lock cmdAction.

The Button ENDE

void cmdEnde_actionPerformed(ActionEvent e) {
if(cmdEnde.getText() == "ENDE") System.exit(0);
else {
ft.emergencyHalt = true;
try{ampel.join();} catch(InterruptedException ex){}
}}

  1. if(cmdEnde ... : Toggle. If cmdEnde has the Text "ENDE", it ends the program, otherwise it starts ending the thread.
  2. ft.emergencyHalt : Propertiy for controlling the end request.
  3. ampel.join() : Wait for the end of the thread.

The Thread ampel

public class Ampel extends FishThread {
public Ampel(JavaFish ft){
super(ft);
}
public void run(){
int Runde = 0;
do {
Runde++;
lblStatus.setText("Runde : " + Runde);
lblAnalog.setText("EX : " + ft.getAnalog(0));
ft.setMotor(1, ft.ON);
ft.pause(1000);
ft.setMotor(1, ft.OFF);
ft.setMotor(2, ft.ON);
ft.pause(500);
ft.setMotor(2,ft.OFF);
ft.setMotor(3, ft.ON);
ft.pause(1234);
ft.setMotor(2, ft.ON);
ft.pause(500);
ft.setMotor(3, ft.OFF);
ft.setMotor(2, ft.OFF);
} while (!ft.finish(1));
ft.clearMotors();
ft.closeInterface();
lblStatus.setText("Finito");
cmdEnde.setText("ENDE");
cmdAction.setEnabled(true);
}}

  1. class Ampel is a member class, it has access rights for the form (e.g. lblStatus ....).
  2. run(), is the only method of the class wihich contains the TrafficLight program.
  3. ft.pause() controls end requests for the thread. In constant intervals between the "sleep" of the thread it asks for ft.emergencyHalt and the Escape key.
  4. ft.finish(1) tut gleiches und wartet außerdem auf E1 = true.
  5. The GUI is availlable to response to application requests because of that pause mechanism. Without an pause method, a Thread.yield() can be use to make the loop interruptable.

Stanze 51 663

An other example (german) is Stanze 51 663. The class JavaFish has been extended a little bit in this case.

Last Update : 26.09.2004