How can i draw a set of rectangles on a canvas Using DrawRect method? I am trying to generate a dynamic indoor map by just passing the coordinates of the blocks.
I want to know.. how to use the drawRect method inside a for loop..!
I made two classes by the help of Luke Taylor.(Thank you..! 🙂 )
One is coordinate and the other one is main class called DrawMapActivity..
Now i need to know how to fill up the coordinates Array???
package itgsm.drawmap;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
public class DrawMapActivity extends Activity {
/** Called when the activity is first created. */
static Coordinates[] coordinates = new Coordinates[10]; // 10 is just an example
// public void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
//
// }
public void onDraw(Canvas canvas) {
Paint myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
myPaint.setStrokeWidth(8/*1 /getResources().getDisplayMetrics().density*/);
myPaint.setColor(0xffff0000); //color.RED
for(int i = 0; i < coordinates.length; i++) {
canvas.drawRect(new Rect(coordinates[i].getX(), coordinates[i].getY(), coordinates[i].getX() + 10,coordinates[i].getY() + 10), myPaint); // 10 is the dimension of your block
}
}
}
You could create a method that takes an array of objects that contain coordinates:
This would be the class called “Coordinate” that contains the coordinates.
You can create an array of these objects containing the individual coordinates:
In your rendering method, you can now render the individual blocks.
I hope this helps.
Update:
This is how you could fill one index of the array with an Coordinate object: