Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Justaddwater

C# MyClass > Paint Event !help!

This topic is 5127 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Im fairly new at C# so please bare with a stupid question here, I know how to use the paint event in c# to make lots of things, (lines,polygons,ellipse,etc,etc) Here is my problem though; I would really like to make a class to contain all the information for a given polygon, for example point 1 2 and 3 for a triangle, I want it in a class so I could make more shapes on the screen or go back and move or delete a existing one, but I cant figure out how to set up my class to talk with the paint object here is what I have:

// In my Paint event 


case Design.Ellipse:
{

g.DrawEllipse(Ellpen,DownRecX,DownRecY,UpRecX,UpRecY);
				
}

//Then I have a Ellipse Class I would like to have this info in 

//and be able to call  in my paint in place of the g.drawellipse part



class Ellipse : AllShapes

{
public override void Create(int x, int y, int w, int h)
{
   base.Create (x, y, w, h);
				
}

//And I have no code in that class since I have no idea where

//to start on getting it to work, the AllShape class simplely 

//has

//properties for x y w h and a virtual Create function

 
I hope this makes sence Im just looking for a way to send the information to paint from my class, and ultimatly I will probably have to amke a array in my classes to keep track of the shapes but if i can get the class to interact with paint i should be ok from there, thanks in advance! EDIT: Cleaned Up code [edited by - justaddwater on May 8, 2004 9:05:15 PM]

Share this post


Link to post
Share on other sites
Advertisement
Ultimately, all you really need to make sure of is that your control class (most likely a form it sounds like) has access to your collection of shapes. You create a base class like AllShapes, and give it a virtual Draw function. Then, all inheriting classes have to do is override the virtual function for drawing.

In your form class, keep an array of collection of AllShape types, and in Paint loop through the array and call Draw. Polymorphism will take care of doing the right thing for the right underlying shape instance.

All that being said, you could keep this array in the form class, or in a secondary data class that the form creates an instance of.

Share this post


Link to post
Share on other sites
HI, thanks for your help, I understand what your saying but Im still kinda lost on how to make it work,

Here is what i have so far

DERIVED CLASS -This is just one example there are more



class Polyrec : AllShapes
{


public override void Draw(object sender, PaintEventArgs e)
{
base.Draw (sender, e);
Pen pen = new Pen(Color.Red,5f);
Graphics g = e.Graphics;
g.DrawRectangle(pen,x,y,w,h);

}



This is the BASE CLASS



class AllShapes
{
public int X,Y,W,H;
public int x
{
get
{
return X;
}
set
{
X = value;
}
}
public int y
{
get
{
return Y;
}
set
{
Y = value;
}
}
public int w
{
get
{
return W;
}
set
{
W = value;
}
}
public int h
{
get
{
return H;
}
set
{
H = value;
}
}
public virtual void Draw(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Not sure what to do here

}



This is in my Paint Function, to call the code



case Design.Rectangle:
{
//g.DrawRectangle(Recpen,DownRecX,DownRecY,UpRecX,UpRecY);

//The line above this works to just draw a rectangle, but

//not use the classes


Shape Rec = new Rect();
Polyrec.Draw(DownRecX,DownRecY,UpRecX,UpRecY);

}


Now i have no array yet either, which i see how im missing that from what your explaining, but I cant figure what to send to an array either, maybe that is all I am missing here.

Thanks!

Share this post


Link to post
Share on other sites
What is the switch for in your Paint event? It look should similar to this

class AllShapes
{
private int x, y, w, h;
// ... additional variables and properties

public virtual void Draw(object sender, PaintEventArgs e)
{
// Do anything that applies to all objects

}
}

class Rectangle : AllShapes
{
public override void Draw(object sender, PaintEventArgs e)
{
// Get pen, draw rectangle

g.DrawRectangle(pen,x,y,w,h);
}
}

class Ellipse : AllShaoes
{
public override void Draw(object sender, PaintEventArgs e)
{
// Get pen, draw ellipse

g.DrawEllipse(pen,x,y,w,h);
}
}

// Paint event in form class

private void Paint(object sender, PaintEventArgs e)
{
// We have an array AllShapes[] called myShapes as a member of the form

for(int i = 0; i < myShapes.Length; ++i)
{
myShapes[i].Draw(sender, e);
}
}

Hope that helps you along.

Share this post


Link to post
Share on other sites
I really appreciate the help, i have a much better understanding now thanks to your feedback. I have the switch in my paint so i can do different math depending on what kind of object i want to make,

i.e. a line pt1 --> pt2 vs a square x,y + w etc etc

But i think that if I implement this in the way your saying, I should be able to put that math in the overide functions and just call the array...

And so im sure about this the array I would make it here right?



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

namespace Shapes
{
public enum Design
{
Nil = 0,Circle,Rectangle,Triangle,Square,Line,Ellipse
}


public class Shapes : System.Windows.Forms.Form
{
#region Varibles

private int Count = 0;
//HERE

...


Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!