为什么我在表格上绘制的方法不起作用? ( C# )

时间:2019-04-10 18:17:02

标签: c# graphics

[已解决] 我正在尝试通过控制台和绘画来模拟“生活游戏”,但是我的表单是纯白色的。如何使我的方法可用?

在Visual Studio 2019中,这是我的第一次C#代码,只是为了与基于对象的语言相处。我尝试了基于事件的绘图(鼠标单击,按钮),一切都很好。

我的表单类如下:

public partial class Board : Form
{
    private int BOARDWIDTH;
    private int BOARDHEIGHT;
    private int cellSize;
    private bool paint = false; //ADDED IN
    private World world;
    Graphics drawArea;
    public Board(World world, int cellsize)
    {
        this.BOARDHEIGHT = world.getWorldHeight() * cellsize;
        this.BOARDWIDTH = world.getWorldWidth() * cellsize;
        InitializeComponent();
        drawingArea.SetBounds(0, 0, this.BOARDWIDTH, this.BOARDHEIGHT);
        drawArea = drawingArea.CreateGraphics();
        this.cellSize = cellsize;
        this.world = world;
    }
    public void updateBoard(World world)
    {
        this.world = world;
    }
    protected override void OnPaint(PaintEventArgs e) // ADDED IN (Basically previous DrawCell inside)
    {
        base.OnPaint(e);
        if(paint)
        {
            Rectangle rect = new Rectangle(x*this.cellSize, y*this.cellSize, 
this.cellSize, this.cellSize);

            if (this.world.getCellState(x, y))
            {
                rect.Height--;
                rect.Width--;
                using (Pen wPen = new Pen(Color.Black))
                {
                    e.Graphics.DrawRectangle(wPen, rect);
                }

            }
            else
            {
                using (SolidBrush bBrush = new SolidBrush(Color.Black))
                {
                    e.Graphics.FillRectangle(bBrush, rect);
                }

            }
            paint = false;
        }
    }
    private void DrawCell(int x, int y) //CHANGED
    {
        this.x = x;
        this.y = y;
        paint = true;
    }
    public void DrawWorld(int refreshRate)
    {
        for(int i = 0; i < this.world.getWorldHeight(); i++)
        {
            for(int j = 0; j <this.world.getWorldWidth(); j++)
            {
                DrawCell(j, i);
            }
        }
    }
}
}

我的主要表情如下:

static void Main()
{
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        World world = new World(20, 20);
        world.createRandomWorld();
        Board board = new Board(world, 10);
        Application.Run(board);
        board.DrawWorld(10);
}

我想得到一个黑色/白色正方形的网格,白色带边框。现在我唯一得到的是我的代码没有崩溃

[编辑]我添加了OnPaint并改变了我的DrawCell方法,现在我有了一些东西(绘制,是的),但是我只有一个单元格,而不是整个网格。另外,我的bool paint解决方案感觉像是一种解决方法,而不是正确的解决方法。如果有的话,我很乐意看到“正确的”工作代码,因为我没有在Web(Microsoft页面)上提供的定义/示例没有向我展示如何使它以我的方式工作^。^ < / p>

2 个答案:

答案 0 :(得分:1)

应该在每次调用World时“按需”绘制整个OnPaint,而不是尝试立即绘制“此单元格”。

例如,当Invalidate()中的某些内容发生更改时,您可以通过在表单上调用World来引起重新绘制。 OnPaint也将自动被调用,例如,当您的表单首次显示时,或在被另一个窗口隐藏后重新出现时。

答案 1 :(得分:0)

好吧,所以在了解到我无法以“我的方式”完成操作(使用在Board类外部调用的方法在Form上绘制)之后,我做了一些重新设置,并添加了一个计时器+设置OptimizedDoubleBuffer true。感谢所有提供帮助的人,如果有人想看一下我现有的Board课程,它看起来像这样:https://pastebin.com/CP9XRN6r(不想用不必要的代码占用空间),希望您过得愉快,我的帖子有一天会帮助某人的^。^

相关问题