// DrawShapes Example // Source Code File: MyView.java package it372.jost.drawshapes; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View; import java.io.InputStream; import java.util.ArrayList; import java.util.Scanner; public class MyView extends View { private Paint p; private ArrayList shapes; public MyView(Context context) { super(context); p = new Paint( ); shapes = new ArrayList( ); // Set up Scanner object InputStream iStream = getResources( ). openRawResource(R.raw.shapes); Scanner scanner = new Scanner(iStream); while(scanner.hasNextLine( )) { String line = scanner.nextLine( ); String[ ] fields = line.split(","); String s = fields[0]; int x1 = Integer.parseInt(fields[1]); int y1 = Integer.parseInt(fields[2]); int x2 = Integer.parseInt(fields[3]); int y2 = Integer.parseInt(fields[4]); String c = fields[5]; shapes.add(new Shape(s, x1, y1, x2, y2, c)); } } @Override public void onDraw(Canvas c) { super.onDraw(c); for(Shape shape : shapes) { p.setColor(Color.parseColor(shape.getColorCode( ))); if(shape.getShapeCode( ).equals("R")) { c.drawRect(shape.getXStart( ), shape.getYStart( ), shape.getXEnd( ), shape.getYEnd( ), p); } else { c.drawOval(shape.getXStart( ), shape.getYStart( ), shape.getXEnd( ), shape.getYEnd( ), p); } } } }