Graphics Grid Display

graphicsGrid

This was a school homework assignment. I had to get draw a grid based on what the user selected from a drop down menu. Then I had to display whatever the user typed in and it had to display on the screen. I’m still working on getting each letter typed in to get into each square individually. This wasn’t a requirement. It’s just something I wanted to see for myself. I’ll update the code when that happens.

Oh also there’s just one class for this program.

CompundAssignments Class:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CompundAssignments
{
    public partial class CompundAssignments : Form
    {
        public CompundAssignments()
        {
            InitializeComponent();

            //Select the first item in the combo box.
            gridSizeComboBox.SelectedIndex = 0;
        }

        private void DrawGrid(Graphics drawingArea)
        {
            float gridMargin = 10.0F;    //Margin to keep the grid away from the group box edge.
            int gridSize = 0;            //Grid size, 4x4, 10x10, etc.

            //Try to convert the grid size selection in the combobox to an integer.
            if (Int32.TryParse(gridSizeComboBox.Text, out gridSize))
            {
                // Make sure we have a grid size > 0 before we attempt to draw a grid.
                if (gridSize > 0)
                {
                    //Create the pen objects for drawing.
                    //Pen pen = new Pen(Color.Red, 2);
                    Pen redPen = new Pen(Color.Red, 2);
                    Pen bluePen = new Pen(Color.Blue, 2);

                    //Use the smaller of the drawing are width or height to ensure uniform grid spacing.
                    float gridWidth = drawingArea.VisibleClipBounds.Width;
                    if (drawingArea.VisibleClipBounds.Height < gridWidth)
                        gridWidth = drawingArea.VisibleClipBounds.Height;

                    //Subtract the margin value.
                    gridWidth -= gridMargin * 2;

                    //Determine grid cell width.
                    float cellWidth = gridWidth / gridSize;

                    //Set up x and y coordinates for both horizontal and vertical lines.
                    float horizontalX = gridMargin;
                    float horizontalY = 0;
                    float verticalX = 0;
                    float verticalY = gridMargin;
                    float letterX = 20;
                    float letterY = 20;

                    //The number of lines in grid is equal to the size of the grid plus 1, so loop over size of grid + 1,
                    //drawing horizontal and vertical lines.
                    for (int i = 0; i < gridSize + 1; i++)
                    {
                        //Select pen, red for even, blue for odd.
                        Pen pen = bluePen;
                        if (i % 2 == 0)
                            pen = redPen;

                        //Draw horizontal lines.
                        //Console.WriteLine("Horizontal: ({0},{1}) to ({2},{3})", horizontalX, horizontalY + gridMargin, horizontalX + gridWidth, horizontalY + gridMargin);
                        drawingArea.DrawLine(pen, horizontalX, horizontalY + gridMargin, horizontalX + gridWidth, horizontalY + gridMargin);
                        horizontalY += cellWidth;
                        //Console.WriteLine("DrawGrid verticalX: " + verticalX);

                        //drawLetter(drawingArea, gridMargin, cellWidth, verticalX, horizontalY);
                        drawLetter(drawingArea, gridMargin, cellWidth, letterX, letterY);

                        //Draw vertical lines.
                        //Console.WriteLine("Vertical: ({0},{1}) to ({2},{3})", verticalX + gridMargin, verticalY, verticalX + gridMargin, verticalY + gridWidth);
                        drawingArea.DrawLine(pen, verticalX + gridMargin, verticalY, verticalX + gridMargin, verticalY + gridWidth);
                        verticalX += cellWidth;
                        //Console.WriteLine("DrawGrid horizontalY: " + horizontalY);
                    }
                }
            }
        }

        private void gridSizeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Notify the group box control that it should redraw itself, which will call the Paint event below.
            drawGroupBox.Refresh();
        }

        private void drawGroupBox_Paint(object sender, PaintEventArgs e)
        {
            //Call the base class OnPaint event to ensure any other controls that need to redrawn are also redrawn.
            base.OnPaint(e);
            //Call our draw grid method.
            DrawGrid(e.Graphics);
        }

        private void drawLetter(Graphics drawingArea, float gridMargin, float cellWidth, float x, float y)
        {
            Console.WriteLine("drawLetter X: " + x);
            Console.WriteLine("drawLetter Y: " + y);

            //Getting the letter or number entered by the user.
            string letter = letterDisplayTextbox.Text;

            //Creating the font and size.
            Font drawFont = new Font("Arial", 16);

            //Setting the font color.
            SolidBrush drawBrush = new SolidBrush(Color.Black);

            //Adding the string to the screen.
            drawingArea.DrawString(letter, drawFont, drawBrush, x, y);

            //x += cellWidth;
            //y += cellWidth;
        }
    }
}

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s