C# createGraphics, pen only working initialized within panel paint event -


i need draw random shapes random color , random position. figured out how use paint event seems work when initialize pen within paint event

    private void shapespanel_paint(object sender, painteventargs e)     {         _graphics = shapespanel.creategraphics();         _pen = new pen(color.black, 1);     } 

this works, want random colors , every shape has own generated random color.

i have foreach works:

        foreach (var shape in _shapes)         {             shape.drawable(_graphics);         } 

now want have drawing have shapes color:

        foreach (var shape in _shapes)         {             _pen = new pen(shape.color, 3);             shape.drawable(_graphics);         } 

and give no drawings @ all. familiar? thanks

form class

    public partial class shapesform : form {     private shape _shape;      private graphics _graphics;     private pen _pen;      private random _random;      private int _red, _blue, _green;     private color _color;      private int _x, _y;     private point _point;      private int _randomshape;      private double _size;     private double _radius;       private list<shape> _shapes;      public shapesform()     {         initializecomponent();         _shapes = new list<shape>();         _random = new random();     }      private void shapespanel_paint(object sender, painteventargs e)     {         _graphics = shapespanel.creategraphics();         _pen = new pen(color.black, 1);     }      private void addshapebutton_click(object sender, eventargs e)     {         _red = _random.next(0, 255);         _green = _random.next(0, 255);         _blue = _random.next(0, 255);         _color = color.fromargb(1, _red, _green, _blue);          _x = _random.next(0, 100);         _y = _random.next(0, 100);         _point = new point(_x, _y);          _radius = _random.next(0, 20);         _size = _random.next(0, 20);          _randomshape = _random.next(0, 2);          switch(_randomshape)         {             case 0:                 _shape = new circle(_point, _color, _radius);                 _shapes.add(_shape);                 updateshapelistbox();                 drawshapes();                 break;             case 1:                 _shape = new square(_point, _color, _size);                 _shapes.add(_shape);                 updateshapelistbox();                 drawshapes();                 break;         }     }      public void updateshapelistbox()     {         shapeslistbox.items.clear();          foreach (var shape in _shapes)         {             shapeslistbox.items.add(shape.tostring());         }     }      public void drawshapes()     {         shapespanel.refresh();         foreach (var shape in _shapes)         {             _pen = new pen(shape.color, 3);             shape.drawable(_graphics);         }     } } 

this sort of logic want implement:

private void shapespanel_paint(object sender, painteventargs e) {     foreach (var shape in _shapes)     {         shape.drawable(e.graphics);     }     }  // in shape class public void drawable(graphics g) {     var pen = new pen(this.color, 3);     g.drawrect( ... ); // or whatever } 

you should use e.graphics paint handler, , while paint handler running.

the paint handler called whenever necessary. if want repaint because shapes have changed, call shapespanel.invalidate().


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -