Solved

Drawing on a panel in C#

Posted on 2007-03-31
34
2,123 Views
Last Modified: 2012-06-22
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]
0
Comment
Question by:apricotsun
  • 16
  • 16
  • 2
34 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18829619
0
 

Author Comment

by:apricotsun
ID: 18829946
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
 
LVL 16

Expert Comment

by:AlexNek
ID: 18830244
>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
 

Author Comment

by:apricotsun
ID: 18830657
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
 

Author Comment

by:apricotsun
ID: 18830719
Also, wouldn't the 4,4 refer to the coordinates for X and Y?
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18831479
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
 

Author Comment

by:apricotsun
ID: 18831686
I'll try that. Thank you.
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18831726
Don't mention it. Good luck.
0
 

Author Comment

by:apricotsun
ID: 18831807
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
 
LVL 16

Expert Comment

by:AlexNek
ID: 18832200
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
 
LVL 16

Expert Comment

by:AlexNek
ID: 18832213
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
 
LVL 16

Expert Comment

by:AlexNek
ID: 18832217
It can be something like this

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

Author Comment

by:apricotsun
ID: 18832405
I'll try that. Thanks.
0
 

Author Comment

by:apricotsun
ID: 18832470
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
 
LVL 16

Expert Comment

by:AlexNek
ID: 18832955
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
 
LVL 16

Expert Comment

by:AlexNek
ID: 18832961
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
 

Author Comment

by:apricotsun
ID: 18832972
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:apricotsun
ID: 18832976
Ok. I will try that. Thanks for sticking with me and helping me.
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18832984
You are welcome.
But it is very easy:
graphics.FillEllipse(new SolidBrush(color),e.X,e.Y,size.width,size.height);
0
 

Author Comment

by:apricotsun
ID: 18833067
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
 
LVL 16

Expert Comment

by:AlexNek
ID: 18833081
Pay attention that FillEllipse is the method of Graphics class.
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18833083
>.....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
 

Author Comment

by:apricotsun
ID: 18833222
Thanks. I'll try that.
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18833267
You are welcome.
0
 

Author Comment

by:apricotsun
ID: 18833429
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
 
LVL 16

Accepted Solution

by:
AlexNek earned 500 total points
ID: 18833460
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18833500
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
 

Author Comment

by:apricotsun
ID: 18834163
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
 
LVL 16

Expert Comment

by:AlexNek
ID: 18835039
Why you need this code? Remove it all? I written it to show which parameters has the FillElipse functrion.
0
 

Author Comment

by:apricotsun
ID: 18835258
Okay. I was just confused. Thank you.
0
 

Author Comment

by:apricotsun
ID: 18835368
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
 
LVL 16

Expert Comment

by:AlexNek
ID: 18837836
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
 

Author Comment

by:apricotsun
ID: 18838453
Thanks. I really appreciate all your help and for being so very patient. I could not have gotten this far without your help.
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18838512
Don't mention it. Is it worked now?
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Email Body 4 46
Query datatable in LINQ 6 39
Long URL in SMS 6 24
Iterate a dictionnary to change values 4 23
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now