NeFix
NeFix

Reputation: 9

Draw three Rectangles in C#

I got one class "Making" that draw one Rectangle into Form1 class.

The problem is that i have to create two more rectangle in different position in the form1 but i dont know how to draw two more rectangles in same class

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

Upvotes: 1

Views: 484

Answers (1)

siride
siride

Reputation: 209955

The problem isn't that the rectangles aren't being drawn, it's that all the rectangles are in a line, so you'll end up with three overlapping rectangles of the same height and color. They'll appear to look like one longer rectangle from x = 30 to 60.

Upvotes: 1

Related Questions