89 lines
2.8 KiB
Java
89 lines
2.8 KiB
Java
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(20, 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";
|
|
;
|
|
}
|
|
s += "Average: " + trackRef.getLastAvg();
|
|
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();
|
|
//}
|
|
}
|
|
|
|
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) {
|
|
|
|
}
|
|
}
|
|
} |