Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to change color of ball When the ball collides on the edges of the form,From each collision its color should become random,and this color should be maintained until the second clash has occurred.
Anyone can help me please?

What I have tried:

public partial class Form2 : Form
    {
        Ball ball;
        Graphics g;
        public Form2()
        {
            InitializeComponent();
            g = this.CreateGraphics();
            ball = new Ball(80, 80, 30,30, g, this);

        }

        private void Form2_Paint(object sender, PaintEventArgs e)
        {
            ball.draw();
        }
    }



class Ball
    {
        public int x;
        public int y;
        public int r;
        public int s;
        public Graphics g;
        public Thread td;
        public Form2 fr;
        public SolidBrush myvariable;
        public Random rnd;
        public Ball(int x, int y, int r,int s, Graphics g, Form2 fr)
        {
            this.fr = fr;
            this.x = x;
            this.y = y;
            this.r = r;
            this.s = s;
            this.g = g;
            td = new Thread(run);
            td.Start();
        }
        public void draw()
        {
             myvariable = new SolidBrush(Color.FromArgb(rnd.Next(150), rnd.Next(205), rnd.Next(160)));
            g.FillEllipse(myvariable, x, y, r, s);
            
        }
        
        public void run()
        {
            for (;;)
            {
                this.move();
                Thread.Sleep(50);
            }
        }
        public void move()
        {
            int newball_x = x + r;
            int newball_y = y + s;
            if (newball_x < -5 || newball_x > this.fr.ClientSize.Width)
            {
                r = -r;
                
            }
            if (newball_y < 0 || newball_y > this.fr.ClientSize.Height)
            {
                s = -s;
            }
            x += r;
            y += s;
            this.fr.Invalidate();
        }
    }
Posted
Updated 9-Mar-18 2:10am
v5
Comments
F-ES Sitecore 8-Mar-18 9:21am    
You're persisting the balls coordinates already so persist its colour the same way. For generating random colours google "c# generate random colour"
Suren97 8-Mar-18 9:26am    
i'm not imagine how can i do it?

1 solution

Move the SolidBrush definition outside any method, so it becomes part of the class (you should do this anyway, as all Graphics objects are scarce resources and need to be Disposed correctly when you are finished with them - if you don't you will get problems as they run out).
The class then persists the value for you.
When you detect it hit the wall, Dispose the existing instance, and create a new SolidBrush using a random colour: How to choose a random color from System.Drawing.Color?[^]
 
Share this answer
 
Comments
Suren97 8-Mar-18 12:06pm    
Sorry, but i nothing understand
OriginalGriff 8-Mar-18 12:14pm    
What part do you not understand? I'll try and explain that in a bit more detail...
Suren97 8-Mar-18 12:28pm    
can't i write it without class?
Suren97 8-Mar-18 13:16pm    
Well, i updated my code, now can you help me where should i write random color?
OriginalGriff 8-Mar-18 14:03pm    
Nowhere in that.
Once again, you are just guessing aren't you? Your previous version was better...

When I said "Move the SolidBrush definition outside any method, so it becomes part of the class" I Meant just that: move it outside your Paint method so it becomes part of the class that contains the method. A Form is a class. That's why your code says
public partial class Form2 : Form
*Everything* in C# is in a class. There are no global variables or methods, they are all part of one class or another.

I know I've told you this before - yesterday I think - but you must stop just guessing and leaping into code. A few minutes of thinking and planning, working out what you need to do and designing something that covers what you want will make your life a whole load easier. And the whole job a load quicker to boot.

So throw that away, go back to the previous version and start planning instead of just typing and hoping it all sorts itself out!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900