You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
4.2KB

  1. package de.hems.trafficsim;
  2. import android.graphics.Canvas;
  3. import android.graphics.Color;
  4. import android.graphics.Paint;
  5. import android.graphics.Rect;
  6. import android.view.SurfaceHolder;
  7. import java.util.ConcurrentModificationException;
  8. import java.util.List;
  9. public class Renderer {
  10. private List<VehicleTimeRecord> stepRecords;
  11. protected Track track;
  12. protected int pixelPerVehicle;
  13. protected int pixelPerLine;
  14. protected float tooShortPerTrackLength;
  15. protected float tooShortPerHeight;
  16. protected Paint paint;
  17. protected SurfaceHolder holder;
  18. protected Canvas canvas;
  19. protected int width;
  20. protected int height;
  21. public Renderer(Track track, SurfaceHolder holder) {
  22. this.track = track;
  23. this.holder = holder;
  24. this.paint = new Paint();
  25. }
  26. public void setSize(int width, int height) {
  27. if (width > 0 && height > 0) {
  28. this.pixelPerVehicle = width / (int) this.track.getTrackLength();
  29. this.tooShortPerTrackLength =
  30. (width - this.pixelPerVehicle * this.track.getTrackLength())
  31. / this.track.getTrackLength();
  32. this.pixelPerLine = height / this.track.getHistoryLength();
  33. this.tooShortPerHeight = (height - this.pixelPerLine * this.track.getHistoryLength())
  34. / (float)height;
  35. System.out.println("Viewport: "+width+"x"+height);
  36. this.width = width;
  37. this.height = height;
  38. }
  39. }
  40. public void setTrack(Track track) {
  41. this.track = track;
  42. this.setSize(width, height);
  43. }
  44. protected int getColor(float curVelocity, float maxVelocity) {
  45. float perc = curVelocity / maxVelocity;
  46. perc = 1 - perc;
  47. if (perc <= 0.5) {
  48. int red = ((int) (2*perc*0xFF)) << 16;
  49. int green = 0xFF << 8;
  50. int blue = 0;
  51. return 0xff000000 | red | green | blue;
  52. } else {
  53. int red = 0xFF << 16;
  54. int green = ((int)(0xFF-0xFF*(perc-0.5)*2)) << 8;
  55. int blue = 0;
  56. return 0xff000000 | red | green | blue;
  57. }
  58. }
  59. protected void draw() {
  60. try {
  61. if (this.pixelPerVehicle == 0) {
  62. Rect rect = this.holder.getSurfaceFrame();
  63. this.setSize(rect.right, rect.bottom);
  64. }
  65. canvas = this.holder.lockCanvas();
  66. synchronized (holder) {
  67. int y = 0;
  68. int left = 0;
  69. int compensate = 0;
  70. float vCompensate = 0;
  71. canvas.drawColor(Color.BLACK);
  72. for (int curStepIdx = this.track.getVtrList().size()-1;curStepIdx >= 0;
  73. curStepIdx--) {
  74. try {
  75. try {
  76. track.getListSemaphore().acquire();
  77. } catch (InterruptedException ex) {
  78. return;
  79. }
  80. vCompensate = Math.round(y * this.tooShortPerHeight);
  81. stepRecords = this.track.getVtrList().get(curStepIdx);
  82. int i = 0;
  83. for (VehicleTimeRecord r : stepRecords) {
  84. left = (int) (this.pixelPerVehicle * r.getPosition());
  85. compensate = Math.round(r.getPosition() * this.tooShortPerTrackLength);
  86. this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity()));
  87. canvas.drawRect(left + compensate, y+vCompensate,
  88. left + this.pixelPerVehicle + compensate,
  89. y + pixelPerLine + vCompensate, this.paint);
  90. i++;
  91. }
  92. track.getListSemaphore().release();
  93. y += pixelPerLine;
  94. } catch (ConcurrentModificationException ex) {
  95. System.out.println("Concurrent Exception occured, skipping record");
  96. y += pixelPerLine;
  97. }
  98. }
  99. }
  100. } finally {
  101. if (canvas != null) {
  102. this.holder.unlockCanvasAndPost(canvas);
  103. }
  104. }
  105. }
  106. }