Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

graphics in c#

Status
Not open for further replies.

christhedonstar

Programmer
Apr 9, 2007
215
GB
Hi,

If I have a bunch of objects and I want them to have graphics which draw onto a form. How do I make that happen?

In Java Swing (a while back) you could make objects extend JComponent and rectangle. And then use the paintMe method to draw.

What do you do in c#?

Cheers,

Chris
 
Ok thanks. I already can draw to the form by overriding the OnPaint method using System.Drawing assembly.

What I want to know is that if I have lets say a Person object say Person chris = new Person()

and I want to have an image associated with chris, which would be drawn onto the form, what would be the best way to do that?

PS that example was completely made up.

Thanks,

Chris
 
I am not sure I'm understanding you correctly, or what the best way to do this would be but..

Couldn't you have a property in your objects for what image to show or draw?
Or have a switch statment if this object draw this ect...

Ordinary Programmer
 
Yeah I would do. Its more about how to get the objects to draw to the form.
 
Make your object (Person or whatever) inherit from System.Windows.Forms.UserControl. This will give you the OnPaint method which you can override, as well as a load of other methods/properties.

In the OnPaint method you can use the e parameter to paint things.

Code:
public partial class Person : UserControl
{
    public Person()
    {
        InitializeComponent();

        SetStyle(
            ControlStyles.AllPaintingInWmPaint |
            ControlStyles.OptimizedDoubleBuffer |
            ControlStyles.UserPaint,
            true);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        e.Graphics.FillEllipse(new SolidBrush(this.ForeColor), this.ClientRectangle);
    }
}

I added the ControlStyles as you would probably want these in there.

Then you would just initialise it as you would any other control object - and it will appear in your toolbox too as a usercontrol that you can drag onto your form, providing you don't have any parameters in your constructor. Or you could just create an instance at runtime as with any other control.

Code:
private void Form1_Load(object sender, EventArgs e)
{
    Person p = new Person();
    this.Controls.Add(p);
}
 
Thanks I may try that. What I did in the end was have a file with this in it:

public static Graphics FORMGRAPHICS;

Then when the form loaded in the constructor I put
FORMGRAPHICS = this.CreateGraphics();

It worked in a test. What do you think to that approach versus yours?

cheers,

Chris
 
That sounds fine although it won't automatically paint to the form when it is added, unless you code it obviously.

Also - what if the object (Person?) is added to a list, or a panel, etc. Wyou might need to cater for this differently.
 
Using a picturebox you can import graphics (say chris.png) into the app, either directly, or from a resource file, and then call them back as the image to be displayed :

pictureBox1.Image = Properties.Resources.chris;



______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Yeah PG001 thats what I was thinking I need to paint stuff in a different place when a new person gets added. I'm not sure how I'll do that yet, maybe I'll put draw coordinates in the constructor of the person...
 
I would keep the business entity as they are, as part of the model layer. Then I would use some sort of controller that knows how to render a certain entity to any view (even to a printer). The view triggers when to paint the object (via Paint event?) then requests the controller to paint the object's image.
Code:
class Person

class PersonPainter
  - Person subject;
  - Draw(Graphics deviceContext, Point location);

[COLOR=green]// use them like...[/color]

Person me = new Person();
PersonPainter painter = new PersonPainter();

void FormLoad()
{
  painter.subject = me;
}

void FormPaint(object sender, PaintEventArgs e)
{
  painter.Draw(e.Graphics, new Point(100,100));
}
There may be other variations but try to keep the division between the data/model and UI/view. This will make your design neat, flexible, easy to maintain.

just my 2cents [wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top