Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill a 2D Rectangle

Tags:

c#

draw

2d

xna

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?

like image 550
ESD Avatar asked Jan 23 '26 11:01

ESD


1 Answers

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.

like image 142
Cyral Avatar answered Jan 25 '26 00:01

Cyral