Drawing a graph

S

Steve

Hey,

I would like to create some random data points at program start (preferably
not with a mouse click or some other user event) and then draw them on a
form. But if I create the points in the Paint event handler, it
reinitializes and redraws the points every time I resize the form (see
below). How can I create one set of points and then simply pass them to
OnPaint? Thanks for your help. Steve

public void OnPaint(object obj, PaintEventArgs e)

{

Graphics g = e.Graphics;


Random num = new Random();

Point[] points = new Point[500];


for (int i=0; i<points.GetLength(0); i++)

{

points.X = i;

points.Y = num.Next(0, 200);

}

Pen pen = new Pen(Color.Green);

g.DrawLines(pen, points);


}
 
D

Derek Harmon

Steve said:
But if I create the points in the Paint event handler, it
reinitializes and redraws the points every time I resize the form (see
below). How can I create one set of points and then simply pass them to
OnPaint? : :
public void OnPaint(object obj, PaintEventArgs e) : :
Point[] points = new Point[500];

You are creating the points as a local variable in the OnPaint
method. That's why they continuously get "washed away"
everytime OnPaint is called by the Framework.

I'd recommend moving the array (or any information you want
to retain) outside of the OnPaint, perhaps make it a field or
property of the Form or Control that is doing the painting.

// Point array declared as a field.
private Point[] m_points;
:
:
public void OnPaint( object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;

// Initialize Points array on the first-time only.
if ( null == m_points )
{
Random num = new Random( );
this.m_points = new Point[ 500];

for ( int i = 0; i < this.m_points.Length; ++i)
{
this.m_points[ i].X = i;
this.m_points[ i].Y = num.Next( 0, 200);
}
}

g.DrawLines( Pens.Green, this.m_points);
}


I've also used the built-in system Pens collection, instead of
creating a new Pen object on each call to OnPaint (because
the system can run out of pen resources). If you must create
a Pen (for example, you're changing it's line style or thickness)
dynamically, always be sure to call Dispose( ) on it before it
leaves scope.


Derek Harmon
 
S

Steve Magoon

Thanks Derek!


Derek Harmon said:
But if I create the points in the Paint event handler, it
reinitializes and redraws the points every time I resize the form (see
below). How can I create one set of points and then simply pass them to
OnPaint? : :
public void OnPaint(object obj, PaintEventArgs e) : :
Point[] points = new Point[500];

You are creating the points as a local variable in the OnPaint
method. That's why they continuously get "washed away"
everytime OnPaint is called by the Framework.

I'd recommend moving the array (or any information you want
to retain) outside of the OnPaint, perhaps make it a field or
property of the Form or Control that is doing the painting.

// Point array declared as a field.
private Point[] m_points;
:
:
public void OnPaint( object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;

// Initialize Points array on the first-time only.
if ( null == m_points )
{
Random num = new Random( );
this.m_points = new Point[ 500];

for ( int i = 0; i < this.m_points.Length; ++i)
{
this.m_points[ i].X = i;
this.m_points[ i].Y = num.Next( 0, 200);
}
}

g.DrawLines( Pens.Green, this.m_points);
}


I've also used the built-in system Pens collection, instead of
creating a new Pen object on each call to OnPaint (because
the system can run out of pen resources). If you must create
a Pen (for example, you're changing it's line style or thickness)
dynamically, always be sure to call Dispose( ) on it before it
leaves scope.


Derek Harmon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top