How do I make a paint bucket tool (FloodFill?) [SOLVED] lazarus / 39-Graphics ---------------------------------------------------------------------- nsunny · 2010-12-20 07:32 Hi guys, Your previous response has helped me a lot. As I told earlier that I am making a paint program like MS Paint. And the MS Paint has a pretty nice tool which they call it "Fill with color". Which allows to fill a certain area of same color to be filled with the foreground color. I am trying to do this, but failed. PLease look at the screenshot: (http://d.imagehost.org/t/0398/ms-paint-fill-tool-lazarus.jpg) I have tried the following code: Code: [Select]procedure TForm1.Button1Click(Sender: TObject); begin   form1.Canvas.Rectangle(0, 0, 100, 100);   form1.Canvas.Ellipse(50, 50, 100, 100);   form1.Canvas.FloodFill(3, 3, clRed, fsSurface); end; ---------------------------------------------------------------------- fabienwang · 2010-12-20 08:20 before the Canvas.Rectangle (or any form Ellipse, etc.), you have to define the fill color in Canvas.Brush.Color property Regards, FabienWang ---------------------------------------------------------------------- nsunny · 2010-12-20 08:47 Quote from: FabienWang on December 20, 2010, 09:20:21 ambefore the Canvas.Rectangle (or any form Ellipse, etc.), you have to define the fill color in Canvas.Brush.Color property Regards, FabienWang Thank you. But Suppose that I have opened a .bmp/bitmap file containing the rectangle and the circle, then how should I Fill the rectangle? (and not the circle?) ---------------------------------------------------------------------- Leledumbo · 2010-12-20 09:44 QuoteThank you. But Suppose that I have opened a .bmp/bitmap file containing the rectangle and the circle, then how should I Fill the rectangle? (and not the circle?)In that case, let TBitmap draws it for you. Anyway, .bmp is just a map of pixels and is a direct representation of the image, so you just follow the pixels. ---------------------------------------------------------------------- Blaazen · 2010-12-20 22:22 AFAIK this is usually done with recursive algorithm. You have start point with some color and you check colors of neighbouring pixels (left, right, top, down). If color is same (exactly or in some tolerancy) you change it to your color and continue. Because procedure calls itself, there is risk of stack overflow, so the algorithm must be well optimalised. Rather use google, for example on wiki: http://en.wikipedia.org/wiki/Flood_fill ---------------------------------------------------------------------- nsunny · 2010-12-21 04:56 Quote from: Blaazen on December 20, 2010, 11:22:18 pmAFAIK this is usually done with recursive algorithm. ... Ummm... so I will have to write my own algorithm? But what does the FloodFill() do? If I must write an algorithm, how can I make it cross-platform? (plus, it would be better if there is tolerance option. ) ---------------------------------------------------------------------- Blaazen · 2010-12-21 16:21 Now I see there is any FloodFill procedure in unit Graphics. So add "Graphics" to your "uses" section and try it (I didn't). ---------------------------------------------------------------------- mmab · 2010-12-21 23:50 Please read http://en.wikipedia.org/wiki/Flood_fill ---------------------------------------------------------------------- typo · 2010-12-22 01:10 Code: [Select]  Canvas.Brush.Color := clNone;   Canvas.Rectangle(0, 0, 100, 100);   Canvas.Ellipse(0, 0, 50, 50);   Canvas.Brush.Color := clBlack;   Canvas.FloodFill(3, 3, clBlack, fsBorder); ---------------------------------------------------------------------- nsunny · 2010-12-22 02:57 Quote from: typo on December 22, 2010, 02:10:56 amCode: [Select]  Canvas.Brush.Color := clNone;   Canvas.Rectangle(0, 0, 100, 100);   Canvas.Ellipse(0, 0, 50, 50);   Canvas.Brush.Color := clBlack;   Canvas.FloodFill(3, 3, clBlack, fsBorder); I just love u bro!! This worked like a charm. Thanks!!! I see that the second "Canvas.Brush.Color" is the fill color and the Tcolor in the FloodFill is the replacement color. Thank you very much. ---------------------------------------------------------------------- focuz · 2011-01-05 21:15 Yeah thanks, I was looking for this too ---------------------------------------------------------------------- typo · 2011-01-05 21:24 Code: [Select]// replaces any color with clYellow at the mouse position procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;   Shift: TShiftState; X, Y: Integer); begin   Canvas.Brush.Color := clYellow;   Canvas.FloodFill(x, y, Canvas.Pixels[x,y], fsSurface); end; ---------------------------------------------------------------------- iguru · 2011-05-12 16:18 Uaaaa      FloodFill work only on Win32!!! Now i tested on Linux (Kubuntu - Natty) and this not working.