123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package de.tu_darmstadt.informatik.tk.olir;
- import android.content.Context;
- import android.content.res.Resources;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.util.AttributeSet;
- import android.util.TypedValue;
- import android.view.View;
- import android.view.ViewGroup;
- import java.util.ArrayList;
- /**
- * Area where the object's shape gets drawn
- * Created by Martin Herbers on 30.03.2017.
- */
- public class DrawView extends View {
- /**
- * List of Strings where every String describes a basic shape, e.g. rectangle: [R;0;0;20;20]
- */
- ArrayList<String> shapes;
- /**
- * millimeter * scale = pixel
- */
- float scale;
- public DrawView(Context context) {
- super(context);
- clearShapes();
- }
- public DrawView(Context context, AttributeSet attribs) {
- super(context, attribs);
- clearShapes();
- }
- public DrawView(Context context, AttributeSet attribs, int defStyle) {
- super(context, attribs, defStyle);
- clearShapes();
- }
- @Override
- public void onDraw(Canvas canvas) {
- //Middle point of the draw area
- int baseX = this.getMeasuredHeight() / 2;
- int baseY = baseX;
- Paint paint = new Paint();
- paint.setStyle(Paint.Style.FILL);
- paint.setColor(Color.BLACK);
- for (String s: shapes) {
- //Circle
- if (s.charAt(1) == 'C') {
- //X offset from middle point
- s = s.substring(3);
- int index = s.indexOf(';');
- float x = Float.parseFloat(s.substring(0, index));
- //Y offset from middle point
- s = s.substring(index + 1);
- index = s.indexOf(';');
- float y = Float.parseFloat(s.substring(0, index));
- //the circle's radius
- s = s.substring(index + 1);
- index = s.length()-1;
- float radius = Float.parseFloat(s.substring(0, index)) / 2;
- //Draw the circle shape
- canvas.drawCircle(baseX + x * scale,
- baseY + y * scale,
- radius * scale,
- paint);
- //Rectangle
- } else if (s.charAt(1) == 'R') {
- //X offset from middle point
- s = s.substring(3);
- int index = s.indexOf(';');
- float x = Float.parseFloat(s.substring(0, index));
- //Y offset from middle point
- s = s.substring(index + 1);
- index = s.indexOf(';');
- float y = Float.parseFloat(s.substring(0, index));
- //width
- s = s.substring(index + 1);
- index = s.indexOf(';');
- float width = Float.parseFloat(s.substring(0, index));
- //height
- s = s.substring(index + 1);
- index = s.length()-1;
- float height = Float.parseFloat(s.substring(0, index));
- //Draw the rectangle
- canvas.drawRect(baseX + x * scale - width / 2 * scale,
- baseY + y * scale - height / 2 * scale,
- baseX + x * scale + width / 2 * scale,
- baseY + y * scale + height / 2 * scale,
- paint);
- }
- }
- }
- /**
- * Clear the shapes list, use it every time a new object is loaded
- */
- public void clearShapes() {
- shapes = new ArrayList<>();
- }
- /**
- * Add a shape to the list
- * @param s the string describing a basic shape
- */
- public void addShape(String s) {
- shapes.add(s);
- }
- /**
- * Set the scale to calculate millimeter to pixel
- * @param s
- */
- public void setScale(float s) {
- scale = s;
- }
- }
|