We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Customised brushes, clearing a picturebox and more....

Iv made conways game of life, but I want the user to be able to select a pre defined pattern or for them to be able to choose different shaped brushes then draw with that brush.

Aswell, when I use the radio buttons to change the size, the program goes slow and stops working, does anyone know why that is?

And, Iv tried to create an eraser type tool using brush color.empty, but it doesnt work, how can I create an eraser tool, or a button to completely clear what the user has drawn, everything Iv tried has not worked.

One more thing, when I change the size the squares drawn do not align to the grid properly, can anyone see why?

Any help with any of these things would be great.

Cheers

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 GameOfLife
{
    public partial class Form1 : Form
    {
        cField Field = new cField();
        bool MouseIsDown = false;
        Brush mybrush = new SolidBrush(Color.White);
        private int squaresize = 4;
        private int gridSizex = 602;
        private int gridSizey = 500;
        private int Lines = 1;
        private int squareheight = 4;
        private int squarewidth = 4;
        

        public Form1()
        {
            InitializeComponent();
            timer1.Interval = trackBar1.Value;
            
        }
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (this.radioSmall.Checked)
            {
                squaresize = 4;
                squareheight = 4;
                squarewidth = 4;
                this.pictureBox1.Refresh();
            }
            if (this.radioMedium.Checked)
            {
                squaresize = 8;
                squareheight = 8;
                squarewidth = 8;
                this.pictureBox1.Refresh();
            }
            if (this.radioLarge.Checked)
            {
                squaresize = 16;
                squareheight = 16;
                squarewidth = 16;
                this.pictureBox1.Refresh();
            }
            if (Lines == 1)
            {
                Graphics grid = e.Graphics;
                Pen myPen = new Pen(Color.Yellow, 1); //create a pen object
                for (int a = 0; a <= this.gridSizex; a += this.squaresize)
                {
                    grid.DrawLine(myPen, 0, a, gridSizex, a); //use the DrawLine Horizontal
                    grid.DrawLine(myPen, a, 0, a, gridSizey); //use the DrawLine Vertical
                }
                myPen.Dispose();
            }
            for (int i = 0; i < 150; i++)
                for (int j = 0; j < 100; j++)
                    if (Field.Cells[i, j] > 0)
                        e.Graphics.FillRectangle(mybrush, i * 4, j * 4, squarewidth, squareheight);

        }
        private void btnStart_Click(object sender, EventArgs e)
        {
            timer1.Enabled = !timer1.Enabled;
            pictureBox1.Refresh();
            btnStart.Text = timer1.Enabled ? "Stop" : "Start";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Field.Recalculate();
            pictureBox1.Refresh();

        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {

            switch (e.Button)
            {
                case MouseButtons.Left:
                    timer1.Enabled = false;
                    MouseIsDown = true;
                    Field.Cells[e.X / 4, e.Y / 4] = 1;
                    break;
                case MouseButtons.Right:
                    timer1.Enabled = false;
                    MouseIsDown = true;
                    Field.Cells[e.X / 4, e.Y / 4] = 1;
                    break;
                default:
                    break;
            } 

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.X < 0) return;
            if (e.Y < 0) return;
            if (e.X / 4 > 149) return;
            if (e.Y / 4 > 99) return;

            if (MouseIsDown)
            {
                Field.Cells[e.X / 4, e.Y / 4] = 1;
                pictureBox1.Refresh();
            }

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            
            
            MouseIsDown = false;
            if (btnStart.Text == "Stop")
                timer1.Enabled = true;

        }
        public class cField
        {
            public int[,] Cells = new int[150, 100];
            public int[,] newCells = new int[150, 100];

            public void Recalculate()
            {
                int neighbours = 0;

                for (int i = 0; i < 150; i++)
                    for (int j = 0; j < 100; j++)
                        newCells[i, j] = 0;

                for (int i = 0; i < 149; i++)
                    for (int j = 0; j < 99; j++)
                    {
                        neighbours = 0;

                        if (i > 0 && j > 0)
                        {
                            if (Cells[i - 1, j - 1] > 0)
                                neighbours++;
                            if (Cells[i - 1, j] > 0)
                                neighbours++;
                            if (Cells[i - 1, j + 1] > 0)
                                neighbours++;

                            if (Cells[i, j - 1] > 0)
                                neighbours++;
                            if (Cells[i, j + 1] > 0)
                                neighbours++;

                            if (Cells[i + 1, j - 1] > 0)
                                neighbours++;
                        }

                        if (Cells[i + 1, j] > 0)
                            neighbours++;
                        if (Cells[i + 1, j + 1] > 0)
                            neighbours++;

                        if (neighbours < 2)
                            newCells[i, j] = 0;
                        if (neighbours > 3)
                            newCells[i, j] = 0;
                        if ((neighbours == 2 || neighbours == 3) && Cells[i, j] > 0)
                            newCells[i, j] = Cells[i, j] + 10;
                        if (neighbours == 3 && Cells[i, j] == 0)
                            newCells[i, j] = 1;
                    }

                for (int i = 0; i < 150; i++)
                    for (int j = 0; j < 100; j++)
                        Cells[i, j] = newCells[i, j];
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Interval = timer1.Interval - 25;
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Interval = timer1.Interval + 25;
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            timer1.Interval = trackBar1.Value;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox1.Text)
            {
                case "Black":
                    mybrush = new SolidBrush(Color.Black);
                    break;
                case "Blue":
                    mybrush = new SolidBrush(Color.Blue);
                    break;
                case "Brown":
                    mybrush = new SolidBrush(Color.Brown);
                    break;
                case "Crimson":
                    mybrush = new SolidBrush(Color.Crimson);
                    break;
                case "Cyan":
                    mybrush = new SolidBrush(Color.Cyan);
                    break;
                case "Gold":
                    mybrush = new SolidBrush(Color.Gold);
                    break;
                case "Gray":
                    mybrush = new SolidBrush(Color.Gray);
                    break;
                case "Green":
                    mybrush = new SolidBrush(Color.Green);
                    break;
                case "Indigo":
                    mybrush = new SolidBrush(Color.Indigo);
                    break;
                case "Lime":
                    mybrush = new SolidBrush(Color.Lime);
                    break;
                case "Magenta":
                    mybrush = new SolidBrush(Color.Magenta);
                    break;
                case "Maroon":
                    mybrush = new SolidBrush(Color.Maroon);
                    break;
                case "Navy":
                    mybrush = new SolidBrush(Color.Navy);
                    break;
                case "Olive":
                    mybrush = new SolidBrush(Color.Olive);
                    break;
                case "Orange":
                    mybrush = new SolidBrush(Color.Orange);
                    break;
                case "Pink":
                    mybrush = new SolidBrush(Color.Pink);
                    break;
                case "Purple":
                    mybrush = new SolidBrush(Color.Purple);
                    break;
                case "Red":
                    mybrush = new SolidBrush(Color.Red);
                    break;
                case "Silver":
                    mybrush = new SolidBrush(Color.Silver);
                    break;
                case "Teal":
                    mybrush = new SolidBrush(Color.Teal);
                    break;
                case "Violet":
                    mybrush = new SolidBrush(Color.Violet);
                    break;
                case "Yellow":
                    mybrush = new SolidBrush(Color.Yellow);
                    break;
                default:
                    break;
            }
            return;
        }

        private void trackBar1_Scroll_1(object sender, EventArgs e)
        {
            timer1.Interval = trackBar1.Value;
        }

        private void clearbtn_Click(object sender, EventArgs e)
        {
            mybrush = new SolidBrush(Color.Empty);
        }

        private void button1_Click_2(object sender, EventArgs e)
        {

        }

        
    }
    
}
3
Contributors
3
Replies
16 Hours
Discussion Span
3 Years Ago
Last Updated
4
Views
Byrne86
Newbie Poster
5 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

