Recent

Author Topic: [Solved] How to add a new procedure to LAMW  (Read 4895 times)

loaded

  • Hero Member
  • *****
  • Posts: 824
[Solved] How to add a new procedure to LAMW
« on: February 11, 2019, 04:24:28 pm »
Hi everyone.
I'm using the drawLine method below to draw lines on canvas in Lamw.
But I want to draw the polygon shapes at once.
I think the drawLines method is necessary for this.
I have never been able to write a procedure that has worked in countless trials.
How do I add the Drawlines method to Lamw?

Thanks in advance to the friends who responded.

Code: Pascal  [Select][+][-]
  1. unit And_jni_Bridge;  
  2. ....
  3. Procedure jCanvas_drawLine             (env:PJNIEnv;
  4.                                         Canv : jObject; x1,y1,x2,y2 : single);
  5. ....
  6.  
  7. Procedure jCanvas_drawLine(env:PJNIEnv; Canv : jObject; x1,y1,x2,y2 : single);
  8. var
  9.  _jMethod: jMethodID = nil;
  10.  _jParams: Array[0..3] of jValue;
  11.  cls: jClass;
  12. begin
  13.  _jParams[0].F := x1;
  14.  _jParams[1].F := y1;
  15.  _jParams[2].F := x2;
  16.  _jParams[3].F := y2;
  17.  cls := env^.GetObjectClass(env, Canv);
  18.  _jMethod:= env^.GetMethodID(env, cls, 'drawLine', '(FFFF)V');
  19.  env^.CallVoidMethodA(env,Canv,_jMethod,@_jParams);
  20.  env^.DeleteLocalRef(env, cls);
  21. end;
  22.  

« Last Edit: February 13, 2019, 01:36:16 pm by loaded »
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: How to add a new procedure to LAMW
« Reply #1 on: February 11, 2019, 07:25:49 pm »
You can see my glTest example code here:
http://forum.lazarus.freepascal.org/index.php/topic,38777.msg264878.html#msg264878

It draws rectangles with empty rectangle in the center of it.

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: How to add a new procedure to LAMW
« Reply #2 on: February 11, 2019, 07:43:41 pm »
Handoko, thank you so much for answering. Health in your hands.
I've reviewed the codes, too complex for the process I want to do, my goal is to easily draw polygons with 4 ~ 100 edges at one time.
I think I can do this in a simple way with the drawLines command. I'd appreciate it if you could help me with that.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: How to add a new procedure to LAMW
« Reply #3 on: February 11, 2019, 07:47:55 pm »
Currently I do not have LAMW properly installed. Hopefully you can get helps from others.

I will check it back some days later. If you get no answer I will try JCanvas, which I haven't used before.

balazsszekely

  • Guest
Re: How to add a new procedure to LAMW
« Reply #4 on: February 11, 2019, 07:52:45 pm »
Maybe something similar works with LAMW too, you should give it a try:
Code: Pascal  [Select][+][-]
  1. var
  2.   ATP : array of TPoint;
  3.  
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. begin
  6.   SetLength(ATP, 4);
  7.   ATP[0].X := 115; ATP[0].Y := 55;
  8.   ATP[1].X := 225; ATP[1].Y := 20;
  9.   ATP[2].X := 275; ATP[2].Y := 175;
  10.   ATP[3].X := 55;  ATP[3].Y := 130;
  11. end;
  12.  
  13. procedure TForm1.FormPaint(Sender: TObject);
  14. begin
  15.   Canvas.Pen.Width := 2;
  16.   Canvas.Pen.Color := clBlack;
  17.   Canvas.Brush.Color := clRed;
  18.   Canvas.Polygon(ATP);
  19. end;  
« Last Edit: February 11, 2019, 07:55:39 pm by GetMem »

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: How to add a new procedure to LAMW
« Reply #5 on: February 11, 2019, 08:16:10 pm »
GetMem, thank you very much for your reply.
But as far as I know, the polygon method in LAMW does not have any unfortunately, the only way is the drawLines method.


Currently I do not have LAMW properly installed. Hopefully you can get helps from others.

I will check it back some days later. If you get no answer I will try JCanvas, which I haven't used before.

Handoko brother,
I am waiting for your reply.


Check out  loaded on Strava
https://www.strava.com/athletes/109391137

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: How to add a new procedure to LAMW
« Reply #6 on: February 12, 2019, 09:23:08 am »
I wrote a simple polygon with animation using JCanvas demo:

