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.

87 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.LinearLayout;
  8. import android.widget.TextView;
  9. import java.util.List;
  10. import java.util.Observable;
  11. import java.util.Observer;
  12. public class MainActivity extends AppCompatActivity implements Observer {
  13. protected Track track;
  14. protected TimeRecordView trackView;
  15. protected Worker worker;
  16. protected LinearLayout viewStack;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. this.track = new Track(40, 200);
  22. this.track.addObserver(this);
  23. this.viewStack = (LinearLayout) findViewById(R.id.trackViewStack);
  24. }
  25. @Override
  26. public void update(Observable observable, Object o) {
  27. final Track trackRef = this.track;
  28. //final TimeRecordView viewRef = this.trackView;
  29. final LinearLayout viewStackRef = this.viewStack;
  30. final List<VehicleTimeRecord> newRecords = (List<VehicleTimeRecord>) o;
  31. final MainActivity mainActivity = this;
  32. runOnUiThread(new Runnable() {
  33. @Override
  34. public void run() {
  35. TimeRecordView newTrView = new TimeRecordView(mainActivity, newRecords, trackRef.getTrackLength());
  36. viewStackRef.addView(newTrView, 0);
  37. int childCount = viewStackRef.getChildCount();
  38. if (childCount > 30) {
  39. viewStackRef.removeViewAt(childCount-1);
  40. }
  41. }
  42. });
  43. }
  44. public void onStepButtonClick(View view) {
  45. //for (int j = 0; j < 3000; j++) {
  46. this.track.timeElapse();
  47. //}
  48. }
  49. public void onPlayButtonClick(View view) {
  50. Button playButton = (Button) findViewById(R.id.playButton);
  51. playButton.setEnabled(false);
  52. Button stepButton = (Button) findViewById(R.id.stepButton);
  53. stepButton.setEnabled(false);
  54. Button stopButton = (Button) findViewById(R.id.stopButton);
  55. stopButton.setEnabled(true);
  56. this.worker = new Worker(track);
  57. this.worker.start();
  58. }
  59. public void onStopButtonClick(View view) {
  60. Button playButton = (Button) findViewById(R.id.playButton);
  61. playButton.setEnabled(true);
  62. Button stepButton = (Button) findViewById(R.id.stepButton);
  63. stepButton.setEnabled(true);
  64. Button stopButton = (Button) findViewById(R.id.stopButton);
  65. stopButton.setEnabled(false);
  66. this.worker.setStop(true);
  67. try {
  68. this.worker.join();
  69. } catch (InterruptedException ex) {
  70. }
  71. }
  72. }