create vehicle list

create observer
implement debug text view
This commit is contained in:
Waetschker Daniel (uib17511) 2020-09-16 17:40:56 +02:00 committed by Waetschker Daniel (uib17511)
parent e9820ebfd2
commit 23ac354bb6
3 changed files with 68 additions and 5 deletions

View File

@ -3,12 +3,37 @@ package de.hems.trafficsim;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
import java.util.Observable;
import java.util.Observer;
public class MainActivity extends AppCompatActivity implements Observer {
protected Track track;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.track = new Track();
this.track.addObserver(this);
}
@Override
public void update(Observable observable, Object o) {
String s = "";
for (Vehicle v: this.track.getVehicles()){
s+="Pos = " + v.getPosition() +"\n";
}
TextView view = (TextView) findViewById(R.id.debugTextView);
view.setText(s);
view.invalidate();
}
public void onStepButtonClick (View view){
this.track.timeElapse(50);
}
}

View File

@ -1,16 +1,33 @@
package de.hems.trafficsim;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
public class Track {
public class Track extends Observable {
protected List<Vehicle> vehicles;
public List<Vehicle> getVehicles() {
return vehicles;
}
public Track() {
this.vehicles = new LinkedList<>();
this.vehicles = createVehiclesList();
}
protected List<Vehicle> createVehiclesList(){
List<Vehicle> result = new ArrayList<>();
for(int i=0;i<10;i++){
Vehicle vehicle = new Vehicle(i, 5, 0.2f);
result.add(vehicle);
}
return result;
}
public void timeElapse(float timeStep) {
this.setChanged();
this.notifyObservers(this);
this.clearChanged();
}
}

View File

@ -6,4 +6,25 @@
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/debugTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/stepButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:onClick="onStepButtonClick"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>