Code: Pascal  [Select][+][-]
  1. {Hint: save all files to location: /home/handoko/Desktop/JCanvasPolygon/LamwGUIProject1/jni }
  2. unit unit1;
  3.  
  4. {$mode delphi}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, AndroidWidget, Laz_And_Controls;
  10.  
  11. type
  12.  
  13.   TPolygon = array of TPoint;
  14.  
  15.   { TAndroidModule1 }
  16.  
  17.   TAndroidModule1 = class(jForm)
  18.     jButton1: jButton;
  19.     jCanvas1: jCanvas;
  20.     jTimer1: jTimer;
  21.     jView1: jView;
  22.     procedure AndroidModule1Destroy(Sender: TObject);
  23.     procedure AndroidModule1JNIPrompt(Sender: TObject);
  24.     procedure jButton1Click(Sender: TObject);
  25.     procedure jTimer1Timer(Sender: TObject);
  26.     procedure jView1Draw(Sender: TObject);
  27.   private
  28.     FSquareX: Integer;
  29.     FSquareY: Integer;
  30.     FSquareS: Integer;
  31.     FSquareVeloX: Integer;
  32.     FSquareVeloY: Integer;
  33.     FTriangleX: Integer;
  34.     FTriangleY: Integer;
  35.     FTriangleVeloX: Integer;
  36.     FTriangleVeloY: Integer;
  37.     procedure DrawPoly(BorderColor: TARGBColorBridge; BorderWidth: Integer;
  38.       Points: TPolygon);
  39.   end;
  40.  
  41. var
  42.   AndroidModule1: TAndroidModule1;
  43.  
  44. implementation
  45.  
  46. {$R *.lfm}
  47.  
  48. { TAndroidModule1 }
  49.  
  50. procedure TAndroidModule1.jView1Draw(Sender: TObject);
  51. var
  52.   Polygon: TPolygon;
  53. begin
  54.  
  55.   // Draw Square
  56.   Polygon := TPolygon.Create(
  57.     Point(FSquareX-FSquareS, FSquareY-FSquareS),
  58.     Point(FSquareX+FSquareS, FSquareY-FSquareS),
  59.     Point(FSquareX+FSquareS, FSquareY+FSquareS),
  60.     Point(FSquareX-FSquareS, FSquareY+FSquareS));
  61.   DrawPoly(colbrGreen, 3, Polygon);
  62.  
  63.   // Draw Triangle
  64.   Polygon := TPolygon.Create(
  65.     Point(FTriangleX,     FTriangleY-80),
  66.     Point(FTriangleX+100, FTriangleY+80),
  67.     Point(FTriangleX-100, FTriangleY+80));
  68.   DrawPoly(colbrRed, 3, Polygon);
  69.  
  70. end;
  71.  
  72. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  73. begin
  74.   jTimer1.Enabled := not(jTimer1.Enabled);
  75.   case jTimer1.Enabled of
  76.     True  : jButton1.Text := 'Disable';
  77.     False : jButton1.Text := 'Animate';
  78.   end;
  79. end;
  80.  
  81. procedure TAndroidModule1.jTimer1Timer(Sender: TObject);
  82. begin
  83.  
  84.   // Animate Square
  85.   Inc(FSquareS);
  86.   if FSquareS > 200 then            FSquareS := 50;
  87.   if FSquareX <= 0  then            FSquareVeloX := abs(FSquareVeloX);
  88.   if FSquareX >= jView1.Width then  FSquareVeloX := -(abs(FSquareVeloX));
  89.   if FSquareY <= 0  then            FSquareVeloY := abs(FSquareVeloY);
  90.   if FSquareY >= jView1.Height then FSquareVeloY := -(abs(FSquareVeloY));
  91.   Inc(FSquareX, FSquareVeloX);
  92.   Inc(FSquareY, FSquareVeloY);
  93.  
  94.   // Animate Triangle
  95.   if FTriangleX <= 0  then            FTriangleVeloX := Random(8)  +1;
  96.   if FTriangleX >= jView1.Width  then FTriangleVeloX := -Random(8) -1;
  97.   if FTriangleY <= 0  then            FTriangleVeloY := Random(8)  +1;
  98.   if FTriangleY >= jView1.Height then FTriangleVeloY := -Random(8) -1;
  99.   Inc(FTriangleX, FTriangleVeloX);
  100.   Inc(FTriangleY, FTriangleVeloY);
  101.  
  102.   // Update the canvas
  103.   jView1.Refresh;
  104.  
  105. end;
  106.  
  107. procedure TAndroidModule1.AndroidModule1Destroy(Sender: TObject);
  108. begin
  109.   jTimer1.Enabled := False;
  110. end;
  111.  
  112. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  113. begin
  114.  
  115.   // Initialize Square
  116.   FSquareX     := (jView1.Width div 2) -100;
  117.   FSquareY     := jView1.Height div 2;
  118.   FSquareS     := 50;
  119.   FSquareVeloX := Random(8) +1;
  120.   FSquareVeloY := Random(8) +1;
  121.  
  122.   // Initialize Triangle
  123.   FTriangleX     := (jView1.Width div 2) +100;
  124.   FTriangleY     := jView1.Height div 2;
  125.   FTriangleVeloX := Random(8) +1;
  126.   FTriangleVeloY := Random(8) +1;
  127.  
  128. end;
  129.  
  130. procedure TAndroidModule1.DrawPoly(BorderColor: TARGBColorBridge;
  131.   BorderWidth: Integer; Points: TPolygon);
  132. var
  133.   i: Integer;
  134. begin
  135.   jView1.Canvas.PaintColor       := BorderColor;
  136.   jView1.Canvas.PaintStrokeWidth := BorderWidth;
  137.   // Connect all the points from first to last
  138.   for i := Low(Points) to High(Points)-1 do
  139.     jCanvas1.drawLine(Points[i].x, Points[i].y, Points[i+1].x, Points[i+1].y);
  140.   // Connect last point with first point
  141.   i := High(Points);
  142.   jView1.Canvas.drawLine(Points[i].X, Points[i].Y, Points[0].X, Points[0].Y);
  143. end;
  144.  
  145. end.

