diff --git a/app/src/main/java/de/hems/trafficsim/MainActivity.java b/app/src/main/java/de/hems/trafficsim/MainActivity.java index f185b30..e2581c5 100644 --- a/app/src/main/java/de/hems/trafficsim/MainActivity.java +++ b/app/src/main/java/de/hems/trafficsim/MainActivity.java @@ -5,6 +5,7 @@ import androidx.constraintlayout.widget.ConstraintLayout; import android.os.Bundle; import android.view.View; +import android.widget.Button; import android.widget.TextView; import java.util.Observable; @@ -14,12 +15,13 @@ public class MainActivity extends AppCompatActivity implements Observer { protected Track track; protected TrackView trackView; + protected Worker worker; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - this.track = new Track(20, 50); + this.track = new Track(25, 100); this.trackView = new TrackView(this, this.track); this.track.addObserver(this); ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constr_layout); @@ -28,23 +30,59 @@ public class MainActivity extends AppCompatActivity implements Observer { @Override public void update(Observable observable, Object o) { - String s = ""; - for (Vehicle v: this.track.getVehicles()){ - s+= "Vehicle " + v.id + " | " - + "Pos = " + v.getPosition() + " | " - + "Vel = " + v.getCurVelocity()+ " | " - + "\n"; - ; - } - TextView view = (TextView) findViewById(R.id.debugTextView); - view.setText(s); - view.invalidate(); - this.trackView.invalidate(); + final Track trackRef = this.track; + final TrackView viewRef = this.trackView; + runOnUiThread(new Runnable() { + @Override + public void run() { + String s = ""; + for (Vehicle v: trackRef.getVehicles()){ + s+= "Vehicle " + v.id + " | " + + "Pos = " + v.getPosition() + " | " + + "Vel = " + v.getCurVelocity()+ " | " + + "\n"; + ; + } + TextView view = (TextView) findViewById(R.id.debugTextView); + view.setText(s); + view.invalidate(); + viewRef.invalidate(); + } + }); + } - public void onStepButtonClick (View view) { + public void onStepButtonClick(View view) { //for (int j = 0; j < 3000; j++) { this.track.timeElapse(50); //} } + + public void onPlayButtonClick(View view) { + Button playButton = (Button) findViewById(R.id.playButton); + playButton.setEnabled(false); + Button stepButton = (Button) findViewById(R.id.stepButton); + stepButton.setEnabled(false); + Button stopButton = (Button) findViewById(R.id.stopButton); + stopButton.setEnabled(true); + + this.worker = new Worker(track); + this.worker.start(); + } + + public void onStopButtonClick(View view) { + Button playButton = (Button) findViewById(R.id.playButton); + playButton.setEnabled(true); + Button stepButton = (Button) findViewById(R.id.stepButton); + stepButton.setEnabled(true); + Button stopButton = (Button) findViewById(R.id.stopButton); + stopButton.setEnabled(false); + + this.worker.setStop(true); + try { + this.worker.join(); + } catch (InterruptedException ex) { + + } + } } \ No newline at end of file diff --git a/app/src/main/java/de/hems/trafficsim/Track.java b/app/src/main/java/de/hems/trafficsim/Track.java index 3b16013..db6a655 100644 --- a/app/src/main/java/de/hems/trafficsim/Track.java +++ b/app/src/main/java/de/hems/trafficsim/Track.java @@ -17,14 +17,15 @@ public class Track extends Observable { } public Track(int numberVehicles, float trackLength) { - this.vehicles = createVehiclesList(numberVehicles); this.trackLength = trackLength; + this.vehicles = createVehiclesList(numberVehicles); + } protected List createVehiclesList(int numberVehicles){ List result = new ArrayList<>(); for(int i=0;i +