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 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.LTGRAY); 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; } }