diff --git a/app/src/main/java/de/hems/trafficsim/MainActivity.java b/app/src/main/java/de/hems/trafficsim/MainActivity.java index a693a4d..e2581c5 100644 --- a/app/src/main/java/de/hems/trafficsim/MainActivity.java +++ b/app/src/main/java/de/hems/trafficsim/MainActivity.java @@ -1,9 +1,11 @@ package de.hems.trafficsim; import androidx.appcompat.app.AppCompatActivity; +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; @@ -12,41 +14,75 @@ import java.util.Observer; public class MainActivity extends AppCompatActivity implements Observer { protected Track track; - protected VehicleTimeRecord records; + 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); - this.records = null; + 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); + layout.addView(this.trackView); } @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"; - ; - } - String t = ""; //vtr arraylist to string - String text = s + "\n" + t; - TextView view = (TextView) findViewById(R.id.debugTextView); - view.setText(text); - view.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) { - for (int j = 0; j < 1; j++) { + 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 aaf508c..06f9ed6 100644 --- a/app/src/main/java/de/hems/trafficsim/Track.java +++ b/app/src/main/java/de/hems/trafficsim/Track.java @@ -8,14 +8,18 @@ import java.util.Observable; public class Track extends Observable { protected List vehicles; protected List> vtr_list; - + protected float trackLength; public List getVehicles() { - return vehicles; } - public Track(int numberVehicles) { + public float getTrackLength() { + return trackLength; + } + + public Track(int numberVehicles, float trackLength) { + this.trackLength = trackLength; this.vehicles = createVehiclesList(numberVehicles); this.vtr_list = new LinkedList<>(); } @@ -23,14 +27,14 @@ public class Track extends Observable { protected List createVehiclesList(int numberVehicles){ List result = new ArrayList<>(); for(int i=0;i +