|
- 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.LinearLayout;
- import android.widget.TextView;
-
- import java.util.List;
- import java.util.Observable;
- import java.util.Observer;
-
- public class MainActivity extends AppCompatActivity implements Observer {
-
- protected Track track;
- protected TimeRecordView trackView;
- protected Worker worker;
- protected LinearLayout viewStack;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- this.track = new Track(40, 200);
- this.track.addObserver(this);
- this.viewStack = (LinearLayout) findViewById(R.id.trackViewStack);
-
- }
-
- @Override
- public void update(Observable observable, Object o) {
- final Track trackRef = this.track;
- //final TimeRecordView viewRef = this.trackView;
- final LinearLayout viewStackRef = this.viewStack;
- final List<VehicleTimeRecord> newRecords = (List<VehicleTimeRecord>) o;
- final MainActivity mainActivity = this;
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- TimeRecordView newTrView = new TimeRecordView(mainActivity, newRecords, trackRef.getTrackLength());
- viewStackRef.addView(newTrView, 0);
- int childCount = viewStackRef.getChildCount();
- if (childCount > 30) {
- viewStackRef.removeViewAt(childCount-1);
- }
- }
- });
-
- }
-
- public void onStepButtonClick(View view) {
- //for (int j = 0; j < 3000; j++) {
- this.track.timeElapse();
- //}
- }
-
- 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) {
-
- }
- }
- }
|