|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- 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;
- import java.util.Observer;
-
- public class MainActivity extends AppCompatActivity implements Observer {
-
- protected Track track;
- protected TrackView trackView;
- protected Worker worker;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- 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) {
- 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 < 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) {
-
- }
- }
- }
|