52 lines
1.6 KiB
Java
52 lines
1.6 KiB
Java
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++;
|
|
}
|
|
}
|
|
}
|