Recent

Author Topic: How do I make a paint bucket tool (FloodFill?) [SOLVED]  (Read 22140 times)

nsunny

  • Full Member
  • ***
  • Posts: 117
  • Code is magic
    • LazPlanet
How do I make a paint bucket tool (FloodFill?) [SOLVED]
« on: December 20, 2010, 08:32:37 am »
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;
« Last Edit: December 22, 2010, 04:41:24 am by nsunny »
Lazarus TTS Tutorial | LazPlanet
Lazarus 2.2.0 | FPC 3.2.2 | Linux/OpenBSD/ReactOS

fabienwang

  • Sr. Member
  • ****
  • Posts: 449
  • Lazarus is the best
    • My blog
Re: How do I make a paint bucket tool (FloodFill?)
« Reply #1 on: December 20, 2010, 09:20:21 am »
before the Canvas.Rectangle (or any form Ellipse, etc.), you have to define the fill color in Canvas.Brush.Color property

Regards,
FabienWang
I'm using Arch Linux.
Known for: CPickSniff, OpenGrabby
Contributed to: LazPaint

nsunny

  • Full Member
  • ***
  • Posts: 117
  • Code is magic
    • LazPlanet
Re: How do I make a paint bucket tool (FloodFill?)
« Reply #2 on: December 20, 2010, 09:47:27 am »
before 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?)
Lazarus TTS Tutorial | LazPlanet
Lazarus 2.2.0 | FPC 3.2.2 | Linux/OpenBSD/ReactOS

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How do I make a paint bucket tool (FloodFill?)
« Reply #3 on: December 20, 2010, 10:44:28 am »
Quote
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?)
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

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: How do I make a paint bucket tool (FloodFill?)
« Reply #4 on: December 20, 2010, 11:22:18 pm »
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
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

nsunny

  • Full Member
  • ***
  • Posts: 117
  • Code is magic
    • LazPlanet
Re: How do I make a paint bucket tool (FloodFill?)
« Reply #5 on: December 21, 2010, 05:56:30 am »
AFAIK this is usually done with recursive algorithm. ...
Ummm... so I will have to write my own algorithm? :o 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. 8-) )
Lazarus TTS Tutorial | LazPlanet
Lazarus 2.2.0 | FPC 3.2.2 | Linux/OpenBSD/ReactOS

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: How do I make a paint bucket tool (FloodFill?)
« Reply #6 on: December 21, 2010, 05:21:12 pm »
Now I see there is any FloodFill procedure in unit Graphics. So add "Graphics" to your "uses" section and try it (I didn't).
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

mmab

  • Jr. Member
  • **
  • Posts: 89
Re: How do I make a paint bucket tool (FloodFill?)
« Reply #7 on: December 22, 2010, 12:50:21 am »

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How do I make a paint bucket tool (FloodFill?)
« Reply #8 on: December 22, 2010, 02:10:56 am »
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

  • Full Member
  • ***
  • Posts: 117
  • Code is magic
    • LazPlanet
Re: How do I make a paint bucket tool (FloodFill?) [SOLVED]
« Reply #9 on: December 22, 2010, 03:57:00 am »
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);
:-* 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.
« Last Edit: December 22, 2010, 04:39:14 am by nsunny »
Lazarus TTS Tutorial | LazPlanet
Lazarus 2.2.0 | FPC 3.2.2 | Linux/OpenBSD/ReactOS

focuz

  • Newbie
  • Posts: 1
Re: How do I make a paint bucket tool (FloodFill?) [SOLVED]
« Reply #10 on: January 05, 2011, 10:15:22 pm »
Yeah thanks, I was looking for this too

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How do I make a paint bucket tool (FloodFill?) [SOLVED]
« Reply #11 on: January 05, 2011, 10:24:59 pm »
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;
« Last Edit: May 12, 2011, 07:28:27 pm by typo »

iguru

  • Newbie
  • Posts: 1
Re: How do I make a paint bucket tool (FloodFill?) [SOLVED]
« Reply #12 on: May 12, 2011, 06:18:09 pm »
Uaaaa  :(  %)  >:(
FloodFill work only on Win32!!! Now i tested on Linux (Kubuntu - Natty) and this not working.

 

TinyPortal © 2005-2018