Drawing on a panel in C#

I'm new to C# and am trying to develop this drawing program in GUI. You can select the color of the brush from radio buttons(Red, Blue, Green, Black) and the size from radio buttons (Small, Medium, Large) and you should be able to draw in a panel. I'm kind of lost with this one - I can create the brush but don't know how to get it to select the color or the size. Any help you provide will be greatly appreciated. Here's my code so far:
[CODE]
public partial class drawingPanelForm : Form
   {
      bool shouldPaint = true;

      public drawingPanelForm()
      {
         InitializeComponent();
      }

      private void drawPanel_Paint(object sender, PaintEventArgs e)
      {
         drawPanel.BackColor = Color.White;
      }

      private void drawPanel_MouseDown(object sender, MouseEventArgs e)
      {
         shouldPaint = true;
      }

      private void drawPanel_MouseUp(object sender, MouseEventArgs e)
      {
         shouldPaint = false;
      }

      private void drawPanel_MouseMove(object sender, MouseEventArgs e)
      {
         if (shouldPaint)
         {
            Graphics graphics = CreateGraphics();
            graphics.FillEllipse(new SolidBrush(Color.Red), e.X, e.Y, 4, 4);
            graphics.Dispose();
               
         }
      }

      private void redRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         SolidBrush brush = new SolidBrush(Color.Red);
      }

      private void blueRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         SolidBrush brush = new SolidBrush(Color.Blue);
      }

      private void greenRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         SolidBrush brush = new SolidBrush(Color.Green);
      }

      private void blackRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         SolidBrush brush = new SolidBrush(Color.Black);
      }

     

   }
[/CODE]
apricotsunAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
0
apricotsunAuthor Commented:
I looked at it but it makes no sense to me. How do I get mine to work? Would I need nested ifs? And where would I put it? Please help.
0
AlexNekCommented:
>but don't know how to get it to select the color or the size
You already did it. color = red, size = 4
graphics.FillEllipse(new SolidBrush(Color.Red), e.X, e.Y, 4, 4);
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

apricotsunAuthor Commented:
Yes, but that was in the following code
[CODE}
private void drawPanel_MouseMove(object sender, MouseEventArgs e)
      {
         if (shouldPaint)
         {
            Graphics graphics = CreateGraphics();
            graphics.FillEllipse(new SolidBrush(Color.Red), e.X, e.Y, 4, 4);
            graphics.Dispose();
               
         }

[/CODE]

how do I separate this code so that I can select it with a radio button? I mean select red or blue or green or black separately from the size - small, medium, or large?
0
apricotsunAuthor Commented:
Also, wouldn't the 4,4 refer to the coordinates for X and Y?
0
AlexNekCommented:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawinggraphicsclassfillellipsetopic3.asp

public void FillEllipse(
   Brush brush,
   int x,
   int y,
   int width,
   int height
);
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawingsolidbrushclassctortopic.asp

public SolidBrush(
   Color color
);

It is quite easy. Create a class data memeber variable, fill it and use it.
graphics.FillEllipse(new SolidBrush(m_Color), e.X, e.Y, m_Width, m_Heigth);
0
apricotsunAuthor Commented:
I'll try that. Thank you.
0
AlexNekCommented:
Don't mention it. Good luck.
0
apricotsunAuthor Commented:
I must be a total idiot cause I can't seem to figure out how to get this to work. Maybe it's because I've tried so many different versions that I have lost all objectivity. Here's the adjusted code.
[CODE]
public partial class drawingPanelForm : Form
   {
      bool shouldPaint = true;

      public void FillEllipse(
         Brush brush,
         int x,
         int y,
         int width,
         int height
      );

      public void SolidBrush(Color color);

      public drawingPanelForm()
      {
         InitializeComponent();
      }

      private void drawPanel_Paint(object sender, PaintEventArgs e)
      {
         drawPanel.BackColor = Color.White;

         

      }

      private void drawPanel_MouseDown(object sender, MouseEventArgs e)
      {
         shouldPaint = true;
      }

      private void drawPanel_MouseUp(object sender, MouseEventArgs e)
      {
         shouldPaint = false;
      }

      private void drawPanel_MouseMove(object sender, MouseEventArgs e)
      {
         if (shouldPaint == true)
         {
            Graphics graphics = CreateGraphics();
            graphics.FillEllipse(new SolidBrush(Color.Blue), e.X, e.Y, 4, 4);
            graphics.Dispose();
               
         }
      }

      private void redRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         

         
         graphics.FillEllipse(new SolidBrush(m_Red), e.X, e.Y, m_Width, m_Heigth);
      }

      private void blueRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         
         graphics.FillEllipse(new SolidBrush(m_Blue), e.X, e.Y, m_Width, m_Heigth);

         
      }

      private void greenRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         graphics.FillEllipse(new SolidBrush(m_Green), e.X, e.Y, m_Width, m_Heigth);

         
      }

      private void blackRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         graphics.FillEllipse(new SolidBrush(m_Black), e.X, e.Y, m_Width, m_Heigth);

         
      }

     

   }
}[/CODE]

