9 Replies - 470 Views - Last Post: 13 January 2013 - 11:02 AM Rate Topic: -----

#1 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

multiple classes use the graphics parameter?

Posted 13 January 2013 - 06:27 AM

hi all here is my challenge.

Write a program to draw a stick figure. Combine hair,face and body to make the figure. Create 'hair', 'face' and 'body' classes to draw each of these parts. Each class will have a draw method that accepts a graphics parameter. use the graphics object to draw that part. do the drawing relative to the size of the window.

now the previous challenge was to draw a face eyes, nose and mouth but all this is drawn within OnPaint, when i create new class within the program i cannot then use the e.graphics in order to draw the sections.

i dont quite understand because i tried before playing around with the different Onpaintbackground, onmousedown etc and couldnt get the e.graphics either. can anyone help me out with advice please here is the code in previous challenge for the face

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

public class StickFigure : Form
{
    

    public StickFigure()
    {
        Size = new Size(800, 800);
        Text = "Stick Figure";
        BackColor = Color.White;
        
    }
    

        protected override void OnPaint(PaintEventArgs e)
        {

            int w = DisplayRectangle.Width;
            int h = DisplayRectangle.Height;

            Graphics g = e.Graphics;

            //Head
            g.DrawEllipse(Pens.Black, w / 20 * 8, h / 20 * 6, w / 20 * 4, h / 20 * 4);
            //body
            g.DrawLine(Pens.Black, w / 20 * 10, h / 20 * 10, w / 20 * 10, h / 20 * 15);
            //legs
            g.DrawLine(Pens.Black, w / 20 * 10, h / 20 * 15, w / 20 * 7, h / 20 * 18);
            g.DrawLine(Pens.Black, w / 20 * 10, h / 20 * 15, w / 20 * 13, h / 20 * 18);
            //arms
            g.DrawLine(Pens.Black, w / 20 * 10, h / 20 * 12, w / 20 * 7, h / 20 * 12);
            g.DrawLine(Pens.Black, w / 20 * 10, h / 20 * 12, w / 20 * 13, h / 20 * 12);

            //eyes
            g.DrawEllipse(Pens.Black, w / 20 * (int)9, h / 20 * (int)7.5, 20, 20);
            g.DrawEllipse(Pens.Black, w / 20 * (int)10 + 15, h / 20 * (int)7.5, 20, 20);

            //nose
            g.DrawArc(Pens.Black, w / 20 * 10 - 10, h / 20 * 8 - 5, 20, 20, -200, -130);



            //mouth
            g.DrawArc(Pens.Black, w / 20 * 10 - 25, h / 20 * 8, 50, 50, -180, -180);
        
            base.OnPaint(e);
        
    }

    
    static void Main()
    {
        Application.Run(new StickFigure());
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: multiple classes use the graphics parameter?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: multiple classes use the graphics parameter?

Posted 13 January 2013 - 07:40 AM

Quote

use the graphics object to draw that part. do the drawing relative to the size of the window.


Don't over think it. Just pass the graphic object from your main application to each of the other class-methods.

Look at line 21. See how the painteventargs were passed in to that method? Do the same with the Graphics object you made on line 27. Pass that to your other class methods.

As this is homework we can't give you code; just advice. Once you have actually created some code in an effort to do the other classes we can help you with issues.
Was This Post Helpful? 0
  • +
  • -

#3 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: multiple classes use the graphics parameter?

Posted 13 January 2013 - 08:44 AM

thanks and this is all i want the advice :) i am not in education i wish this was homework but im just self learning from a book its a challenge from the book :) thanks i should be able to finish it thanks
Was This Post Helpful? 0
  • +
  • -

#4 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: multiple classes use the graphics parameter?

Posted 13 January 2013 - 08:56 AM

ok here is my attempt i have done and the error it come out with:

public class Face
    {
        PaintEventArgs e;
        Graphics h = [u]e[/u].Graphics;
 }


this brings an error saying "A field initializer cannot reference the non-static field,method or property of StickFigure.Face.e ( i have underlined the e because thats where the error shows)
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: multiple classes use the graphics parameter?

Posted 13 January 2013 - 09:01 AM

You can't just make up a new PaintEventArgs and get the graphics from it. This is an all new object with no relation to your form.

You have to pass the graphics object from the paint method of the form.
Was This Post Helpful? 0
  • +
  • -

#6 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: multiple classes use the graphics parameter?

Posted 13 January 2013 - 09:07 AM

ahh ok here is what i just tried
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

public class StickFigure : Form
{
    

    public StickFigure()
    {
        Size = new Size(800, 800);
        Text = "Stick Figure";
        BackColor = Color.White;
        
    }
    

