Windows窗体 - 不会画一个球

时间:2018-05-18 22:29:02

标签: c# winforms graphics draw

所以我在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);
    }
}

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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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

此外,使用Graphics.SmoothingMode设置为AntiAliasGraphics.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对象仍然是属性值,但您可能希望使用List,这样您就可以同时管理更多对象。

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()以举起Form Paint()事件:

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