You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.8KB

  1. package de.hems.trafficsim;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import androidx.constraintlayout.widget.ConstraintLayout;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8. import java.util.Observable;
  9. import java.util.Observer;
  10. public class MainActivity extends AppCompatActivity implements Observer {
  11. protected Track track;
  12. protected TrackView trackView;
  13. protected Worker worker;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. this.track = new Track(20, 100);
  19. this.trackView = new TrackView(this, this.track);
  20. this.track.addObserver(this);
  21. ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constr_layout);
  22. layout.addView(this.trackView);
  23. }
  24. @Override
  25. public void update(Observable observable, Object o) {
  26. final Track trackRef = this.track;
  27. final TrackView viewRef = this.trackView;
  28. runOnUiThread(new Runnable() {
  29. @Override
  30. public void run() {
  31. String s = "";
  32. for (Vehicle v: trackRef.getVehicles()){
  33. s+= "Vehicle " + v.id + " | "
  34. + "Pos = " + v.getPosition() + " | "
  35. + "Vel = " + v.getCurVelocity()+ " | "
  36. + "\n";
  37. ;
  38. }
  39. s += "Average: " + trackRef.getLastAvg();
  40. TextView view = (TextView) findViewById(R.id.debugTextView);
  41. view.setText(s);
  42. view.invalidate();
  43. viewRef.invalidate();
  44. }
  45. });
  46. }
  47. public void onStepButtonClick(View view) {
  48. //for (int j = 0; j < 3000; j++) {
  49. this.track.timeElapse();
  50. //}
  51. }
  52. public void onPlayButtonClick(View view) {
  53. Button playButton = (Button) findViewById(R.id.playButton);
  54. playButton.setEnabled(false);
  55. Button stepButton = (Button) findViewById(R.id.stepButton);
  56. stepButton.setEnabled(false);
  57. Button stopButton = (Button) findViewById(R.id.stopButton);
  58. stopButton.setEnabled(true);
  59. this.worker = new Worker(track);
  60. this.worker.start();
  61. }
  62. public void onStopButtonClick(View view) {
  63. Button playButton = (Button) findViewById(R.id.playButton);
  64. playButton.setEnabled(true);
  65. Button stepButton = (Button) findViewById(R.id.stepButton);
  66. stepButton.setEnabled(true);
  67. Button stopButton = (Button) findViewById(R.id.stopButton);
  68. stopButton.setEnabled(false);
  69. this.worker.setStop(true);
  70. try {
  71. this.worker.join();
  72. } catch (InterruptedException ex) {
  73. }
  74. }
  75. }