How can I use method in class that I created?

  • Thread starter Thread starter John
  • Start date Start date
J

John

I wrote this class

class CLine
{
public float sX,sY,dX,dY,m,x,y;
public void Draw (PaintEventArgs g)
{
//Graphics g = this.CreateGraphics
();
Bitmap bm=new Bitmap(1,1);
bm.SetPixel(0,0,Color.Black);
m = (dY-sY)/(dX-sX);
if (m<=1)
{
for (x=sX;x<=dX;x++)
{
y=sY+(x+sX)*m;

g.Graphics.DrawImageUnscaled(bm,Convert.ToInt32(x)
+512,-1*Convert.ToInt32(Math.Round(y))+384);
}
}
else
{
for (y=sY;y<=dY;y++)
{
x=sX+(y+sY)*m;

g.Graphics.DrawImageUnscaled(bm,Convert.ToInt32
(Math.Round(x))+512,-1*Convert.ToInt32(y)+384);
}
}
}
}

What should I do if I wanna call Draw method? I've tried
below code but it doesn't work.

CLine first = new CLine();
first.sX = 0; first.sY = 0; first.dX = 300; first.dY = -
500;
first.Draw(); <== Problem's here!

Thank in advance for your help.
 
The only problem I see here is that you call method

public void Draw (PaintEventArgs g)

without a PaintEventArgs object:
 
which is the problem ? do you got an error ?

class CLine has no constructor, you could create a constructor an pass your
members value through it...
 
Draw expects to be passed a PaintEventArgs. You can get this from
OnPaint, or you can rewrite it to use a graphics object and create the
graphics object before calling Draw.

Graphics g = this.CreateGraphics(); // this assumes you are calling draw
from a windows form/control
Draw(g);
g.Dispose();

rewrite Draw to
public void Draw(Graphics g)

and change all references to g.Graphics in Draw to g
 
So how to fixed it?
-----Original Message-----
The only problem I see here is that you call method

public void Draw (PaintEventArgs g)

without a PaintEventArgs object:

.
 
Yeah, I got an error. I think I can pass member value by
use this line, "first.sX = 0; first.sY = 0; first.dX =
300; first.dY = - 500;". So how can I fix it?
 
private void YourPaintEvent(object sender,
System.Windows.Forms.PaintEventArgs e)
{
CLine first = new CLine();
first.Draw(e.Graphics);
}
 
Back
Top