在C#中绘制三个矩形

时间:2012-11-25 01:56:04

标签: c#

我上了一堂课#34;制作"将一个Rectangle绘制到Form1类中。

问题是我必须在form1中的不同位置再创建两个矩形,但我不知道如何在同一个类中绘制另外两个矩形

Making.cs:

    class Making
{
    public Rectangle[] makingRec;
    private SolidBrush brush;
    private int x, y, width, height;

    public Making()
    {
        makingRec = new Rectangle[7];
        brush = new SolidBrush(Color.Red);

        x = 50;
        y = 50;
        width = 10;
        height = 10;

        for (int i = 0; i < makingRec.Length; i++)
        {
            makingRec[i] = new Rectangle(x, y, width, height);
            x -= 10;
        }

    }

    public void drawMaking(Graphics paper)
    {
        foreach (Rectangle making in makingRec)
        {
            paper.FillRectangle(brush, making);
        }

    }}
}

Form1.cs中:

    public partial class Form1 : Form
{
    Graphics paper;
    Making making = new Making();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        paper = e.Graphics;
        making.drawMaking(paper);

1 个答案:

答案 0 :(得分:1)

问题不在于没有绘制矩形,而是所有矩形都在一条直线上,所以你最终会得到三个相同高度和颜色的重叠矩形。从x = 30到60,它们看起来像一个较长的矩形。