        protected override void OnPaint(PaintEventArgs e)
        {

            int w = DisplayRectangle.Width;
            int h = DisplayRectangle.Height;

            Graphics g = e.Graphics;

            
            //Head
            g.DrawEllipse(Pens.Black, w / 20 * 8, h / 20 * 6, w / 20 * 4, h / 20 * 4);
            //body
            g.DrawLine(Pens.Black, w / 20 * 10, h / 20 * 10, w / 20 * 10, h / 20 * 15);
            //legs
            g.DrawLine(Pens.Black, w / 20 * 10, h / 20 * 15, w / 20 * 7, h / 20 * 18);
            g.DrawLine(Pens.Black, w / 20 * 10, h / 20 * 15, w / 20 * 13, h / 20 * 18);
            //arms
            g.DrawLine(Pens.Black, w / 20 * 10, h / 20 * 12, w / 20 * 7, h / 20 * 12);
            g.DrawLine(Pens.Black, w / 20 * 10, h / 20 * 12, w / 20 * 13, h / 20 * 12);

            //eyes
            g.DrawEllipse(Pens.Black, w / 20 * (int)9, h / 20 * (int)7.5, 20, 20);
            g.DrawEllipse(Pens.Black, w / 20 * (int)10 + 15, h / 20 * (int)7.5, 20, 20);

            //nose
            g.DrawArc(Pens.Black, w / 20 * 10 - 10, h / 20 * 8 - 5, 20, 20, -200, -130);



            //mouth
            g.DrawArc(Pens.Black, w / 20 * 10 - 25, h / 20 * 8, 50, 50, -180, -180);

            Facefigure(g);
            base.OnPaint(e);
            
    }
    public class Facefigure
    {
        
 }
   
    static void Main()
    {
        Application.Run(new StickFigure());
        
    }
}


i tried passing g into facefigure (line 53) but it obviously doesn't exist in that context. am i supposed to create the new class within protect override OnPaint ?

This post has been edited by bboyzeez: 13 January 2013 - 09:07 AM

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: multiple classes use the graphics parameter?

Posted 13 January 2013 - 09:10 AM

Quote

i supposed to create the new class within protect override OnPaint


Stop. If you don't understand classes then you can't make classes.
Classes can't be made within methods. That's like saying I'm going to make a car inside the engine: Its backwards.

There is a tutorial just on classes and objects linked in my signature block. You should do it, then come back to this.
Was This Post Helpful? 0
  • +
  • -

#8 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: multiple classes use the graphics parameter?

Posted 13 January 2013 - 10:35 AM

ok i read your tutorial thanks here is what i got i created the classes but dont see where i am supposed to call each class in order to draw, i was guessing it was within the main class OnPaint method but it didnt work?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

public class StickFigure : Form
{
    

    public StickFigure()
    {
        Size = new Size(800, 800);
        Text = "Stick Figure";
        BackColor = Color.White;
        
    }
    

        protected override void OnPaint(PaintEventArgs e)
        {

            int w = DisplayRectangle.Width;
            int h = DisplayRectangle.Height;

            Graphics g = e.Graphics;

            
            base.OnPaint(e);
            
    }
    
   
    static void Main()
    {
        Application.Run(new StickFigure());
        
    }
}

public class DrawFace : StickFigure
{
    protected override void OnPaint(PaintEventArgs e)
    {
        int w = DisplayRectangle.Width;
        int h = DisplayRectangle.Height;
        Graphics g = e.Graphics;

        //Head
        g.DrawEllipse(Pens.Black, w / 20 * 8, h / 20 * 6, w / 20 * 4, h / 20 * 4);
        //eyes
        g.DrawEllipse(Pens.Black, w / 20 * (int)9, h / 20 * (int)7.5, 20, 20);
        g.DrawEllipse(Pens.Black, w / 20 * (int)10 + 15, h / 20 * (int)7.5, 20, 20);

        //nose
        g.DrawArc(Pens.Black, w / 20 * 10 - 10, h / 20 * 8 - 5, 20, 20, -200, -130);

        //mouth
        g.DrawArc(Pens.Black, w / 20 * 10 - 25, h / 20 * 8, 50, 50, -180, -180);


        Invalidate();
        base.OnPaint(e);
    }

}

public class Body : StickFigure
{

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        int w = DisplayRectangle.Width;
        int h = DisplayRectangle.Height;
        //body
        g.DrawLine(Pens.Black, w / 20 * 10, h / 20 * 10, w / 20 * 10, h / 20 * 15);
        //legs
        g.DrawLine(Pens.Black, w / 20 * 10, h / 20 * 15, w / 20 * 7, h / 20 * 18);
        g.DrawLine(Pens.Black, w / 20 * 10, h / 20 * 15, w / 20 * 13, h / 20 * 18);
        //arms
        g.DrawLine(Pens.Black, w / 20 * 10, h / 20 * 12, w / 20 * 7, h / 20 * 12);
        g.DrawLine(Pens.Black, w / 20 * 10, h / 20 * 12, w / 20 * 13, h / 20 * 12);
        base.OnPaint(e);
    }
}



Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: multiple classes use the graphics parameter?

Posted 13 January 2013 - 10:53 AM

Hoped the tutorials helped.
But now you need to go back and re-read the instructions because you're not designing as instructed

Quote

Each class will have a draw method that accepts a graphics parameter.


class Head
{
   public void Draw(Graphics g)
   {
      // Do drawing here
   }
}

class Body
{
   public void Draw(Graphics g)
   {
      // Do drawing here
   }
}


In other words - there was no instruction to inherit from stickman and nothing about overriding OnPaint
Was This Post Helpful? 0
  • +
  • -

#10 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: multiple classes use the graphics parameter?

Posted 13 January 2013 - 11:02 AM

ahh haha i just come into here to say wait i found it out ye i got it working now just finishing it off :) thanks for the help and yes it did help , i just focused that much into the question i completly forgot what i have learnt in last few chapters :) once i am finished ill post code up to be sure
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1