I get the following error messages:
SimpleWindowsGUIProgram.drawingPanelForm.FillEllipse(System.Drawing.Brush, int, int, int, int)' must declare a body because it is not marked abstract or extern

SimpleWindowsGUIProgram.drawingPanelForm.SolidBrush(System.Drawing.Color)' must declare a body because it is not marked abstract or extern      


      
0
AlexNekCommented:
I can't see a difference here:
private void drawPanel_MouseMove(object sender, MouseEventArgs e)
      {
         if (shouldPaint == true)
         {
            Graphics graphics = CreateGraphics();
            graphics.FillEllipse(new SolidBrush(Color.Blue), e.X, e.Y, 4, 4);
            graphics.Dispose();
               
         }
      }
0
AlexNekCommented:
This is wrong. You declared local variable which you can't use later
private void redRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         SolidBrush brush = new SolidBrush(Color.Red);
      }
0
AlexNekCommented:
It can be something like this

private Color m_Color;
...
private void redRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         m_Color = Color.Red;
      }
0
apricotsunAuthor Commented:
I'll try that. Thanks.
0
apricotsunAuthor Commented:
I reworked it a little and this is what I have. The only problem is I still can't get it to draw anything.
[CODE]
public partial class drawingPanelForm : Form
   {
      bool shouldPaint = false; // determines whether to paint
      Size size = new Size(8, 8);
      Color color = Color.Black;


      //default constructor
      public drawingPanelForm()
      {
         InitializeComponent();
      }

      //should paint when mouse button is pressed down
      private void drawingPanelForm_MouseDown(object sender, MouseEventArgs e)
      {
         //indicate that user is dragging the mouse
         shouldPaint = true;
      }

      //stop painting when mouse button is released
      private void drawingPanelForm_MouseUp(object sender, MouseEventArgs e)
      {
         //indicate that user released the mouse button
         shouldPaint = false;
      }


      private void redRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         color = Color.Red;


      }

      private void blueRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         color = Color.Blue;

      }

      private void greenRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         color = Color.Green;


      }

      private void blackRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         color = Color.Black;


      }

      private void smallRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         size = new Size(4, 4);

      }

      private void mediumRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         size = new Size(6, 6);

      }

      private void largeRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         size = new Size(12, 12);


      }

      private void drawPanel_MouseMove(object sender, MouseEventArgs e)
      {
         if (shouldPaint)//check if mouse button is being pressed
         {
            //draw where the mouse pointer is present
            Graphics graphics = CreateGraphics();
            graphics.FillEllipse(new SolidBrush(Color.Chartreuse),e.X,e.Y,4,4);
            graphics.Dispose();
         }




      }
   }
}
[/CODE]
0
AlexNekCommented:
You must change this function

