Windows 表格 - 不会画球

Windows Forms - Won't draw a ball

所以我在 Visual Studio 中使用 Windows 表格,我正在尝试创建一个简单的程序,它只使用 Graphics class.

这是球的代码class:

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;
    }
}

这里是主程序的class:

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);
    }
}

然而,每当我启动程序时,它都会成功运行,没有错误,但不会绘制任何东西。

有什么想法吗?

我对 Ball class 进行了一些修改以管理 List<Color>,因此可以在 运行 时添加更多颜色,一些默认属性和IDisposable支持。

另外,渲染质量增强,使用Graphics.SmoothingMode, set to AntiAlias and Graphics.CompositingQuality,设置为HighQuality

public class Ball : IDisposable
{
    private SolidBrush brush = null;
    private int PrivateState = 0;

    public Ball()
        : this(new Point(40, 40)) { }

    public Ball(Point InitialPosition)
    {
        this.FillColorList();
        this.Centre = InitialPosition;
        this.Radius = 40;
        this.brush = new SolidBrush(this.Colors[this.PrivateState]);
    }

    public Point Centre { get; set; }
    public int State {
        get { return this.PrivateState; }
        set { this.PrivateState = (value < this.Colors.Count) ? value : 0; }
    }
    public int Radius { get; set; }
    public List<Color> Colors { get; set; }

    private void FillColorList()
    {
        this.Colors = new List<Color>() { Color.White, Color.Green, Color.Blue, Color.Red };
    }

    public void Draw(Graphics g)
    {
        this.brush.Color = this.Colors[this.PrivateState];
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.FillEllipse(brush, Centre.X - Radius, Centre.Y - Radius, 2 * Radius, 2 * Radius);
    }

    public void Move(Point NewCentre) => this.Centre = NewCentre;

    public void Dispose()
    {
        if (this.brush != null)
            this.brush.Dispose();
    }
}

您的 Ball 对象仍然是一个 属性 值,但您可能想改用列表,这样您可以同时管理更多对象。

Form 设置:
(请注意 Paint()FormClosing() 事件已在 Form 构造函数中注册,因此请确保它们未在其他地方注册,除非出于其他原因使用它们)。

public Form1()
{
    InitializeComponent();
    ball = new Ball(new Point(250, 250));
    ball.State = 1;  // <- Will draw a green ball
    this.Paint += (s, e) => { ball.Draw(e.Graphics); };
    this.FormClosing += (s, e) => { ball.Dispose(); };
}

public Ball ball { get; set; }

当你想移动Ball对象时,设置新位置并调用this.Invalidate()引发FormPaint()事件:

ball.Colors.Add(Color.Orange);
ball.State = this.ball.Colors.Count - 1;  // <- Will draw an orange ball
ball.Radius = [new Radius];
ball.Move(new Point([X], [Y]));
this.Invalidate();