Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
hey guys how can I change this code and make the Nodes to create on the panel instead of form???
 

 

 

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[i].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[i].Location.X + this._balls[i].IncrementX;
                        float y = _balls[i].Location.Y + this._balls[i].IncrementY;
 

                        if (x >= this.ClientSize.Width - _balls[i].Size.Width || x <= 0)
                            this._balls[i].IncrementX *= -1;
 
                        if (y >= this.ClientSize.Height - _balls[i].Size.Height || y <= 0)
                            this._balls[i].IncrementY *= -1;
 
                        _balls[i].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[i].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));
            
        }
 
}
Posted 2-Nov-12 22:52pm
Edited 3-Nov-12 0:02am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you want the nodes in the panel, you have to add them to the control collection of the panel. You are directly drawing them on the form... which is why they are ending up there.
 
You are creating the panel in code and adding it to the form's control's collection. You should be doing the same thing with the nodes (and the panel's controls collection) if you want them to be children of the panel. Of course, you will know that the coordinates specified for the nodes will be relative to the panel's top-left corner.
  Permalink  
Comments
Marcus Kramer at 3-Nov-12 14:55pm
   
+5.
rasoolkhalife at 4-Nov-12 8:18am
   
could you change the code? for me tnx?
Marcus Kramer at 4-Nov-12 13:24pm
   
That part is for you to do.
rasoolkhalife at 11-Nov-12 12:20pm
   
Hey guys new Q how can I make the Nodes that if the ball was on each of them I mean if the Ball passed from each of them the color of that node changes???
Jason Gleim at 11-Nov-12 16:14pm
   
Each control has a bounding box property which gives you the top-left corner of the control as well as the length and width. You simply need to walk through your nodes collection each time you update the ball's position and test it the ball is within the bounds of one of the nodes. If so, change the color of that node. Remember, by using a for..each over the nodes collection, you get a reference to each node you can do the math against then change the color.
 
More generally, this is called collision detection. If you look at some of the game development tutorials, they all address collision detection and talk about implementing an algorithm that will work for your approach. You might dive into some of the XNA tutorials especially will cover this and might help you improve your structure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 643
1 OriginalGriff 550
2 CPallini 415
3 George Jonsson 231
4 ChauhanAjay 150
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,032
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 3 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100