0

I am trying to do the following:

  1. Open a new form and draw few lines and arcs (this is working well).
  2. When an event occurs a new coordinate (x1,y1) is calculated, and a small circle should be drawn at that coordinate.
  3. When the next event occurs, a small circle should be drawn at (x2,y2), and the first circle should disappear, while keeping the lines and arcs that were drawn at step 1.

How do I delete the first circle while keeping all the rest? Thank you

YigalB
  • 87
  • 2
  • 11
  • 1
    See the simple example [here](https://stackoverflow.com/a/53708936/7444103). You just need to keep track of what you draw, store the shapes, remove/add one when needed. (Btw, you're using the `Paint` event / `OnPaint` method to draw your shapes, right?) – Jimi Feb 22 '20 at 21:48
  • Maybe [this](https://stackoverflow.com/a/59340672/10216583) too. –  Feb 22 '20 at 22:10
  • What are you targetting: Winforms, WPF, ASP..? YOU should __always__ TAG your questions correctly so one can see it on the questions page! – TaW Feb 22 '20 at 23:29
  • @TaW: agreed. I use WPF. – YigalB Feb 24 '20 at 19:43

2 Answers2

0

Once a circle has been drawn, there is no way to delete it. So, you need to clear the entire canvas with the background color and redraw everything.

So, you need to:

  1. Come up with a bunch of "shape" classes for representing each line and each ark and each circle. They will probably all derive from some common base "Shape" class which offers methods that are common to all shapes, for example, a method to draw the shape on a canvas.

  2. Instantiate objects from these classes to represent the shapes that are supposed drawn on the screen, and keep these objects in a list throughout the lifetime of your application.

  3. When the event occurs, you make any changes to your shapes, (in your case, remove a circle and add another circle, or, more likely, change the coordinates of an existing circle without removing it and re-inserting it in the list,) and then you need to invalidate the canvas control that you presumably use for painting, (search for "Invalidate" for documentation,) so as to cause the canvas to repaint itself.

  4. You override the paint method of the canvas control so as to do your painting: first you clear the control to its background color, and then you iterate through your list of shapes, calling each shape to draw itself on the canvas.

Of course this will cause flicker; if that's unacceptable, then you will need to read up on how to implement "double buffering" (look it up) to eliminate the flicker.

Another approach for flicker elimination is to only erase and repaint the area that has changed. In your case, that would be the smallest rectangle that contains both the circle in its old location, and the circle in its new location. So, instead of invalidating the entire canvas, you invalidate only that rectangle. The problem with this approach is that other shapes that cross the invalidated area might appear to be redrawn somewhat inaccurately. This may be unacceptable, or it may be acceptable, you will not know unless you try it.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
0

You can't in the way you're thinking of, because the graphics works like MSPaint (a 2d array of pixels and when it's drawn, it's drawn) not Adobe Photoshop (layers or objects that can be moved independently)

The easiest thing to do is implement photoshop-like functionality yourself and keep eg a List of everything you want to draw, draw from the list (the list includes a circle) then later, remove that circle from the list, add another one and redraw the whole canvas from the list

I know it seems wasteful, and you probably could get into the mechanics of saving the pixels you drew over when you draw the first circle and restore them to erase the circle, but it's far more complex than just ditching it all and starting over every time

Caius Jard
  • 72,509
  • 5
  • 49
  • 80