Reputation: 55
I have a map data which will draw a map like this:
private int[,] mapData = new int[11, 25]
{
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 4 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ 0, 1, 1, 2, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3 },
{ -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, 2, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, 3, 1, 1, 1, 1, 1, 1, 1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
};
And here is my render logic:
e.Graphics.Clear(Color.Gray);
float renderRectWidth = (float)pictureBox1.Width / (float)25;
float renderRectHeight = (float)pictureBox1.Height / (float)11;
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 25; j++)
{
int element = mapData[i, j];
Color c;
if (element == 0)
{
c = Color.Black;
}
else if (element == 1)
{
c = Color.Blue;
}
else if (element == 2)
{
c = Color.Orange;
}
else if (element == 3)
{
c = Color.Green;
}
else if (element == 4)
{
c = Color.Red;
}
else
{
c = Color.AliceBlue;
}
e.Graphics.FillRectangle(new SolidBrush(c), startPoint.X, startPoint.Y, renderRectWidth, renderRectHeight);
startPoint.X = startPoint.X + renderRectWidth;
}
startPoint.X = 0;
startPoint.Y = (i + 1) * renderRectHeight;
}
It will cause the following result, so my question is: why the first line can't be drawn, but other lines can be drawn?
Upvotes: 1
Views: 68
Reputation: 39132
I'd rewrite it as:
private int[,] mapData =
{
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 4 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ 0, 1, 1, 2, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3 },
{ -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, 2, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, 3, 1, 1, 1, 1, 1, 1, 1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
};
private Point startPoint;
private Dictionary<Color, SolidBrush> brushes = new Dictionary<Color, SolidBrush>();
private Color[] colors = { Color.Black, Color.Blue, Color.Orange, Color.Green, Color.Red };
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
startPoint = new Point(0, 0);
e.Graphics.Clear(Color.Gray);
float renderRectWidth = (float)pictureBox1.Width / (float)mapData.GetLength(1);
float renderRectHeight = (float)pictureBox1.Height / (float)mapData.GetLength(0);
for (int i = 0; i <= mapData.GetUpperBound(0); i++)
{
for (int j = 0; j <= mapData.GetUpperBound(1); j++)
{
int element = mapData[i, j];
Color c = (element == -1) ? Color.AliceBlue : colors[element];
if (!brushes.ContainsKey(c))
{
brushes.Add(c, new SolidBrush(c));
}
e.Graphics.FillRectangle(brushes[c], startPoint.X, startPoint.Y, renderRectWidth, renderRectHeight);
startPoint.X = startPoint.X + (int)renderRectWidth;
}
startPoint.X = 0;
startPoint.Y = startPoint.Y + (int)renderRectHeight;
}
}
Output:
Upvotes: 3