{Hint: save all files to location: /home/handoko/Desktop/JCanvasPolygon/LamwGUIProject1/jni }
unit unit1;
{$mode delphi}
interface
uses
Classes, SysUtils, AndroidWidget, Laz_And_Controls;
type
TPolygon = array of TPoint;
{ TAndroidModule1 }
TAndroidModule1 = class(jForm)
jButton1: jButton;
jCanvas1: jCanvas;
jTimer1: jTimer;
jView1: jView;
procedure AndroidModule1Destroy(Sender: TObject);
procedure AndroidModule1JNIPrompt(Sender: TObject);
procedure jButton1Click(Sender: TObject);
procedure jTimer1Timer(Sender: TObject);
procedure jView1Draw(Sender: TObject);
private
FSquareX: Integer;
FSquareY: Integer;
FSquareS: Integer;
FSquareVeloX: Integer;
FSquareVeloY: Integer;
FTriangleX: Integer;
FTriangleY: Integer;
FTriangleVeloX: Integer;
FTriangleVeloY: Integer;
procedure DrawPoly(BorderColor: TARGBColorBridge; BorderWidth: Integer;
Points: TPolygon);
end;
var
AndroidModule1: TAndroidModule1;
implementation
{$R *.lfm}
{ TAndroidModule1 }
procedure TAndroidModule1.jView1Draw(Sender: TObject);
var
Polygon: TPolygon;
begin
// Draw Square
Polygon := TPolygon.Create(
Point(FSquareX-FSquareS, FSquareY-FSquareS),
Point(FSquareX+FSquareS, FSquareY-FSquareS),
Point(FSquareX+FSquareS, FSquareY+FSquareS),
Point(FSquareX-FSquareS, FSquareY+FSquareS));
DrawPoly(colbrGreen, 3, Polygon);
// Draw Triangle
Polygon := TPolygon.Create(
Point(FTriangleX, FTriangleY-80),
Point(FTriangleX+100, FTriangleY+80),
Point(FTriangleX-100, FTriangleY+80));
DrawPoly(colbrRed, 3, Polygon);
end;
procedure TAndroidModule1.jButton1Click(Sender: TObject);
begin
jTimer1.Enabled := not(jTimer1.Enabled);
case jTimer1.Enabled of
True : jButton1.Text := 'Disable';
False : jButton1.Text := 'Animate';
end;
end;
procedure TAndroidModule1.jTimer1Timer(Sender: TObject);
begin
// Animate Square
Inc(FSquareS);
if FSquareS > 200 then FSquareS := 50;
if FSquareX <= 0 then FSquareVeloX := abs(FSquareVeloX);
if FSquareX >= jView1.Width then FSquareVeloX := -(abs(FSquareVeloX));
if FSquareY <= 0 then FSquareVeloY := abs(FSquareVeloY);
if FSquareY >= jView1.Height then FSquareVeloY := -(abs(FSquareVeloY));
Inc(FSquareX, FSquareVeloX);
Inc(FSquareY, FSquareVeloY);
// Animate Triangle
if FTriangleX <= 0 then FTriangleVeloX := Random(8) +1;
if FTriangleX >= jView1.Width then FTriangleVeloX := -Random(8) -1;
if FTriangleY <= 0 then FTriangleVeloY := Random(8) +1;
if FTriangleY >= jView1.Height then FTriangleVeloY := -Random(8) -1;
Inc(FTriangleX, FTriangleVeloX);
Inc(FTriangleY, FTriangleVeloY);
// Update the canvas
jView1.Refresh;
end;
procedure TAndroidModule1.AndroidModule1Destroy(Sender: TObject);
begin
jTimer1.Enabled := False;
end;
procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
begin
// Initialize Square
FSquareX := (jView1.Width div 2) -100;
FSquareY := jView1.Height div 2;
FSquareS := 50;
FSquareVeloX := Random(8) +1;
FSquareVeloY := Random(8) +1;
// Initialize Triangle
FTriangleX := (jView1.Width div 2) +100;
FTriangleY := jView1.Height div 2;
FTriangleVeloX := Random(8) +1;
FTriangleVeloY := Random(8) +1;
end;
procedure TAndroidModule1.DrawPoly(BorderColor: TARGBColorBridge;
BorderWidth: Integer; Points: TPolygon);
var
i: Integer;
begin
jView1.Canvas.PaintColor := BorderColor;
jView1.Canvas.PaintStrokeWidth := BorderWidth;
// Connect all the points from first to last
for i := Low(Points) to High(Points)-1 do
jCanvas1.drawLine(Points[i].x, Points[i].y, Points[i+1].x, Points[i+1].y);
// Connect last point with first point
i := High(Points);
jView1.Canvas.drawLine(Points[i].X, Points[i].Y, Points[0].X, Points[0].Y);
end;
end.