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.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.widget.Button;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import java.util.Observable;
|
import java.util.Observable;
|
||||||
@ -14,12 +15,13 @@ public class MainActivity extends AppCompatActivity implements Observer {
|
|||||||
|
|
||||||
protected Track track;
|
protected Track track;
|
||||||
protected TrackView trackView;
|
protected TrackView trackView;
|
||||||
|
protected Worker worker;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
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.trackView = new TrackView(this, this.track);
|
||||||
this.track.addObserver(this);
|
this.track.addObserver(this);
|
||||||
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constr_layout);
|
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constr_layout);
|
||||||
@ -28,23 +30,59 @@ public class MainActivity extends AppCompatActivity implements Observer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(Observable observable, Object o) {
|
public void update(Observable observable, Object o) {
|
||||||
String s = "";
|
final Track trackRef = this.track;
|
||||||
for (Vehicle v: this.track.getVehicles()){
|
final TrackView viewRef = this.trackView;
|
||||||
s+= "Vehicle " + v.id + " | "
|
runOnUiThread(new Runnable() {
|
||||||
+ "Pos = " + v.getPosition() + " | "
|
@Override
|
||||||
+ "Vel = " + v.getCurVelocity()+ " | "
|
public void run() {
|
||||||
+ "\n";
|
String s = "";
|
||||||
;
|
for (Vehicle v: trackRef.getVehicles()){
|
||||||
}
|
s+= "Vehicle " + v.id + " | "
|
||||||
TextView view = (TextView) findViewById(R.id.debugTextView);
|
+ "Pos = " + v.getPosition() + " | "
|
||||||
view.setText(s);
|
+ "Vel = " + v.getCurVelocity()+ " | "
|
||||||
view.invalidate();
|
+ "\n";
|
||||||
this.trackView.invalidate();
|
;
|
||||||
|
}
|
||||||
|
TextView view = (TextView) findViewById(R.id.debugTextView);
|
||||||
|
view.setText(s);
|
||||||
|
view.invalidate();
|
||||||
|
viewRef.invalidate();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onStepButtonClick (View view) {
|
public void onStepButtonClick(View view) {
|
||||||
//for (int j = 0; j < 3000; j++) {
|
//for (int j = 0; j < 3000; j++) {
|
||||||
this.track.timeElapse(50);
|
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) {
|
public Track(int numberVehicles, float trackLength) {
|
||||||
this.vehicles = createVehiclesList(numberVehicles);
|
|
||||||
this.trackLength = trackLength;
|
this.trackLength = trackLength;
|
||||||
|
this.vehicles = createVehiclesList(numberVehicles);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected List<Vehicle> createVehiclesList(int numberVehicles){
|
protected List<Vehicle> createVehiclesList(int numberVehicles){
|
||||||
List<Vehicle> result = new ArrayList<>();
|
List<Vehicle> result = new ArrayList<>();
|
||||||
for(int i=0;i<numberVehicles;i++){
|
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);
|
result.add(vehicle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,14 +8,16 @@ public class Vehicle {
|
|||||||
protected float curVelocity;
|
protected float curVelocity;
|
||||||
protected float maxVelocity;
|
protected float maxVelocity;
|
||||||
protected float brakeProp;
|
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.id = id;
|
||||||
this.position = position;
|
this.position = position;
|
||||||
this.maxVelocity = maxVelocity;
|
this.maxVelocity = maxVelocity;
|
||||||
this.brakeProp = brakeProp;
|
this.brakeProp = brakeProp;
|
||||||
this.curVelocity = 0;
|
this.curVelocity = 0;
|
||||||
|
this.trackLength = trackLength;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,7 +51,7 @@ public class Vehicle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void timeElapse(float timeStep) {
|
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"
|
android:layout_height="match_parent"
|
||||||
tools:context=".MainActivity">
|
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
|
<TextView
|
||||||
android:id="@+id/debugTextView"
|
android:id="@+id/debugTextView"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
@ -23,9 +36,21 @@
|
|||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginBottom="16dp"
|
android:layout_marginBottom="16dp"
|
||||||
android:onClick="onStepButtonClick"
|
android:onClick="onStepButtonClick"
|
||||||
android:text="Button"
|
android:text="Step"
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintHorizontal_bias="0.5"
|
app:layout_constraintHorizontal_bias="0.5"
|
||||||
app:layout_constraintStart_toStartOf="parent" />
|
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>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in New Issue
Block a user