PDA

View Full Version : آموزش: رسم موج آب



مصطفی ساتکی
دوشنبه 02 خرداد 1390, 08:07 صبح
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const N =500;
N2 = 250;
var
Form1: TForm1;

implementation

{$R *.dfm}
procedure DrawWave(Bit : TBitmap;Ticks : Integer);
var x,y : Integer;
p : PByte;
Gray : byte;
d : Double;
dy ,dx : Integer;
begin

for y := 0 to Bit.Height-1 do
begin
dy := y - N2;
p := Bit.ScanLine[y];
for x := 0 to Bit.Width-1 do
begin
dx := x - N2;
d := sqrt( dx*dx + dy*dy);
Gray :=round( 128 + 127 *(cos(d/10-Ticks/7)/ (d/10 +1)));
p^ := Gray;
inc(p);
p^ := Gray;
inc(p);
p^ := Gray;
inc(p);
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var bit : TBitmap;
i : Integer;
begin
bit := TBitmap.Create;
with bit do
begin
PixelFormat := pf24bit;
Width := N;
Height := N;


end;

for i := 0 to 200 do
begin
DrawWave(bit,50+i);
Canvas.Draw(0,0,bit);
Application.ProcessMessages;

end;

end;

end.

Felony
دوشنبه 02 خرداد 1390, 08:23 صبح
Delphi_CAT جان این نمونه سورس ها رو در بخش معرفی سورس ها هم قرار بده تا بعدا کاربران دیگه بتونن پیداشون کنن و ازشون استفاده کنن ، در ضمن یادت رفته شئ bit که از کلاس TBitmap مشتق کردی رو بعد از اتمام کار در دکمه آزاد کنی ، کد مروبط به رویداد OnClick شئ Button1 باید به صورت زیر در بیاد :

procedure TForm1.Button1Click(Sender: TObject);
var
Bit: TBitmap;
i: Integer;
begin
Bit := TBitmap.Create;
try
with Bit do
begin
PixelFormat := pf24bit;
Width := N;
Height := N;
end;

for i := 0 to 200 do
begin
DrawWave(Bit, 50 + i);
Canvas.Draw(0, 0, Bit);
Application.ProcessMessages;
end;
finally
Bit.Free;
end;
end;