这是关于我在同一个项目中遇到的问题的后续问题: 在我的图形方法中(我将其用作更新方法)我正在绘制一个本地存储在调试文件夹中的地图图像。但是,当点击地图上的某个区域时,地图应该消失并被3个图像替换。但是我遇到的问题是,一次只能绘制一个图像(最后一个)。无论如何,这是我的图形方法:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
//load the map image
Image map = Image.FromFile("map.png");
SolidBrush b = new SolidBrush(Color.Red);
//now I need to add the paint controls for is hit and not hit
if (isHit == true && goBack == false)
{
g.DrawImage(backButton, new Point(0, 0));
for (int i = 0; i < trees.Length; i++)
{
if (trees[i].isHit == true)
{
Tree t = trees[i];
Image treeImage1, treeImage2;
treeImage1 = Image.FromFile(t.EnglishName + ".png");
treeImage2 = Image.FromFile(t.EnglishName + "leaf.png");
g.DrawImage(treeImage1, new Point(10,20);
g.DrawImage(treeImage2, new Point((treeImage1.Width + 10), 20);
loadTree(trees[i]);
}
}
//update labels for text and info
// draw a back button(add control to click, use image)
}
else if(isHit == false)
{
g.DrawImage(backButton, new Point(0, 0));
g.DrawImage(map, new Point(0, 0));
for (int i = 0; i < trees.Length; i++)
{
g.FillRectangle(b, trees[i].X, trees[i].Y, 3, 3);
}
}
this.Refresh();
}
到目前为止我发现的一个问题是,只要语句为真,就不会达到第一个if语句(原因不明) 这些变量在我的鼠标单击事件中控制如下:
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
mouseX = e.X;
mouseY = e.Y;
bool isClicked = false;
for (int i = 0; i < trees.Length; i++)
{
isClicked = trees[i].checkClick(mouseX, mouseY);
if (isClicked)
{
isHit = isClicked;
if(mouseX >= 5 && mouseX <= backButton.Width)
{
if(mouseY >= 5 & mouseY <= backButton.Height)
{
goBack = true;
trees[i].isHit = false;
isHit = false;
HideLabels();
}
}
for (int j = 0; j < trees.Length; j++)
{
if (trees[j] != trees[i])
{
trees[j].isHit = false;
}
}
trees[i].isHit = true;
loadTree(trees[i]);
}
}
}
非常感谢任何和所有帮助。如果您需要更多信息,请告诉我,我会将其发布到a.s.a.p. 谢谢:))
答案 0 :(得分:0)
“但是我遇到的问题是,一次只能绘制一张图像(最后一张图像)。”
在你的点击例程中,无论什么树被击中,你都会通过这个循环重复所有其他树:
for (int j = 0; j < trees.Length; j++)
{
if (trees[j] != trees[i])
{
trees[j].isHit = false;
}
}
因此,只有最后一个命中的树才会成立。
我们不知道你的系统是什么样的(或者它应该如何表现),但是一个可能的解决方案是使用该循环ONCE,在检查所有树之前所有它们都以false开始,只有命中的那些被切换。也许是这样的:
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
// reset all the trees:
for (int j = 0; j < trees.Length; j++)
{
trees[j].isHit = false;
}
// see which trees are hit:
mouseX = e.X;
mouseY = e.Y;
bool isClicked = false;
for (int i = 0; i < trees.Length; i++)
{
isClicked = trees[i].checkClick(mouseX, mouseY);
if (isClicked)
{
// initially true, but might be reset below if the
// back button was hit instead?
trees[i].isHit = true;
isHit = isClicked;
if(mouseX >= 5 && mouseX <= backButton.Width)
{
if(mouseY >= 5 & mouseY <= backButton.Height)
{
goBack = true;
trees[i].isHit = false;
isHit = false;
HideLabels();
}
}
loadTree(trees[i]);
}
}
}
答案 1 :(得分:0)
问题实际上是在绘制后退按钮的行中。画布没有按比例缩小,并且设置得比应用程序大,所以除非你最大化应用程序,否则它将永远是真的。成功裁剪后,一切都按原样使用原始方法