【发布时间】:2018-05-18 22:29:02
【问题描述】:
因此,我在 Visual Studio 中使用 Windows 窗体,并尝试创建一个简单的程序,该程序仅使用 Graphics 类绘制一个球。
这是球类的代码:
public class Ball
{
public Point centre { get; set; }
public int state { get; set; }
public static int radius = 40;
public void Draw(Graphics g)
{
Brush brush;
brush = new SolidBrush(Color.White);
if(state==0)
{
brush = new SolidBrush(Color.Green);
}
if(state==1)
{
brush = new SolidBrush(Color.Blue);
}
if(state==2)
{
brush = new SolidBrush(Color.Red);
}
g.FillEllipse(brush, centre.X - radius, centre.Y - radius, 2 * radius, 2 * radius);
}
public void Move(Point centre)
{
this.centre = centre;
}
}
这是主程序的类:
public partial class Form1 : Form
{
public Ball ball { get; set; }
public Form1()
{
InitializeComponent();
ball = new Ball();
this.Width = 500;
this.Height = 500;
ball.centre = new Point(250, 250);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
ball.state = 1;
ball.Draw(g);
}
}
但是,每当我启动程序时,它都会成功运行,没有错误,但没有绘制任何东西。
有什么想法吗?
【问题讨论】: