Browse Source

implement and integrate VehicleTimeRecord

tags/Release_1
Waetschker Daniel (uib17511) 3 years ago
parent
commit
a3dd3b6585
3 changed files with 23 additions and 8 deletions
  1. +10
    -3
      app/src/main/java/de/hems/trafficsim/MainActivity.java
  2. +8
    -2
      app/src/main/java/de/hems/trafficsim/Track.java
  3. +5
    -3
      app/src/main/java/de/hems/trafficsim/VehicleTimeRecord.java

+ 10
- 3
app/src/main/java/de/hems/trafficsim/MainActivity.java View File

@@ -12,12 +12,14 @@ import java.util.Observer;
public class MainActivity extends AppCompatActivity implements Observer {

protected Track track;
protected VehicleTimeRecord records;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.track = new Track(20);
this.records = null;
this.track.addObserver(this);

}
@@ -32,14 +34,19 @@ public class MainActivity extends AppCompatActivity implements Observer {
+ "\n";
;
}
String t = ""; //vtr arraylist to string
String text = s + "\n" + t;
TextView view = (TextView) findViewById(R.id.debugTextView);
view.setText(s);
view.setText(text);
view.invalidate();
}

public void onStepButtonClick (View view) {
for (int j = 0; j < 3000; j++) {
for (int j = 0; j < 1; j++) {
this.track.timeElapse(50);
}
}
}


}


+ 8
- 2
app/src/main/java/de/hems/trafficsim/Track.java View File

@@ -1,11 +1,13 @@
package de.hems.trafficsim;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Observable;

public class Track extends Observable {
protected List<Vehicle> vehicles;
protected List<List<VehicleTimeRecord>> vtr_list;


public List<Vehicle> getVehicles() {
@@ -15,6 +17,7 @@ public class Track extends Observable {

public Track(int numberVehicles) {
this.vehicles = createVehiclesList(numberVehicles);
this.vtr_list = new LinkedList<>();
}

protected List<Vehicle> createVehiclesList(int numberVehicles){
@@ -27,7 +30,7 @@ public class Track extends Observable {
return result;
}

public void timeElapse(float timeStep) {
public void timeElapse(int timeStep) {
for(int i=0; i<vehicles.size();i++) {
Vehicle v = vehicles.get(i);
int forerunnerIndex = i + 1;
@@ -41,9 +44,12 @@ public class Track extends Observable {
}
v.updateVelocity(distanceForerunner);
}

List <VehicleTimeRecord> records = new ArrayList<>(vehicles.size());
this.vtr_list.add(records);
for(Vehicle v: vehicles){
v.timeElapse(50);
VehicleTimeRecord vtr = new VehicleTimeRecord(v.id, v.position, v.curVelocity, timeStep);
records.add(vtr);
}

this.setChanged();


+ 5
- 3
app/src/main/java/de/hems/trafficsim/VehicleTimeRecord.java View File

@@ -13,12 +13,14 @@ public class VehicleTimeRecord {
return timestep;
}

protected int id;
protected float position;
protected float velocity;
protected float timestep;
protected int timestep;

public VehicleTimeRecord(float position, float velocity, float timestep) {
this.position = position;
public VehicleTimeRecord(int id, float position, float velocity, int timestep) {
this.id = id;
this.position = position; //needed???
this.velocity = velocity;
this.timestep = timestep;
}


Loading…
Cancel
Save