【问题标题】:Windows Forms - Won't draw a ballWindows 窗体 - 不会画球
【发布时间】: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);
    }
}

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

有什么想法吗?

【问题讨论】:

    标签: c# winforms graphics draw


    【解决方案1】:

    我对@9​​87654323@ 类进行了一些修改以管理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();
    

    【讨论】:

      猜你喜欢
      • 2021-02-09
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      • 2011-11-04
      • 2015-12-01
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多