c#为每个阵列点绘制一个点
这个程序应该做一个数组,根据数组中的值,它应该画出不同颜色的点 首先,它创建一个空白映射(数组)。 之后,它会用障碍物填充地图。 最后,它应该在imagebox上以图形方式表示障碍物,但它没有这样做 错误是什么c#为每个阵列点绘制一个点,c#,winforms,C#,Winforms,这个程序应该做一个数组,根据数组中的值,它应该画出不同颜色的点 首先,它创建一个空白映射(数组)。 之后,它会用障碍物填充地图。 最后,它应该在imagebox上以图形方式表示障碍物,但它没有这样做 错误是什么 public partial class Form1 : Form { public byte[,] ObstacleGenerator(byte[,] map, byte leftTopX, byte leftTopY, byte rightBottomX, byte rig
public partial class Form1 : Form
{
public byte[,] ObstacleGenerator(byte[,] map, byte leftTopX, byte leftTopY, byte rightBottomX, byte rightBottomY)
{
// akadályt építi fel
for (int i = leftTopX; i <= rightBottomX; i++)
{
for (int j = leftTopY; j <= rightBottomY; j--)
{
map[i, j] = 1;
}
}
return map;
}
public byte[,] MapGenerator()
{
// a térképet építi fel
byte[,] map = new byte[102-1, 102-1];
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
//a térkép grafikusan nem látható pereme
if (i == 0 || j == 0 || i == map.GetLength(0)-1 || j == map.GetLength(1)-1)
{
map[i, j] = 253;
continue;
}
else
//járható út
{
map[i, j] = 0;
}
}
}
return map;
}
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
byte[,] map = MapGenerator();
map = ObstacleGenerator(map, 20, 80, 40, 20);
Brush aBrush = (Brush)Brushes.Black;
Graphics g = this.CreateGraphics();
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
//láthatatlan perem
if (map[i,j] == 253)
{
continue;
}
//út
else if (map[i, j] == 0)
{
continue;
}
//akadály
else if (map[i, j] == 1)
{
g.FillRectangle(aBrush, i, j, i, j);
}
}
}
}
}
公共部分类表单1:表单
{
公共字节[,]ObstacleGenerator(字节[,]映射,字节leftTopX,字节leftTopY,字节rightBottomX,字节rightBottomY)
{
//阿卡达莱蒂·皮蒂·费尔
对于(int i=leftTopX;i i i将g.FillRectangle(aBrush,i,j,i,j)更改为g.FillRectangle(aBrush,i,j,1,1);但不更改替换Graphics g=this.CreateGraphics()
使用Graphics g=e.Graphics;
-!在某个时候,你应该通过执行无效来触发Paint
事件!另外:Paint
事件属于PictureBox
但是这是表单
。根据它实际上是一个属性e.Graphics,而不是一个methodA typo,co更正..我替换了您编写的部件,但它抛出错误“名称e在当前上下文中不存在”。还有,我如何触发绘制事件?我将g.FillRectangle(aBrush,I,j,I,j)更改为g.FillRectangle(aBrush,I,j,1,1);但没有更改ReplaceGraphics g=this.CreateGraphics();
withGraphics g=e.Graphics;
-!在某个时刻,您应该通过执行Invalidate
!触发Paint
事件,该事件属于PictureBox
但this
是Form
。根据它实际上是属性e。图形,而不是方法a type,co更正..我替换了您编写的部件,但它抛出错误“名称e在当前上下文中不存在”。我如何触发绘制事件?
public partial class Form1 : Form
{
public byte[,] ObstacleGenerator(byte[,] map, byte leftTopX, byte leftTopY, byte rightBottomX, byte rightBottomY)
{
// akadályt építi fel
for (int i = leftTopX; i <= rightBottomX; i++)
{
for (int j = leftTopY; j <= rightBottomY; j--)
{
map[i, j] = 1;
}
}
return map;
}
public byte[,] MapGenerator()
{
// a térképet építi fel
byte[,] map = new byte[102-1, 102-1];
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
//a térkép grafikusan nem látható pereme
if (i == 0 || j == 0 || i == map.GetLength(0)-1 || j == map.GetLength(1)-1)
{
map[i, j] = 253;
}
else
//járható út
{
map[i, j] = 0;
}
}
}
return map;
}
public void PictureBuilder(byte[,] map)
{
Brush aBrush = (Brush)Brushes.Black;
Graphics g = e.Graphics();
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
//láthatatlan perem
if (map[i, j] == 0)
{
}
//út
else if (map[i, j] == 253)
{
}
//akadály
else if (map[i, j] == 1)
{
g.FillRectangle(aBrush, i, j, 1, 1);
}
}
}
}
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
byte startX = 1;
byte startY = 1;
byte endX = 99;
byte endY = 99;
byte currentX = startX;
byte currentY = startY;
while (true)
{
}
byte[,] map = MapGenerator();
map = ObstacleGenerator(map, 20, 80, 40, 20);
PictureBuilder(map);
}
}
public partial class Form1 : Form
{
public byte[,] ObstacleGenerator(byte[,] map, byte leftTopX, byte leftTopY, byte rightBottomX, byte rightBottomY)
{
// akadályt építi fel
for (int i = leftTopX; i <= rightBottomX; i++)
{
for (int j = leftTopY; j <= rightBottomY; j--)
{
map[i, j] = 1;
}
}
return map;
}
public byte[,] MapGenerator()
{
// a térképet építi fel
byte[,] map = new byte[102-1, 102-1];
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
//a térkép grafikusan nem látható pereme
if (i == 0 || j == 0 || i == map.GetLength(0)-1 || j == map.GetLength(1)-1)
{
map[i, j] = 253;
}
else
//járható út
{
map[i, j] = 0;
}
}
}
return map;
}
public void PictureBuilder(byte[,] map, Graphics g)
{
Brush aBrush = (Brush)Brushes.Black;
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
//láthatatlan perem
if (map[i, j] == 0)
{
}
//út
else if (map[i, j] == 253)
{
}
//akadály
else if (map[i, j] == 1)
{
g.FillRectangle(aBrush, i, j, 1, 1);
}
}
}
}
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
byte startX = 1;
byte startY = 1;
byte endX = 99;
byte endY = 99;
byte currentX = startX;
byte currentY = startY;
byte[,] map = MapGenerator();
map = ObstacleGenerator(map, 20, 80, 40, 20);
Graphics g = e.Graphics;
PictureBuilder(map, g);
}
}
public partial class Form1 : Form
{
public byte[,] ObstacleGenerator(byte[,] map, byte leftTopX, byte leftTopY, byte rightBottomX, byte rightBottomY)
{
// akadályt építi fel
for (int i = leftTopX; i <= rightBottomX; i++)
{
for (int j = leftTopY; j <= rightBottomY; j--)
{
map[i, j] = 1;
}
}
return map;
}
public byte[,] MapGenerator()
{
// a térképet építi fel
byte[,] map = new byte[102-1, 102-1];
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
//a térkép grafikusan nem látható pereme
if (i == 0 || j == 0 || i == map.GetLength(0)-1 || j == map.GetLength(1)-1)
{
map[i, j] = 253;
}
else
//járható út
{
map[i, j] = 0;
}
}
}
return map;
}
public void PictureBuilder(byte[,] map, Graphics g)
{
Brush aBrush = (Brush)Brushes.Black;
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
//láthatatlan perem
if (map[i, j] == 0)
{
}
//út
else if (map[i, j] == 253)
{
}
//akadály
else if (map[i, j] == 1)
{
g.FillRectangle(aBrush, i, j, 1, 1);
}
}
}
}
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
byte startX = 1;
byte startY = 1;
byte endX = 99;
byte endY = 99;
byte currentX = startX;
byte currentY = startY;
byte[,] map = MapGenerator();
map = ObstacleGenerator(map, 20, 80, 40, 20);
Graphics g = e.Graphics;
PictureBuilder(map, e.Graphics);
}