Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8431939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:59:22+00:00 2026-06-10T05:59:22+00:00

How can i draw a set of rectangles on a canvas Using DrawRect method?

  • 0

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
        }


}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:59:23+00:00Added an answer on June 10, 2026 at 5:59 am

    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.

    public class Coordinate {
    
    int x;
    int y;
    
    public void setX(int x) {
    this.x = x;
    }
    
    public void setY(int y) {
    this.y = y;
    }
    
    public int getX() {
    return this.x;
    }
    
    public int getY() {
    return this.y;
    
    }
    
    }
    

    You can create an array of these objects containing the individual coordinates:

    Coordinates[] coordinates = new Coordinates[10]; // 10 is just an example
    

    In your rendering method, you can now render the individual blocks.

    public void drawMap(Coordinates[] coordinates) {
    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), paint); // 10 is the dimension of your block
    }
    }
    

    I hope this helps.

    Update:

    This is how you could fill one index of the array with an Coordinate object:

    coordinates[1] = new Coordinate();
    coordinates[1].setX(0);
    coordinates[1].setY(0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set up something in SDL [in C++] where I can draw
I can draw many things using this : NSString *imagePath = [[NSBundle mainBundle] pathForResource:@dummy2.png
Any idea how I can draw Bullet Points using Flex? instead of using image
How I can draw bezier curve in canvas. I have only start point and
I have an area where the user can draw using a finger. This area
I'm drawing rectangles on the screen using the basic NSBezierPath method. I have an
I have a grid on which users can draw rectangles (actually rooms in a
I am new to HTML5 <canvas> and was trying to make something, actually draw
when using matplotlib, how can I set the text to be below other objects,
I just started to learn a little bit about canvas! I want to draw

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.