What exactly do you mean by different shaped brushes?
See you are using a SolidBrush.
There is also a HatchBrush (a brush with patterns)
A TextureBrush (a brush with a bitmap or jpeg image)
A LinearGradientBrush and a PathGradientBrush.

ddanbe
Industrious Poster
4,308 posts since Oct 2008
Reputation Points: 2,126
Solved Threads: 725
Skill Endorsements: 26

I need to make it so the brush covers a certain number of squares in a pre designed pattern like the following:

http://www.kulturservern.se/wronsov/selfpassage/XXI/XXI-0804/GoF1.gif

Is there a way to make the brush shaped like that?

Byrne86
Newbie Poster
5 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

i would write a method for each shape and paint the pixels around the point the user clicks.

I wrote an example that draws Shape 1 from the link you gave into a picturebox when the user clicks:

private void pictureBox5_MouseClick(object sender, MouseEventArgs e)
        {
            if (pictureBox5.Image == null)
                pictureBox5.Image = new Bitmap(pictureBox5.Width, pictureBox5.Height);
            pictureBox5.Image = DrawShape1(pictureBox5.Image, e.Location);
        }

        private Image DrawShape1(Image i, Point p)
        {
            Graphics g = Graphics.FromImage(i);
            g.DrawLine(Pens.Black, new Point(p.X, p.Y - 1), new Point(p.X, p.Y + 1));
            g.DrawLine(Pens.Black, new Point(p.X - 1, p.Y), new Point(p.X + 1, p.Y));

            return i;
        }
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 247
Skill Endorsements: 10

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0793 seconds using 2.77MB