Add Start-Stop-Buttons, implement Threading
This commit is contained in:
parent
ca1a82a9ee
commit
bffcddf6e2
@ -5,6 +5,7 @@ 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;
|
||||
@ -14,12 +15,13 @@ 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, 50);
|
||||
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);
|
||||
@ -28,8 +30,13 @@ public class MainActivity extends AppCompatActivity implements Observer {
|
||||
|
||||
@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: this.track.getVehicles()){
|
||||
for (Vehicle v: trackRef.getVehicles()){
|
||||
s+= "Vehicle " + v.id + " | "
|
||||
+ "Pos = " + v.getPosition() + " | "
|
||||
+ "Vel = " + v.getCurVelocity()+ " | "
|
||||
@ -39,12 +46,43 @@ public class MainActivity extends AppCompatActivity implements Observer {
|
||||
TextView view = (TextView) findViewById(R.id.debugTextView);
|
||||
view.setText(s);
|
||||
view.invalidate();
|
||||
this.trackView.invalidate();
|
||||
viewRef.invalidate();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void onStepButtonClick (View view) {
|
||||
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) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -17,14 +17,15 @@ public class Track extends Observable {
|
||||
}
|
||||
|
||||
public Track(int numberVehicles, float trackLength) {
|
||||
this.vehicles = createVehiclesList(numberVehicles);
|
||||
this.trackLength = trackLength;
|
||||
this.vehicles = createVehiclesList(numberVehicles);
|
||||
|
||||
}
|
||||
|
||||
protected List<Vehicle> createVehiclesList(int numberVehicles){
|
||||
List<Vehicle> result = new ArrayList<>();
|
||||
for(int i=0;i<numberVehicles;i++){
|
||||
Vehicle vehicle = new Vehicle(i, i, 5, 0.2f);
|
||||
Vehicle vehicle = new Vehicle(i, i, 5, 0.2f, this.trackLength);
|
||||
result.add(vehicle);
|
||||
}
|
||||
|
||||
|
@ -8,14 +8,16 @@ public class Vehicle {
|
||||
protected float curVelocity;
|
||||
protected float maxVelocity;
|
||||
protected float brakeProp;
|
||||
protected float trackLength;
|
||||
|
||||
|
||||
public Vehicle(int id, int position, float maxVelocity, float brakeProp) {
|
||||
public Vehicle(int id, int position, float maxVelocity, float brakeProp, float trackLength) {
|
||||
this.id = id;
|
||||
this.position = position;
|
||||
this.maxVelocity = maxVelocity;
|
||||
this.brakeProp = brakeProp;
|
||||
this.curVelocity = 0;
|
||||
this.trackLength = trackLength;
|
||||
|
||||
}
|
||||
|
||||
@ -49,7 +51,7 @@ public class Vehicle {
|
||||
}
|
||||
|
||||
public void timeElapse(float timeStep) {
|
||||
position = (position + curVelocity) % 50;
|
||||
position = (position + curVelocity) % this.trackLength;
|
||||
}
|
||||
|
||||
}
|
||||
|
22
app/src/main/java/de/hems/trafficsim/Worker.java
Normal file
22
app/src/main/java/de/hems/trafficsim/Worker.java
Normal file
@ -0,0 +1,22 @@
|
||||
package de.hems.trafficsim;
|
||||
|
||||
public class Worker extends Thread {
|
||||
protected Track track;
|
||||
protected boolean stop;
|
||||
public Worker(Track track) {
|
||||
super();
|
||||
this.track = track;
|
||||
this.stop = false;
|
||||
}
|
||||
|
||||
void setStop(boolean stop) {
|
||||
this.stop = stop;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!stop) {
|
||||
this.track.timeElapse(50);
|
||||
}
|
||||
}
|
||||
}
|
@ -7,6 +7,19 @@
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<Button
|
||||
android:id="@+id/stopButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:enabled="false"
|
||||
android:onClick="onStopButtonClick"
|
||||
android:text="Stop"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.95"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/debugTextView"
|
||||
android:layout_width="wrap_content"
|
||||
@ -23,9 +36,21 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:onClick="onStepButtonClick"
|
||||
android:text="Button"
|
||||
android:text="Step"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.5"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/playButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:onClick="onPlayButtonClick"
|
||||
android:text="Play"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.049"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in New Issue
Block a user