ESD
ESD

Reputation: 544

Fill a 2D Rectangle

I am new to XNA and right now I am drawing a rectangle using this code:

// Calculate particle intensity
intense = (int)((float)part.Life / PARTICLES_MAX_LIFE);
// Generate pen for the particle
pen = new Pen(Color.FromArgb(intense * m_Color.R , intense * m_Color.G, intense * m_Color.B));
// Draw particle
g.DrawRectangle(pen, part.Position.X, part.Position.Y, 
    Math.Max(1,8 * part.Life / PARTICLES_MAX_LIFE),
    Math.Max(1,8 * part.Life / PARTICLES_MAX_LIFE));
pen.Dispose();

All the ways to fill a rectangle with color that I found online dosen't seems to apply to the way I draw my rectangle. How can I fill it with color?

Upvotes: 2

Views: 9768

Answers (1)

Cyral
Cyral

Reputation: 14153

Your code appears to be most likely made for GDI and not XNA, therefor it is not working correctly.

However, XNA includes a very useful Rectangle structure.

This means that you can "stretch" an image to fill a rectangle, so create a new Texture2D that is 1x1 pixels, and stretch the dimensions when drawn to increase the size.. (Or you could load one)

Texture2D texture = new Texture2D(graphics, 1, 1, false, SurfaceFormat.Color);
texture.SetData<Color>(new Color[] { Color.White });
return texture;

You can use this small texture in conjunction with the Rectangle based overload method for SpriteBatch

spriteBatch.Draw(texture, new Rectangle(X, Y, Width, Height), Color.White);

Change the Width, Height, Position and Color to your liking.

Upvotes: 11

Related Questions