Download des Projekts: AntiAliasing.zip
unit uAnalogUhr2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,uTUhr2, ExtCtrls;
type
TForm2 = class(TForm)
eH: TEdit; eMin: TEdit; eSec: TEdit; L1: TLabel; L2: TLabel;
bSetzen: TButton; lAusgabe: TLabel; IUhr: TImage;
procedure bAuslesenClick(Sender: TObject);
procedure bSetzenClick(Sender: TObject);
procedure bIncSecClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
b1,b2 : TBitmap;
public
procedure aktualisiereAusgabe;
end;
var
Form2: TForm2;
implementation
{$R *.DFM}
procedure zeichneUhr;
var
mx,my,r,ax,ay,ex,ey : word;
i,h,min,sec : byte;
wh,wmin,wsec : real;
P1,P2,P3 : PByteArray;
x,y : integer;
procedure Uhr2Winkel(h,min,sec : byte; var wh,wmin,wsec : real);
begin
h := h mod 12; // 24h-Anzeige --> 12h-Anzeige
wh := pi/2 - 2*pi/12/60*(60*h+min);
wmin := pi/2 - 2*pi/60*min;
wsec := pi/2 - 2*pi/60*sec;
end;
begin
mx := Form2.b1.Width div 2; my := Form2.b1.Height div 2;
if mx < my then r := mx else r := my; r := r-6;
// alles löschen
Form2.b1.canvas.RectAngle(0,0,Form2.b1.width,Form2.b1.height);
// Zifferblatt zeichnen
Form2.b1.canvas.Pen.Width := 2;
for i := 1 to 12 do
begin
Uhr2Winkel(i,0,0,wh,wmin,wsec);
ax := Round(mx+r*0.8*cos(wh)); ay := Round(my-r*0.8*sin(wh));
ex := Round(mx+r*cos(wh)); ey := Round(my-r*sin(wh));
Form2.b1.Canvas.MoveTo(ax,ay); Form2.b1.Canvas.LineTo(ex,ey);
end;
// Zeit auslesen und Winkel berechnen
h := oUhr.GetH; min := oUhr.GetMin; sec := oUhr.GetSec;
Uhr2Winkel(h,min,sec,wh,wmin,wsec);
// Stundenzeiger
Form2.b1.canvas.Pen.Width := 4;
ex := Round(mx+r*0.65*cos(wh)); ey := Round(my-r*0.65*sin(wh));
Form2.b1.Canvas.MoveTo(mx,my); Form2.b1.Canvas.LineTo(ex,ey);
// Minutenzeiger
ex := Round(mx+r*0.9*cos(wmin)); ey := Round(my-r*0.9*sin(wmin));
Form2.b1.Canvas.MoveTo(mx,my); Form2.b1.Canvas.LineTo(ex,ey);
// Sekundenzeiger
Form2.b1.Canvas.Pen.Width := 2;
ex := Round(mx+r*0.98*cos(wsec)); ey := Round(my-r*0.98*sin(wsec));
Form2.b1.Canvas.MoveTo(mx,my); Form2.b1.Canvas.LineTo(ex,ey);
//Antialiasing
with Form2 do
begin
for y := 0 to b2.Height-1 do
begin
P1 := b1.ScanLine[2*y];
P2 := b1.ScanLine[2*y+1];
P3 := b2.ScanLine[y];
for x := 0 to b2.Width-1 do
begin
P3[x*3] := (P1[2*3*x]+P1[2*3*x+3]+P2[2*3*x]+P2[2*3*x+3]) div 4;
P3[x*3+1] := (P1[2*3*x+1]+P1[2*3*x+4]+P2[2*3*x+1]+P2[2*3*x+4]) div 4;
P3[x*3+2] := (P1[2*3*x+2]+P1[2*3*x+5]+P2[2*3*x+2]+P2[2*3*x+5]) div 4;
end;
end;
end;
//Zeichnen
Form2.IUhr.Canvas.Draw(0,0,Form2.b2);
end;
procedure TForm2.aktualisiereAusgabe;
var
hs,mins,secs : string;
begin
hs := IntToStr(oUhr.GetH); if Length(hs) < 2 then hs := '0'+hs;
mins := IntToStr(oUhr.GetMin); if Length(mins) < 2 then mins := '0'+mins;
secs := IntToStr(oUhr.GetSec); if Length(secs) < 2 then secs := '0'+secs;
Form2.lAusgabe.caption := hs+':'+mins+':'+secs;
zeichneUhr;
end;
procedure TForm2.bAuslesenClick(Sender: TObject);
begin
aktualisiereAusgabe;
end;
procedure TForm2.bSetzenClick(Sender: TObject);
var
h,min,sec : byte;
begin
h := StrToInt(eH.text); min := StrToInt(eMin.text); sec := StrToInt(eSec.text);
oUhr.SetH(h); oUhr.SetMin(min); oUhr.SetSec(sec);
end;
procedure TForm2.bIncSecClick(Sender: TObject);
begin
oUhr.IncSec; aktualisiereAusgabe;
end;
procedure TForm2.Timer1Timer(Sender: TObject);
begin
oUhr.IncSec; aktualisiereAusgabe;
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
b1 := TBitmap.Create; b1.PixelFormat := pf24bit;
b1.Width := IUhr.Width*2; b1.Height := IUhr.Height*2;
b2 := TBitmap.Create; b2.PixelFormat := pf24bit;
b2.Width := IUhr.Width; b2.Height := IUhr.Height;
end;
procedure TForm2.FormDestroy(Sender: TObject);
begin
b1.Free; b2.Free;
end;
end.