Implement TrackView

This commit is contained in:
Loch Christian (uib05376) 2020-10-26 21:17:40 +01:00
parent e149a5c342
commit ca1a82a9ee
5 changed files with 73 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package de.hems.trafficsim;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.os.Bundle;
import android.view.View;
@ -12,14 +13,17 @@ import java.util.Observer;
public class MainActivity extends AppCompatActivity implements Observer {
protected Track track;
protected TrackView trackView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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);
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constr_layout);
layout.addView(this.trackView);
}
@Override
@ -35,11 +39,12 @@ public class MainActivity extends AppCompatActivity implements Observer {
TextView view = (TextView) findViewById(R.id.debugTextView);
view.setText(s);
view.invalidate();
this.trackView.invalidate();
}
public void onStepButtonClick (View view) {
for (int j = 0; j < 3000; j++) {
//for (int j = 0; j < 3000; j++) {
this.track.timeElapse(50);
}
//}
}
}

View File

@ -6,15 +6,19 @@ import java.util.Observable;
public class Track extends Observable {
protected List<Vehicle> vehicles;
protected float trackLength;
public List<Vehicle> getVehicles() {
return vehicles;
}
public Track(int numberVehicles) {
public float getTrackLength() {
return trackLength;
}
public Track(int numberVehicles, float trackLength) {
this.vehicles = createVehiclesList(numberVehicles);
this.trackLength = trackLength;
}
protected List<Vehicle> createVehiclesList(int numberVehicles){
@ -37,7 +41,7 @@ public class Track extends Observable {
Vehicle forerunner = vehicles.get(forerunnerIndex);
float distanceForerunner = forerunner.getPosition() - v.getPosition() - 1;
if(distanceForerunner < 0.0){
distanceForerunner += 50;
distanceForerunner += this.trackLength;
}
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

@ -27,6 +27,10 @@ public class Vehicle {
return curVelocity;
}
public float getMaxVelocity() {
return maxVelocity;
}
public void updateVelocity(float distanceForerunner) {
Random random = new Random();

View File

@ -2,6 +2,7 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constr_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">