DrawView.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package de.tu_darmstadt.informatik.tk.olir;
  2. import android.content.Context;
  3. import android.content.res.Resources;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.util.AttributeSet;
  8. import android.util.TypedValue;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import java.util.ArrayList;
  12. /**
  13. * Area where the object's shape gets drawn
  14. * Created by Martin Herbers on 30.03.2017.
  15. */
  16. public class DrawView extends View {
  17. /**
  18. * List of Strings where every String describes a basic shape, e.g. rectangle: [R;0;0;20;20]
  19. */
  20. ArrayList<String> shapes;
  21. /**
  22. * millimeter * scale = pixel
  23. */
  24. float scale;
  25. public DrawView(Context context) {
  26. super(context);
  27. clearShapes();
  28. }
  29. public DrawView(Context context, AttributeSet attribs) {
  30. super(context, attribs);
  31. clearShapes();
  32. }
  33. public DrawView(Context context, AttributeSet attribs, int defStyle) {
  34. super(context, attribs, defStyle);
  35. clearShapes();
  36. }
  37. @Override
  38. public void onDraw(Canvas canvas) {
  39. //Middle point of the draw area
  40. int baseX = this.getMeasuredHeight() / 2;
  41. int baseY = baseX;
  42. Paint paint = new Paint();
  43. paint.setStyle(Paint.Style.FILL);
  44. paint.setColor(Color.LTGRAY);
  45. for (String s: shapes) {
  46. //Circle
  47. if (s.charAt(1) == 'C') {
  48. //X offset from middle point
  49. s = s.substring(3);
  50. int index = s.indexOf(';');
  51. float x = Float.parseFloat(s.substring(0, index));
  52. //Y offset from middle point
  53. s = s.substring(index + 1);
  54. index = s.indexOf(';');
  55. float y = Float.parseFloat(s.substring(0, index));
  56. //the circle's radius
  57. s = s.substring(index + 1);
  58. index = s.length()-1;
  59. float radius = Float.parseFloat(s.substring(0, index)) / 2;
  60. //Draw the circle shape
  61. canvas.drawCircle(baseX + x * scale,
  62. baseY + y * scale,
  63. radius * scale,
  64. paint);
  65. //Rectangle
  66. } else if (s.charAt(1) == 'R') {
  67. //X offset from middle point
  68. s = s.substring(3);
  69. int index = s.indexOf(';');
  70. float x = Float.parseFloat(s.substring(0, index));
  71. //Y offset from middle point
  72. s = s.substring(index + 1);
  73. index = s.indexOf(';');
  74. float y = Float.parseFloat(s.substring(0, index));
  75. //width
  76. s = s.substring(index + 1);
  77. index = s.indexOf(';');
  78. float width = Float.parseFloat(s.substring(0, index));
  79. //height
  80. s = s.substring(index + 1);
  81. index = s.length()-1;
  82. float height = Float.parseFloat(s.substring(0, index));
  83. //Draw the rectangle
  84. canvas.drawRect(baseX + x * scale - width / 2 * scale,
  85. baseY + y * scale - height / 2 * scale,
  86. baseX + x * scale + width / 2 * scale,
  87. baseY + y * scale + height / 2 * scale,
  88. paint);
  89. }
  90. }
  91. }
  92. /**
  93. * Clear the shapes list, use it every time a new object is loaded
  94. */
  95. public void clearShapes() {
  96. shapes = new ArrayList<>();
  97. }
  98. /**
  99. * Add a shape to the list
  100. * @param s the string describing a basic shape
  101. */
  102. public void addShape(String s) {
  103. shapes.add(s);
  104. }
  105. /**
  106. * Set the scale to calculate millimeter to pixel
  107. * @param s
  108. */
  109. public void setScale(float s) {
  110. scale = s;
  111. }
  112. }