Merge remote-tracking branch 'origin/master'

# Conflicts:
#	app/src/main/java/de/hems/trafficsim/MainActivity.java
#	app/src/main/java/de/hems/trafficsim/Track.java
This commit is contained in:
Waetschker Daniel (uib17511) 2020-10-29 13:46:59 +01:00
commit b2bc60394a
6 changed files with 175 additions and 30 deletions

View File

@ -1,9 +1,11 @@
package de.hems.trafficsim; package de.hems.trafficsim;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
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;
@ -12,41 +14,75 @@ import java.util.Observer;
public class MainActivity extends AppCompatActivity implements Observer { public class MainActivity extends AppCompatActivity implements Observer {
protected Track track; protected Track track;
protected VehicleTimeRecord records; 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); this.track = new Track(25, 100);
this.records = null; this.trackView = new TrackView(this, this.track);
this.track.addObserver(this); this.track.addObserver(this);
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constr_layout);
layout.addView(this.trackView);
} }
@Override @Override
public void update(Observable observable, Object o) { 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 = ""; String s = "";
for (Vehicle v: this.track.getVehicles()){ for (Vehicle v: trackRef.getVehicles()){
s+= "Vehicle " + v.id + " | " s+= "Vehicle " + v.id + " | "
+ "Pos = " + v.getPosition() + " | " + "Pos = " + v.getPosition() + " | "
+ "Vel = " + v.getCurVelocity()+ " | " + "Vel = " + v.getCurVelocity()+ " | "
+ "\n"; + "\n";
; ;
} }
String t = ""; //vtr arraylist to string
String text = s + "\n" + t;
TextView view = (TextView) findViewById(R.id.debugTextView); TextView view = (TextView) findViewById(R.id.debugTextView);
view.setText(text); view.setText(s);
view.invalidate(); view.invalidate();
viewRef.invalidate();
}
});
} }
public void onStepButtonClick(View view) { public void onStepButtonClick(View view) {
for (int j = 0; j < 1; 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) {
}
}
}

View File

@ -8,14 +8,18 @@ import java.util.Observable;
public class Track extends Observable { public class Track extends Observable {
protected List<Vehicle> vehicles; protected List<Vehicle> vehicles;
protected List<List<VehicleTimeRecord>> vtr_list; protected List<List<VehicleTimeRecord>> vtr_list;
protected float trackLength;
public List<Vehicle> getVehicles() { public List<Vehicle> getVehicles() {
return vehicles; return vehicles;
} }
public Track(int numberVehicles) { public float getTrackLength() {
return trackLength;
}
public Track(int numberVehicles, float trackLength) {
this.trackLength = trackLength;
this.vehicles = createVehiclesList(numberVehicles); this.vehicles = createVehiclesList(numberVehicles);
this.vtr_list = new LinkedList<>(); this.vtr_list = new LinkedList<>();
} }
@ -23,14 +27,14 @@ public class Track extends Observable {
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);
} }
return result; return result;
} }
public void timeElapse(int timeStep) { public void timeElapse(float timeStep) {
for(int i=0; i<vehicles.size();i++) { for(int i=0; i<vehicles.size();i++) {
Vehicle v = vehicles.get(i); Vehicle v = vehicles.get(i);
int forerunnerIndex = i + 1; int forerunnerIndex = i + 1;
@ -40,7 +44,7 @@ public class Track extends Observable {
Vehicle forerunner = vehicles.get(forerunnerIndex); Vehicle forerunner = vehicles.get(forerunnerIndex);
float distanceForerunner = forerunner.getPosition() - v.getPosition() - 1; float distanceForerunner = forerunner.getPosition() - v.getPosition() - 1;
if(distanceForerunner < 0.0){ if(distanceForerunner < 0.0){
distanceForerunner += 50; distanceForerunner += this.trackLength;
} }
v.updateVelocity(distanceForerunner); v.updateVelocity(distanceForerunner);
} }

View File

@ -0,0 +1,51 @@
package de.hems.trafficsim;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
public class TrackView extends View {
protected Paint paint;
protected Track track;
protected int pixelPerVehicle;
public TrackView(Context context, Track track) {
super(context);
this.track = track;
this.paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
}
protected int getColor(float curVelocity, float maxVelocity) {
float perc = curVelocity / maxVelocity;
if (perc <= 0.5) {
int red = ((int) (2*perc*0xFF)) << 16;
int green = ((int)0xFF) << 8;
int blue = 0;
return 0xff000000 | red | green | blue;
} else {
int red = ((int)(0xFF)) << 16;
int green = ((int)(0xFF-0xFF*(perc-0.5)*2)) << 8;
int blue = 0;
return 0xff000000 | red | green | blue;
}
}
@Override
protected void onDraw(Canvas canvas) {
this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
//super.onDraw(canvas);
int i = 0;
for (Vehicle v : this.track.getVehicles()) {
int left = (int) (this.pixelPerVehicle * v.getPosition());
this.paint.setColor(getColor(v.getCurVelocity(), v.getMaxVelocity()));
canvas.drawRect(left, 0, left + this.pixelPerVehicle - 1,
20, this.paint);
i++;
}
}
}

View File

@ -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;
} }
@ -27,6 +29,10 @@ public class Vehicle {
return curVelocity; return curVelocity;
} }
public float getMaxVelocity() {
return maxVelocity;
}
public void updateVelocity(float distanceForerunner) { public void updateVelocity(float distanceForerunner) {
Random random = new Random(); Random random = new Random();
@ -45,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;
} }
} }

View 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);
}
}
}

View File

@ -2,10 +2,24 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constr_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
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"
@ -22,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>