I created a procedure to do polygon drawing, see DrawPoly see line #130.
To use DrawPoly, see lines #55..#68.

Due to the limitation of maximum total post size, the apk file can be found on the next post.
« Last Edit: February 12, 2019, 09:37:00 am by Handoko »

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: How to add a new procedure to LAMW
« Reply #7 on: February 12, 2019, 09:27:40 am »
This is the install-able apk file:

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: How to add a new procedure to LAMW
« Reply #8 on: February 12, 2019, 04:50:52 pm »
Handoko, thank you so much for your answer.
Health in your hands, the program you wrote is very good.
But, the solution is available, I'm already using a similar solution.
Code: Pascal  [Select][+][-]
  1. var
  2.   pol_coor:array of TPoint;
  3.   ans:integer;
  4.  begin
  5.  
  6.       .....
  7.           for ans:=low(pol_coor) to High(pol_coor)-1 do
  8.           begin
  9.                  jView1.Canvas.drawLine(pol_coor[ans].X,pol_coor[ans].Y,pol_coor[ans+1].X,pol_coor[ans+1].Y);
  10.           end;
  11.          jView1.Canvas.drawLine(pol_coor[High(pol_coor)].X,pol_coor[High(pol_coor)].Y,pol_coor[0].X,pol_coor[0].Y);
  12. ....
  13.  

But what I want, instead of drawing the edges one by one, is the direct drawing method which is much faster than the above.

Code: Pascal  [Select][+][-]
  1. jview1.canvas.drawlines (pol_coor);
  2.  

That is, polygon object with direct operating system boot.
my experience on windows canvas is that drawing polygons at once is much simpler and faster than drawing their edges one at a time.

I want, accessing the Java Local Interface, with Lazarus
Code: Pascal  [Select][+][-]
  1. _jMethod: = env ^ .GetMethodID (env, cls, 'drawLines', '????????');
use the method
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: How to add a new procedure to LAMW
« Reply #9 on: February 12, 2019, 04:55:58 pm »
I'm not familiar with Java things.

Be patient, the developer of LAMW checks this forum regularly. He will answer your question.

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: How to add a new procedure to LAMW
« Reply #10 on: February 12, 2019, 05:30:51 pm »
Yes, you're right.
I hope the precious developer will add this feature. :)
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: How to add a new procedure to LAMW
« Reply #11 on: February 13, 2019, 11:23:15 am »
Done!!

Added to jCanvas:

    function GetNewPath(var _points: TDynArrayOfSingle): jObject; overload;
    function GetNewPath(_points: array of single): jObject;  overload;
    procedure DrawPath(var _points: TDynArrayOfSingle);  overload;
    procedure DrawPath(_points: array of single);  overload;
    procedure DrawPath(_path: jObject);  overload;

    Edited: +1

    procedure DrawArc(_leftRectF: single; _topRectF: single; _rightRectF: single; _bottomRectF: single; _startAngle: single; _sweepAngle: single; _useCenter: boolean);

how to use:

Code: Pascal  [Select][+][-]
  1. var
  2.   path: jObjectRef;
  3.   w1, h1: integer;
  4. begin
  5.  
  6.    w1:= jMyView.Width;
  7.    h1:= jMyView.Height;
  8.  
  9.    //Frame
  10.    path:= jDrawingView1.GetNewPath([
  11.                                 0,0,               //p1
  12.                                 0,h1-1,         //p2
  13.                                 w1-1,h1-1,   //p3
  14.                                 w1-1,0,         //p4
  15.                                 0,0                 //p1
  16.                                 ]);
  17.  
  18.    jDrawingView1.DrawPath(path);
  19.  
  20. end;
  21.  


But,  "jDrawingView"  is much better supported! Please,  go to demo "AppDrawingViewDemo1"
« Last Edit: February 13, 2019, 01:22:34 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: How to add a new procedure to LAMW
« Reply #12 on: February 13, 2019, 01:35:52 pm »
jmpessoa, thank you very much. :)
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

 

TinyPortal © 2005-2018