I want to know whats missing in my code...i couldnt draw a multiple boxes

Console.Title = "Checkboard Draw";
            Console.WriteLine("{0, 40}", "Checkboard Draw");
            
            CDrawer Canvas = new CDrawer(800, 600);
            int irange = 0;
            bool bError;
            do
            {
                try
                {
                    bError = false;
                    Console.Write("\nEnter the size of the square: ");
                    irange = int.Parse(Console.ReadLine());

                    if ((irange < 10) || (irange > 200))
                    {
                        Console.Write("\nThe value entered is out of range.");
                        continue;
                    }
                }
                catch (FormatException)
                {
                    Console.Write("An incorrect character was entered.");
                    continue;
                }
                finally
                {
                    Console.WriteLine("");
                    bError = true;
                }
                Canvas.Scale = irange;

                for (int idraw = 0; idraw < 800; ++idraw)
                    Canvas.AddRectangle(idraw, idraw, 10,10,Color.Red);
                
                bError = false;
            }
            while (bError);

            Console.ReadLine();
        }
    }
}

Recommended Answers

All 17 Replies

Don't understand your question very well.
And what is CDrawer?

its a GDIDrawer....im just wondering how to make a multiple squares just like on the picture above???

This code draws a blue and red rectangle on a form:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            // Create solid brushes.
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
            SolidBrush redBrush = new SolidBrush(Color.Red);
            // Create blue and red rectangles.
            Rectangle rectb = new Rectangle(20, 20, 20, 20);
            g.FillRectangle(blueBrush, rectb);
            Rectangle rectr = new Rectangle(40, 20, 20, 20);
            g.FillRectangle(redBrush, rectr);
        }
    }
}
Member Avatar for Unhnd_Exception

This doesn't divide equal space but paints a checker board

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            SolidBrush BrushRed = new SolidBrush(Color.Red);
            SolidBrush BrushBlue = new SolidBrush(Color.Blue);
            Boolean SwitchColor = false;

            for (int x = 0; x <= this.ClientSize.Width; x += 10)
            {
                for (int y = 0; y <= this.ClientSize.Height; y += 10)
                {
                    if (SwitchColor == true)
                    {
                        e.Graphics.FillRectangle(BrushRed, new Rectangle(x, y, 10, 10));
                        SwitchColor = false;
                    }
                    else
                    {
                        e.Graphics.FillRectangle(BrushBlue, new Rectangle(x, y, 10, 10));
                        SwitchColor = true;
                    }
                }
            }
            BrushRed.Dispose();
            BrushBlue.Dispose();
        }
    }
}

you need to paint the board as if all the squares were the same color. then you need to alternate the color every time.

here is the instruction and a hint that he gave us
you will use the for loop to draw a checkerboard in the GDIDrawer. When the program starts, ask the user to input the size of the square in pixels. The value accepted from the user will be checked in a loop, and must be a valid integer in the range of 10 to 200. All exceptions must be handled. The user will be trapped in the loop until a valid square size has been entered. The square size must be evenly divisible into the size of the GDIDrawer window (600 and 800). The remainder of dividing the GDIDrawer window size by the square size should be zero.
Once the size of the squares has been entered correctly, the program will draw the checkerboard pattern using a combination of for loops, using the size that the user entered for each square. The program will fill the drawing window, as shown below.

Hint
Use the remainder operator (%) to determine if the square size is evenly divisible into 800 and 600. Set the scale of the drawing window to the square size. Draw a checkerboard by using the remainder operator to determine if the x and y location is even or odd (x % 2 == 0 indicates even, x % 2 == 1 indicates odd), and switch the drawing colors.


so far i have this. i'm having a prob about the "for loop" to make checkerboard...

Console.Title = "Checkboard Draw";

            int irange = 0;
            bool bError;

            Console.WriteLine("{0, 40}", "Checkerboard GDIDrawer");

            do
            {
                try
                {
                    bError = false;
                    Console.Write("\nEnter the size of the squares: ");
                    irange = int.Parse(Console.ReadLine());

                    if ((irange < 10) || (irange > 200))
                    {
                        Console.Write("\nThe value entered is out of range.");
                        continue;
                    }
                }
                catch (FormatException)
                {
                    Console.Write("\nAn incorrect character was entered");
                    continue;
                }
                finally
                {
                    Console.Write("");
                    bError = true;
                }
                for (int isquare = 10; isquare < irange; ++isquare)
                {
                    CDrawer Canvas = new CDrawer(600, 800);
                    Canvas.SetBBScaledPixel(isquare, isquare, Color.Red);
                }
            }
            while (bError);

            Console.Write("Press the <Enter> key to exit: ");
            Console.Read();

