ThePhillipParadox
ThePhillipParadox

Reputation: 171

Create a Graphics Method in a Class?

Okay, so I'm creating a class in C#. It works fine, so I'm adding more methods. This is meant to port some BASIC commands as methods into C#. I want to create a method that'll automatically do graphics without having to manually set up pens, points, graphics, etc. Just type in a command. The problem is that...well...it won't work. When I compile, it runs, but it throws an exception when I call the method (Object reference not set to an instance of an object). I know what the exception is telling me, but I can't figure out how to fix it. Here's my current code:

    Graphics g;

    public void gpset(int x1, int y1, string colour1)
    {
        Pen myPen = new Pen(Color.FromName(colour1));
        g.DrawLine(myPen, x1, y1, x1 + 1, y1 + 1);
        myPen = new Pen(Color.White);
        g.DrawLine(myPen, x1 + 1, y1, x1 + 1, y1 + 1);
        myPen.Dispose();
        g.Dispose();
    }

    public void gline(int x1, int y1, int x2, int y2, string colour1)
    {
        Pen myPen = new Pen(Color.FromName(colour1));
        g.DrawLine(myPen, x1, y1, x2, y2);
        myPen.Dispose();
        g.Dispose();
    }

    public void gbox(int x1, int y1, int x2, int y2, string colour1)
    {
        Pen myPen = new Pen(Color.FromName(colour1));
        g.DrawRectangle(myPen, x1, y1, x2, y2);
        myPen.Dispose();
        g.Dispose();
    }

Since this didn't work, I tried to do this instead of Graphics g;

    PaintEventArgs e;
    Graphics g = e.Graphics();

Now it simply won't compile. It says A field initializer cannot reference the non-static field, method, or property 'SmileB.SmileB.e'.

I've also tried doing:

    PaintEventArgs e;

    //Later in the code:
    method stuff here()
    {
        other stuff here;
        e.Graphics.<command>;
    }

But this doesn't seem to work either. Help? Is this even possible to do? Also, I've run these methods directly inside of a forms application, and they work, so the methods themselves seem not to be the problem.

EDIT: Also, is there a better way to do the gpset method? It should just create one pixel of colour.

EDIT EDIT: Here's how I'm declaring the class:

    using SmileB;
    namespace Drawing_Test
    {
        public partial class Form1 : Form
        {
            SmileB.SmileB ptc = new SmileB.SmileB();

            //Down here is where I use the code
        }
    }

Upvotes: 1

Views: 2178

Answers (1)

payo
payo

Reputation: 4561

It appears that e is never defined. Looks like you are trying to use the PaintEventArgs object, commonly named e, to access your Grahpics object. You need to overload OnPaint to get this event object. You can't simply make your own.

Also, the Graphics object is disposed after a paint event, you should not be holding on to it. Only use it locally within the scope of the OnPaint event.

EDIT You asked for me details. The point is, you shouldn't be making your own PaintEventArgs, nor your own Graphics. You should hook into OnPaint to get these objects.

public partial class SomeForm : Form
{
  protected override void OnPaint(PaintEventArgs e)
  {
    Graphics g = e.Graphics;

    // some code to define x1, y1, colors, etc
    gpset(g, x1, y1, color1);
    gline(g, x1, y1, x2, y2, color1);
    gbox(g, x1, y1, x2, y2, color1);
  }

  public void gpset(Graphics g, int x1, int y1, string colour1)
  {
    Pen myPen = new Pen(Color.FromName(colour1));
    g.DrawLine(myPen, x1, y1, x1 + 1, y1 + 1);
    myPen = new Pen(Color.White);
    g.DrawLine(myPen, x1 + 1, y1, x1 + 1, y1 + 1);
    myPen.Dispose();
    g.Dispose();
  }

  public void gline(Graphics g, int x1, int y1, int x2, int y2, string colour1)
  {
    Pen myPen = new Pen(Color.FromName(colour1));
    g.DrawLine(myPen, x1, y1, x2, y2);
    myPen.Dispose();
    g.Dispose();
  }

  public void gbox(Graphics g, int x1, int y1, int x2, int y2, string colour1)
  {
    Pen myPen = new Pen(Color.FromName(colour1));
    g.DrawRectangle(myPen, x1, y1, x2, y2);
    myPen.Dispose();
    g.Dispose();
  }

}

If you need to do this from another object NOT DURING a paint event, please read about Creating a Graphics object.

If you are making this draw method from another object during a paint event, just pass the Grahpics you get from its PaintEventArgs to these methods.

Upvotes: 2

Related Questions