【发布时间】:2018-08-29 00:53:24
【问题描述】:
我一直在做一个简单的图形项目,我可以使用按钮绘制形状并编辑它们(调整大小和其他东西)。我已经能够绘制不同的形状,但我的问题是我无法再次绘制相同的形状。它只更新之前相同形状的参数。
例如,我尝试绘制一个矩形,然后我想以不同的大小再次绘制,矩形改变它的大小而不是创建一个新的。
这是我的代码:
形状类
class ShapeClass
{
public int x { get; set; }
public int y { get; set; }
public int width { get; set; }
public int height { get; set; }
public Pen color { get; set; }
public string Shape { get; set; }
}
绘图类
class Draw:ShapeClass
{
public void DrawRectangle(PaintEventArgs e)
{
e.Graphics.DrawRectangle(color, new Rectangle(x, y, width, height));
}
public void DrawSquare(PaintEventArgs e)
{
e.Graphics.DrawRectangle(color, new Rectangle(x, y, width, height));
}
public void DrawCircle(PaintEventArgs e)
{
e.Graphics.DrawEllipse(color, new Rectangle(x,y,width,height));
}
public void DrawEllipse(PaintEventArgs e)
{
e.Graphics.DrawEllipse(color, new Rectangle(x, y, width, height));
}
绘画活动
public void PaintRect(object sender, PaintEventArgs e)
{
//Calls Draw class and sets the parameters of the Shape.
Draw d = new Draw();
d.x = rX;
d.y = rY;
d.width = rW;
d.height = rH;
rC = new Pen(Color.Red, 2);
d.color = rC;
d.DrawRectangle(e);
}
public void PaintSquare(object sender, PaintEventArgs e)
{
//Calls Draw class and sets the parameters of the Shape.
Draw d = new Draw();
d.x = sX;
d.y = sY;
d.width = sW;
d.height = sH;
d.color = new Pen(Color.Red, 2);
//d._Rectangle.Add(new Rectangle(sX,sY,sW,sH));
d.DrawSquare(e);
}
public void PaintCircle(object sender, PaintEventArgs e)
{
//Calls Draw class and sets the parameters of the Shape.
Draw d = new Draw();
d.x = cX;
d.y = cY;
d.width = cW;
d.height = cH;
d.color = new Pen(Color.Red, 2);
d.DrawCircle(e);
}
public void PaintEllipse(object sender, PaintEventArgs e)
{
//Calls Draw class and sets the parameters of the Shape.
Draw d = new Draw();
d.x = eX;
d.y = eY;
d.width = eW;
d.height = eH;
d.color = new Pen(Color.Red, 2);
d.DrawEllipse(e);
}
【问题讨论】:
-
我想我们需要看看
ShapeClass的定义,看看你是如何定义各种位置和大小参数的。 -
编辑了正文。
-
我显然遗漏了一些东西。你如何定义
cX、cY、cW和cH? -
每次引发
Paint事件时,您之前绘制的所有内容都会被删除。如果要绘制多个形状,则必须将这些形状存储在类级别的列表中,然后在每个Paint事件上绘制该列表中的所有内容。您应该有一个Paint事件处理程序来绘制列表中的每个项目,然后添加一个新形状,即向该列表添加一个项目,然后通过调用Refresh强制重新绘制。 -
在被告知要做什么后几秒钟内您无法编写正确的代码这一事实并不表明您不能这样做。把一些想法放进去。尽你最大的努力,然后,如果它不起作用,用新代码更新你的问题,我们可以提供进一步的帮助。简而言之,总是先为自己做你能做的事,只有在你真正陷入困境时才寻求更多帮助。假设你做不到并要求别人为你编写代码并不是让你自己编写代码更好的方法。失败是学习不可或缺的一部分,因此失败的可能性不是不尝试的理由。