@@ -1,6 +1,7 @@ | |||||
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; | ||||
@@ -12,14 +13,17 @@ 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 TrackView trackView; | |||||
@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(20, 50); | |||||
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 | ||||
@@ -35,11 +39,12 @@ public class MainActivity extends AppCompatActivity implements Observer { | |||||
TextView view = (TextView) findViewById(R.id.debugTextView); | TextView view = (TextView) findViewById(R.id.debugTextView); | ||||
view.setText(s); | view.setText(s); | ||||
view.invalidate(); | view.invalidate(); | ||||
this.trackView.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); | ||||
} | |||||
//} | |||||
} | } | ||||
} | } |
@@ -6,15 +6,19 @@ import java.util.Observable; | |||||
public class Track extends Observable { | public class Track extends Observable { | ||||
protected List<Vehicle> vehicles; | protected List<Vehicle> vehicles; | ||||
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.vehicles = createVehiclesList(numberVehicles); | this.vehicles = createVehiclesList(numberVehicles); | ||||
this.trackLength = trackLength; | |||||
} | } | ||||
protected List<Vehicle> createVehiclesList(int numberVehicles){ | protected List<Vehicle> createVehiclesList(int numberVehicles){ | ||||
@@ -37,7 +41,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); | ||||
} | } | ||||
@@ -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++; | |||||
} | |||||
} | |||||
} |
@@ -27,6 +27,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(); | ||||
@@ -2,6 +2,7 @@ | |||||
<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"> | ||||