I want to draw an circle from a method in another Class. But it says invalid parameters on line 41 and 42 in my class. When im done whith this project it should be an analog clock. and this is my first Project im unsing the drawing Event.Im undersatnding a shit and dont know how to fix my Problem. Im new in Forms. I allready tried to draw the circle in the main and it works fine but however it didnt work putting into a class. Im sorry for my classnames they might seem strange because im german. Thanks for your help.
Thats my class:
class Ziffernblatt
{
Size RectSize;
Point RectPoint = new Point(5, 10);
Rectangle Myrect;
Rectangle MyCircle;
Pen MyPen = new Pen(Color.Black, 1);
Pen Invpen = new Pen(Color.White, 1);
Graphics gObject;
public Ziffernblatt(Graphics NgObject)
{
gObject = NgObject;
}
public void Draw(int PosX, int PosY)
{
RectSize.Width = PosX / 2;
RectSize.Height = PosY / 2;
RectPoint.X = PosX / 2 - RectSize.Width / 2;
RectPoint.Y = PosY / 2 - RectSize.Height / 2;
Myrect = new Rectangle(RectPoint, RectSize);
MyCircle = new Rectangle(RectPoint, RectSize);
gObject.DrawRectangle(Pens.Red, Myrect);
gObject.DrawEllipse(Pens.Black, MyCircle);
}
and this is my main:
public partial class Form1 : Form
{
Ziffernblatt[] Ziffern = new Ziffernblatt[1];
Graphics gObject;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
gObject = e.Graphics;
Ziffern[0] = new Ziffernblatt(gObject);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
Ziffern[0].Draw(this.ClientSize.Width, this.ClientSize.Height);
Invalidate();
}
}
You received this error because of you encapsulated the Graphics object to you class. Just call your draw method from the paint event.
The best practice when drawing is performed from the paint method of the Form1
. Take to account that the Graphics
object will be changed for each paint event.
Your code might look like this:
public partial class Form1 : Form
{
Ziffernblatt[] Ziffern = new Ziffernblatt[1];
public Form1()
{
InitializeComponent();
Ziffern[0] = new Ziffernblatt();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Ziffern[0].Draw(e.Graphics, this.ClientSize.Width, this.ClientSize.Height);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
//Ziffern[0].Draw(this.ClientSize.Width, this.ClientSize.Height);
Invalidate();
}
}
public class Ziffernblatt
{
Size RectSize;
Point RectPoint = new Point(5, 10);
Rectangle Myrect;
Rectangle MyCircle;
Pen MyPen = new Pen(Color.Black, 1);
Pen Invpen = new Pen(Color.White, 1);
public Ziffernblatt() //Graphics NgObject)
{
//gObject = NgObject;
}
public void Draw(Graphics g, int PosX, int PosY)
{
RectSize.Width = PosX / 2;
RectSize.Height = PosY / 2;
RectPoint.X = PosX / 2 - RectSize.Width / 2;
RectPoint.Y = PosY / 2 - RectSize.Height / 2;
Myrect = new Rectangle(RectPoint, RectSize);
MyCircle = new Rectangle(RectPoint, RectSize);
g.DrawRectangle(Pens.Red, Myrect);
g.DrawEllipse(Pens.Black, MyCircle);
}
}
For additional information about the paint event see Control.Paint Event