we haven't learned yet how to use graphics or solidbrushes that you guys talking about.

Idem dito!
We (at least me) haven't heard about the CDrawer and GDIDrawer objects you guy or girl are talking about. :)

your teacher probably gave you the GDIDrawer, etc. so that you didnt have to care about brushes.

you need to do something like:

for (int row = 1; row <= row_count; row++)
            for (int column = 1; column <= columnCount; column++)
            {
                Color color;
                if (row % 2 == 0)//row is even
                {
                    if (column % 2 == 0) color = Colors.Blue;
                    else color = Colors.Red;
                }
                else
                {
                    if (column % 2 == 0) color = Colors.Red;
                    else color = Colors.Blue;
                }
                Canvas.AddRectangle(row, column, color);
            }

which line supposed to put that...if its in line 32, then,
there an error:
"No overload for method 'AddRectangle' takes 3 argument".
The row_Count and columnCount says does not exist in the current context.

My code is this

Console.Title = "Checkboard Draw";

            int irange = 0;
            bool bError;   

            Console.WriteLine("{0, 40}", "Checkerboard GDIDrawer");

            do
            {
                try
                {
                    bError = false;
                    Console.Write("\nEnter the size of the squares: ");
                    irange = int.Parse(Console.ReadLine());
                    
                    if ((irange < 10) || (irange > 200))
                    {
                        Console.Write("\nThe value entered is out of range.");
                        continue;
                    }
                }
                catch (FormatException)
                {
                    Console.Write("\nAn incorrect character was entered");
                    continue;
                }
                finally
                {
                    Console.Write("");
                    bError = true;
                }
                for (int row = 1; row <= row_count; row++)
                    for (int column = 1; column <= columnCount; column++)
                    {
                        Color color;
                        if (row % 2 == 0)
                        {
                            if (column % 2 == 0) color = Color.Blue;
                            else color = Color.Red;
                        }
                        else
                        {
                            if (column % 2 == 0) color = Color.Red;
                            else color = Color.Blue;
                        }
                        CDrawer Canvas = new CDrawer(600, 800);
                        Canvas.AddRectangle(row, column, color);
                    }
            }
            while (bError);
            Console.Write("Press the <Enter> key to exit: ");
            Console.Read();

They sure (those two variable members; row_count, columnCount) do not exist in the scope. How does the compiper go through?
Where do you instanitate these two varibles? For sure not in the code you pasted up here. What they represent and which values they should have?

Please double check where do you do:

//this means to instanitate the variable:
int row_count= xx; //xx is some value
int columnCount = xx;  //xx is some value

Mitja

the rowcount and columncount are what you have in your code as "irange". it's what the user entered for the size of the board. so you replace both for "irange"

looking at your code the AddRectangle method seems to need 5 params so you needed to do a little modification there:

Canvas.AddRectangle(row, column,10,10,color);

ok...i replace it with irange, just like you said...

the output was
it gives me more GDIDrawer
i mean just keep popping up new GDIDrawer...

private void button1_Click(object sender, EventArgs e) {
    int w = pictureBox1.Size.Width;
    int h = pictureBox1.Size.Height;
    int count = 23;

    Bitmap b = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    w /= count;
    h /= count;

    Graphics g = Graphics.FromImage(b);
    for (int i = 0; i < count; i++) {
        for (int j = 0; j < count; j++) {
            Color c = (i + j) % 2 == 0 ? Color.Red : Color.Black;
            Brush br = new SolidBrush(c);
            g.FillRectangle(br, i * w, j * h, w, h);
            br.Dispose();
        }
    }
    g.Dispose();

    pictureBox1.Image = b;
    pictureBox1.Refresh();
}

Change lines 2,3 to whatever control you want to draw on.
Change line 4 for however many are in a row.
Change lines 19,20 to assign the bitmap to whatever control you wanted to draw on in lines 2,3.

we are not using graphics or brushes...
we are using GDIDrawer...

we are not using graphics or brushes...
we are using GDIDrawer...

Good for you. What is GDIDrawer?

using visual studio, write a console application in c# that draws a checkboard using text output

using visual studio, write a console application in c# that draws a checkboard using text output

Ok, done. Now what?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.