private void drawPanel_MouseMove(object sender, MouseEventArgs e)
      {
         if (shouldPaint)//check if mouse button is being pressed
         {
            //draw where the mouse pointer is present
            Graphics graphics = CreateGraphics();
            graphics.FillEllipse(new SolidBrush(Color.Chartreuse),e.X,e.Y,4,4);
            graphics.Dispose();
         }
0
AlexNekCommented:
remove this and use m_ prefix before class data member, please.
public void FillEllipse(
         Brush brush,
         int x,
         int y,
         int width,
         int height
      );
0
apricotsunAuthor Commented:
How do I change it so that it reflects the color and size chosen? I have been struggling with it but have not found something that works yet. I tried the following but that just gave me error messages:
[CODE]
private void drawPanel_MouseMove(object sender, MouseEventArgs e)
      {
         if (shouldPaint)//check if mouse button is being pressed
         {
            //draw where the mouse pointer is present
            Graphics graphics = CreateGraphics();
            graphics.FillEllipse();
            graphics.Dispose();
         }
[/CODE]

FillEllipse will not work without any parameters. How do I get around it?
0
apricotsunAuthor Commented:
Ok. I will try that. Thanks for sticking with me and helping me.
0
AlexNekCommented:
You are welcome.
But it is very easy:
graphics.FillEllipse(new SolidBrush(color),e.X,e.Y,size.width,size.height);
0
apricotsunAuthor Commented:
I did that. I removed the above mousemove code and added the public void ellipse ...method but I get the following error:
Error      1      SimpleWindowsGUIProg.drawingPanelForm.FillEllipse(System.Drawing.Brush, int, int, int, int)' must declare a body because it is not marked abstract or extern      

Also where do I put the following code:
[CODE]
graphics.FillEllipse(new SolidBrush(m_Color), e.X, e.Y, m_Width, m_Heigth);

[/CODE]

I tried all this before but I kept getting error messages.What am I doing wrong?
0
AlexNekCommented:
Pay attention that FillEllipse is the method of Graphics class.
0
AlexNekCommented:
>.....Also where do I put the following code:
Instead the old one
graphics.FillEllipse(new SolidBrush(Color.Chartreuse),e.X,e.Y,4,4);
0
apricotsunAuthor Commented:
Thanks. I'll try that.
0
AlexNekCommented:
You are welcome.
0
apricotsunAuthor Commented:
This is a really dumb question. But since FillEllipse is a method, what do I put in the body? I tried putting the ints but it doesn't do anything and the error message keeps asking me to declare a body. Help!
0
AlexNekCommented:
private void drawPanel_MouseMove(object sender, MouseEventArgs e)
      {
         if (shouldPaint)//check if mouse button is being pressed
         {
            //draw where the mouse pointer is present
            Graphics graphics = CreateGraphics();
            graphics.FillEllipse(new SolidBrush(color),e.X,e.Y,size.width,size.height);
            graphics.Dispose();
         }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You need a completely different design paradigm...

The use of CreateGraphics() is a major problem.  It gives you a TEMPORARY drawing surface.  As soon as the controls (or the form) repaints itself your "drawings" will be erased.

Furthermoe, there is an implied "this." in front of CreateGraphics() so what you are drawing onto is the form itself, not the panel.

To make your drawings persistent you need to either draw onto a Bitmap (or some other kind of Image) and set that up as the form background or use the Graphics supplied in the "e" parameter of the panels Paint() event.

If you would like to be able to "undo" or dynamically change some of the already drawn items then you need to store the data about them in some kind of collection such as the ArrayList.  Then you can change/modify them at will.  In the Paint() event you iterate over the data and paint accordingly.

Again, study the code examples in the link I gave earlier...VB.Net is not that different from C#...
0
apricotsunAuthor Commented:
What I meant was the following code:
[CODE]
public void FillEllipse(Brush brush,int x,int y,int width,int height);
   
      Brush brush;
      int x;
      int y;
      int width;
      int height;
[/CODE]

That's what's causing the errors. How do I fix that? And yes how do I get the freehand drawings to stay and not disappear?
0
AlexNekCommented:
Why you need this code? Remove it all? I written it to show which parameters has the FillElipse functrion.
0
apricotsunAuthor Commented:
Okay. I was just confused. Thank you.
0
apricotsunAuthor Commented:
This is what I have so far and still won't draw on the panel.
[CODE]
public partial class drawingPanelForm : Form
   {
      bool shouldPaint = false; // determines whether to paint
      private Color m_Color;
      public int m_Width;
      public int m_Height;

     
   

      //default constructor
      public drawingPanelForm()
      {
         InitializeComponent();
      }

      //should paint when mouse button is pressed down
      private void drawingPanelForm_MouseDown(object sender, MouseEventArgs e)
      {
         //indicate that user is dragging the mouse
         shouldPaint = true;
      }

      //stop painting when mouse button is released
      private void drawingPanelForm_MouseUp(object sender, MouseEventArgs e)
      {
         //indicate that user released the mouse button
         shouldPaint = false;
      }


      private void redRadioButton_CheckedChanged(object sender, EventArgs e)
      {
   
         m_Color = Color.Red;
      }

      private void blueRadioButton_CheckedChanged(object sender, EventArgs e)
      {
       
         m_Color = Color.Blue;
      }

      private void greenRadioButton_CheckedChanged(object sender, EventArgs e)
      {
       
         m_Color = Color.Green;

      }

      private void blackRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         
         m_Color = Color.Black;

      }

      private void smallRadioButton_CheckedChanged(object sender, EventArgs e)
      {
       
         
         m_Width = 4;
         m_Height = 4;
      }

      private void mediumRadioButton_CheckedChanged(object sender, EventArgs e)
      {

         m_Width = 8;
         m_Height = 8;
      }

      private void largeRadioButton_CheckedChanged(object sender, EventArgs e)
      {

         m_Width = 12;
         m_Height = 12;

      }

      private void drawPanel_MouseMove(object sender, MouseEventArgs e)
      {
         if (shouldPaint)//check if mouse button is being pressed
         {
            //draw where the mouse pointer is present
            Graphics graphics = CreateGraphics();
            graphics.FillEllipse(new SolidBrush(m_Color), e.X, e.Y, m_Width, m_Height);
            graphics.Dispose();
         }
 
      }


     
   }
}
[/CODE]
0
AlexNekCommented:
If your code in the first post was working then thie last one must worked too.
I can't see something wrong, except that you draw on the form not on the panel.
0
apricotsunAuthor Commented:
Thanks. I really appreciate all your help and for being so very patient. I could not have gotten this far without your help.
0
AlexNekCommented:
Don't mention it. Is it worked now?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.