Home > Blockchain >  Delphi Paint box glitching after color change
Delphi Paint box glitching after color change

Time:09-23

I have a Paint Box component set up on my form, with its OnPaint event looking like this:

procedure TForm1.PBPaint(Sender: TObject);
var
  Bean: TBean;
begin
  PB.Color := $008800;
  Bean := Snake.Head;
  while not (Bean = Snake.Tail) do
  begin
    PB.Canvas.FillRect(TRect.Create(Bean.x * 30, Bean.y * 30, Bean.x * 30   30, Bean.y * 30   30));
    Bean := Bean.Next;
  end;
  PB.Canvas.FillRect(TRect.Create(Bean.x * 30, Bean.y * 30, Bean.x * 30   30, Bean.y * 30   30));
  //PB.Color := $000055;
  PB.Canvas.FillRect(TRect.Create(Fruit.x * 30, Fruit.y * 30,Fruit.x * 30   30,Fruit.y * 30   30));
end;

It's supposed to render the snake parts in green and the fruit in red. But if I change the color before filling the part with the fruit then everything becomes red instead, and starts glitching.

Why does this happen and is there a way for me to fill 2 areas with different colors without causing this issue? I believe I am using the latest version of free delphi, if that helps

CodePudding user response:

You should be setting the canvas brush colour,

PB.Canvas.Brush.Color := ...              // Yes

not the control's colour:

PB.Color := ...                           // No
  • Related