samedi 30 mars 2019

how can I draw rectangles with random height and color?

I'm building app that shows random histogram by clicking on button. All rectangles of histogram should have different random colors and values from -100 to 100, for example.

I already have Y and X axis and some variables for the future.


Pen coord = new Pen(Color.Black);


private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            width = 350;
            g.TranslateTransform(50, 200);
            g.DrawLine(coord, 0, -200, 0, 210); //Y axis
            g.DrawLine(coord, 0, 0, 700, 0); //X axis

tried to add generation of random colors and draw rectangles like that. I understand that I need to add loop, but it doesn't work properly, because rectangles are drawn upside down when I generate Y axis coordinates for them. Also they are all the same and when I'm trying to change their color or height I'm getting some errors.

Random randcolor = new Random(); 
randcolor.Next(0, 256); 
SolidBrush painter = new SolidBrush(Color.FromArgb(randcolor.Next(256), randcolor.Next(256), randcolor.Next(256))); 
Rectangle rect = new Rectangle(0, 0, 50, 100); 
e.Graphics.FillRectangle(painter, rect); 

Probably I need to create List for rectangles and store them there. I need some parts of code to make this program work properly because i still have some problems with syntax and understanding with work of Lists.




Aucun commentaire:

Enregistrer un commentaire