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.

88 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(25, 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. TextView view = (TextView) findViewById(R.id.debugTextView);
  40. view.setText(s);
  41. view.invalidate();
  42. viewRef.invalidate();
  43. }
  44. });
  45. }
  46. public void onStepButtonClick(View view) {
  47. //for (int j = 0; j < 3000; j++) {
  48. this.track.timeElapse(50);
  49. //}
  50. }
  51. public void onPlayButtonClick(View view) {
  52. Button playButton = (Button) findViewById(R.id.playButton);
  53. playButton.setEnabled(false);
  54. Button stepButton = (Button) findViewById(R.id.stepButton);
  55. stepButton.setEnabled(false);
  56. Button stopButton = (Button) findViewById(R.id.stopButton);
  57. stopButton.setEnabled(true);
  58. this.worker = new Worker(track);
  59. this.worker.start();
  60. }
  61. public void onStopButtonClick(View view) {
  62. Button playButton = (Button) findViewById(R.id.playButton);
  63. playButton.setEnabled(true);
  64. Button stepButton = (Button) findViewById(R.id.stepButton);
  65. stepButton.setEnabled(true);
  66. Button stopButton = (Button) findViewById(R.id.stopButton);
  67. stopButton.setEnabled(false);
  68. this.worker.setStop(true);
  69. try {
  70. this.worker.join();
  71. } catch (InterruptedException ex) {
  72. }
  73. }
  74. }