how to create node on the panel?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
hey guys how can I change this code and make the Nodes to create on the panel instead of form???

<pre class="prettyprint using System;


namespace WindowsFormsApplication6
{
public partial class Form12 : Form
{
public List<MyNode> _Node = new List<MyNode>();
Random _rnd = new Random();
private MyPanel panel1 = new MyPanel();

public Form12()
{
InitializeComponent();

panel1.Location = new Point(269, 46);
panel1.Size = new Size(this.ClientSize.Width - 285, this.ClientSize.Height - 85);
this.Controls.Add(panel1);



this.Paint += new PaintEventHandler(Form1_Paint);
this.MouseDown += new MouseEventHandler(Form1_MouseDown);

}


private void CreateRandomNode()
{
if (_Node == null)
_Node = new List<MyNode>();

for (int i = 0; i < Convert.ToInt64(textBox1.Text); i++)
{

int Rh;
int Rs;
int l=6;
int a = Convert.ToInt32(l * 3.5);
int b = Convert.ToInt32(l * 3.75);
int Rx = _rnd.Next(290, 920);
int Ry = _rnd.Next(70, 620);
Rh = Rx - a;
Rs = Ry - b;

PointF LocNde = new PointF(Rx, Ry);
PointF LocRng = new PointF(Rh, Rs);
SizeF SzeNde = new SizeF(l, l);
SizeF SzeRng = new SizeF(l*8, l*8);
Color cl = Color.White;

int j = _rnd.Next(0, 100);
bool h = false;


MyNode el = new MyNode() { LocNde = LocNde ,Color=cl,LocRng = LocRng, Size = SzeNde, Sizes = SzeRng, ID = i, Power = j, isCH = h };

this._Node.Add(el);
if (checkBox1.Checked == true)
{
if (el.Power > 50)
{
el.Color = Color.FromArgb(255, 0, 0, 0);
el.isCH = true;
}

}
}


void Form1_Paint(object sender, PaintEventArgs e)
{
if (_Node != null && _Node.Count > 0)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

foreach (MyNode el in _Node)
el.Render(e.Graphics);
}
}

private void button1_Click(object sender, EventArgs e)
{

CreateRandomNode();
this.Refresh();



}

public class Ball : IDisposable
{
public PointF Location { get; set; }
public SizeF Size { get; set; }
private System.Drawing.Drawing2D.LinearGradientBrush brush;


private float r;

public float IncrementX { get; set; }
public float IncrementY { get; set; }
public float RotationAdd { get; set; }

public Ball()
{
this.IncrementX = this.IncrementY = 2;
this.RotationAdd = 2;
this.Size = new SizeF(100, 100);
this.brush = new System.Drawing.Drawing2D.LinearGradientBrush(new RectangleF(0, 0, Size.Width, Size.Height), Color.Red, Color.Yellow, 0F);
}

public Ball(SizeF size, Color c1, Color c2, float increment)
{
this.IncrementX = this.IncrementY = increment;
this.RotationAdd = 2;
this.Size = size;
this.brush = new System.Drawing.Drawing2D.LinearGradientBrush(new RectangleF(0, 0, Size.Width, Size.Height), c1, c2, 0F);
}

public void Dispose()
{

brush.Dispose();
}

internal void Render(Graphics g)
{

r += RotationAdd;


brush.TranslateTransform(-brush.Rectangle.Width / 2f, -brush.Rectangle.Height / 2f);
brush.RotateTransform(r, System.Drawing.Drawing2D.MatrixOrder.Append);
brush.TranslateTransform(brush.Rectangle.Width / 2f, brush.Rectangle.Height / 2f, System.Drawing.Drawing2D.MatrixOrder.Append);


g.TranslateTransform(Location.X, Location.Y);
g.FillEllipse(brush, new RectangleF(new Point(0, 0), Size));


brush.ResetTransform();
g.ResetTransform();
}
}



private void button2_Click_1(object sender, EventArgs e)
{
panel1.Visible = true;
}
public class MyPanel : Panel
{
private System.Windows.Forms.Timer t1 = new System.Windows.Forms.Timer();
public List<Ball> Balls { get { return _balls; } }
private List<Ball> _balls = new List<Ball>();

public MyPanel()
{
this.BackColor = Color.FromArgb(255,0,0,0);
this.DoubleBuffered = true;

t1.Interval =1;
t1.Tick += new EventHandler(t1_Tick);



AddOneBall(new Size(10, 10), Color.GreenYellow, Color.White);

}
private void AddOneBall(System.Drawing.Size size, Color color, Color color_2)
{



Ball b = new Ball(size, color, color_2, 9);
b.RotationAdd = 12;
this._balls.Add(b);

if (t1.Enabled == false)
t1.Start();
}

private void AddOneBall()
{
Ball b = new Ball();
this._balls.Add(b);

if (t1.Enabled == false)
t1.Start();
}



protected override void OnHandleDestroyed(EventArgs e)
{
if (t1 != null)
t1.Dispose();

if (_balls != null)
{
for (int i = _balls.Count - 1; i >= 0; i--)
{
_balls.Dispose();
}

_balls.Clear();
}
}

void t1_Tick(object sender, EventArgs e)
{
t1.Stop();


if (_balls != null)
{
for (int i = 0; i < _balls.Count; i++)
{
float x = _balls.Location.X + this._balls.IncrementX;
float y = _balls.Location.Y + this._balls.IncrementY;


if (x >= this.ClientSize.Width - _balls.Size.Width || x <= 0)
this._balls.IncrementX *= -1;

if (y >= this.ClientSize.Height - _balls.Size.Height || y <= 0)
this._balls.IncrementY *= -1;

_balls.Location = new PointF(x, y);
}
}


this.Invalidate();

if (t1.Enabled == false)
t1.Start();
}

protected override void OnPaint(PaintEventArgs e)
{
if (_balls != null)
{

e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;


for (int i = 0; i < _balls.Count; i++)
{
_balls.Render(e.Graphics);
}
}
}
}


public class MyNode
{
public PointF LocNde { get; set; }
public PointF LocRng { get; set; }
public SizeF Size { get; set; }
public Color Color { get; set; }
public Color Colors { get; set; }
public float PenWidth { get; set; }
public float PenWidthRnge { get; set; }
public int ID { get; set; }
public int Power { get; set; }
public bool isCH { get; set; }
public SizeF Sizes { get; set; }

public MyNode()
{
LocNde = new PointF(0, 0);
LocRng = new PointF(0, 0);
Size = new SizeF(100, 100);
Sizes = new SizeF(300, 100);
Color = Color.Black;
Colors = Color.Black;

}

public void Render(Graphics g)
{

using (SolidBrush p = new SolidBrush(Color))
g.FillEllipse(p, new RectangleF(LocNde, Size));
using (SolidBrush s = new SolidBrush(Color.FromArgb(80, 0, 200, 0)))
g.FillEllipse(s, new RectangleF(LocRng, Sizes));

}

}[/code]
<br/>


View the full article
 
Back
Top