Sign in Join
Wiki copytemporary

BGRABitmap tutorial 3/fr: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11Ce tutoriel montre comment dessiner avec la souris.
22
33### Création d'un nouveau projet
44
55Créez un nouveau projet et ajoutez la référence à [[BGRABitmap]], de la même façon que dans [[BGRABitmap tutorial/fr|le premier tutoriel]].
66
77### Création d'une nouvelle image
88
99Ajoutez une variable privée à la fenêtre principale pour stocker l'image :
1010
1111``` pascal
1212 TForm1 = class(TForm)
1313 private
1414 { private declarations }
1515 image: TBGRABitmap;
1616 public
1717 { public declarations }
1818 end;
1919```
2020
2121Créez l'image quand la fenêtre est créée. Pour cela, double-cliquez sur la fenêtre. Une procédure devrait apparaître dans l'éditeur de code. Ajoutez l'instruction de création :
2222
2323``` pascal
2424procedure TForm1.FormCreate(Sender: TObject);
2525begin
2626 image := TBGRABitmap.Create(640,480,BGRAWhite); //crée une image 640x480
2727end;
2828```
2929
3030### Dessin de l'image
3131
3232Ajoutez un gestionnaire OnPaint. Pour cela, cliquez sur la fenêtre, allez dans l'inspecteur d'objet dans l'onglet événement, et double-cliquez sur la ligne *OnPaint*. Ensuite, ajoutez le code suivant :
3333
3434``` pascal
3535procedure TForm1.FormPaint(Sender: TObject);
3636begin
3737 PaintImage;
3838end;
3939```
4040
4141Ajoutez la procédure PaintImage :
4242
4343``` pascal
4444procedure TForm1.PaintImage;
4545begin
4646 image.Draw(Canvas,0,0,True);
4747end;
4848```
4949
5050Après avoir écrit cela, mettez le curseur texte sur *PaintImage* et pressez [[Ctrl-Shift-C]] pour ajouter la déclaration à l'interface.
5151
5252### Gestion de la souris
5353
5454Avec l'inspecteur d'objet, ajoutez des gestionnaires pour les événements *MouseDown* et *MouseMove* :
5555
5656``` pascal
5757procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
5858 Shift: TShiftState; X, Y: Integer);
5959begin
6060 if Button = mbLeft then DrawBrush(X,Y);
6161end;
6262
6363procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
6464 Y: Integer);
6565begin
6666 if ssLeft in Shift then DrawBrush(X,Y);
6767end;
6868```
6969
7070Ajoutez la procédure DrawBrush :
7171
7272``` pascal
7373procedure TForm1.DrawBrush(X, Y: Integer);
7474const radius = 5;
7575begin
7676 image.GradientFill(X-radius,Y-radius, X+radius,Y+radius,
7777 BGRABlack,BGRAPixelTransparent, gtRadial,
7878 PointF(X,Y), PointF(X+radius,Y), dmDrawWithTransparency);
7979
8080 PaintImage;
8181end;
8282```
8383
8484Après avoir écrit cela, mettez le curseur texte sur *DrawBrush* et pressez *Ctrl-Shift-C* pour ajouter la déclaration à l'interface.
8585
8686Cette procédure dessine un gradient radial (*gtRadial*) :
8787
8888- le rectangle encadrant est (X-radius,Y-radius, X+radius,Y+radius) ;
8989- le centre est noir et le bord transparent ;
9090- le centre est à (X,Y) et le bord à (X+radius,Y).
9191
9292### Code
9393
9494``` pascal
9595unit UMain;
9696
9797{$mode objfpc}{$H+}
9898
9999interface
100100
101101uses
102102 Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
103103 BGRABitmap, BGRABitmapTypes;
104104
105105type
106106 { TForm1 }
107107
108108 TForm1 = class(TForm)
109109 procedure FormCreate(Sender: TObject);
110110 procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
111111 Shift: TShiftState; X, Y: Integer);
112112 procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
113113 procedure FormPaint(Sender: TObject);
114114 private
115115 { private declarations }
116116 image: TBGRABitmap;
117117 procedure DrawBrush(X, Y: Integer);
118118 procedure PaintImage;
119119 public
120120 { public declarations }
121121 end;
122122
123123var
124124 Form1: TForm1;
125125
126126implementation
127127
128128{ TForm1 }
129129
130130procedure TForm1.FormCreate(Sender: TObject);
131131begin
132132 image := TBGRABitmap.Create(640,480,BGRAWhite);
133133end;
134134
135135procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
136136 Shift: TShiftState; X, Y: Integer);
137137begin
138138 if Button = mbLeft then DrawBrush(X,Y);
139139end;
140140
141141procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
142142 Y: Integer);
143143begin
144144 if ssLeft in Shift then DrawBrush(X,Y);
145145end;
146146
147147procedure TForm1.FormPaint(Sender: TObject);
148148begin
149149 PaintImage;
150150end;
151151
152152procedure TForm1.DrawBrush(X, Y: Integer);
153153const radius = 20;
154154begin
155155 image.GradientFill(X-radius,Y-radius, X+radius,Y+radius,
156156 BGRABlack,BGRAPixelTransparent,gtRadial,
157157 PointF(X,Y), PointF(X+radius,Y), dmDrawWithTransparency);
158158
159159 PaintImage;
160160end;
161161
162162procedure TForm1.PaintImage;
163163begin
164164 image.Draw(Canvas,0,0,True);
165165end;
166166
167167initialization
168168 {$I UMain.lrs}
169169
170170end.
171171```
172172
173173### Exécution du programme
174174
175175Vous pouvez dessiner sur la fenêtre.
176176
177-<figure>
178-<img src="BGRATutorial3.png" title="BGRATutorial3.png" />
179-<figcaption>BGRATutorial3.png</figcaption>
180-</figure>
177+![](/wiki-images/a/aa/BGRATutorial3.png)
181178
182179Remarquez que, selon la vitesse de déplacement de la souris, le tracé est plus ou moins foncé.
183180
184181### Obtenir un tracé continu
185182
186183Afin d'avoir un tracé continu, vous aurez besoin de variables supplémentaires :
187184
188185``` pascal
189186 TForm1 = class(TForm)
190187 ...
191188 private
192189 { private declarations }
193190 image: TBGRABitmap;
194191 mouseDrawing: boolean;
195192 mouseOrigin: TPoint;
196193```
197194
198195*mouseDrawing* sera à *True* pendant le tracé (avec le bouton gauche appuyé), et *mouseOrigin* sera le point de départ du segment à dessiner.
199196
200197Au moment du clic, le code devient un peu plus compliqué :
201198
202199``` pascal
203200procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
204201 Shift: TShiftState; X, Y: Integer);
205202begin
206203 if Button = mbLeft then
207204 begin
208205 mouseDrawing := True;
209206 mouseOrigin := Point(X,Y);
210207 DrawBrush(X,Y,True);
211208 end;
212209end;
213210```
214211
215212On initialise le tracé en précisant la position de départ. Ensuite, on dessine un segment complet (notez le nouveau paramètre de *DrawBrush*). En effet, au début, le segment est complet, ce qui dans le cas d'un segment de longueur zéro correspond à un disque :
216213
217-<figure>
218-<img src="BGRATutorial3b.png" title="BGRATutorial3b.png" />
219-<figcaption>BGRATutorial3b.png</figcaption>
220-</figure>
214+![](/wiki-images/b/b1/BGRATutorial3b.png)
221215
222216Au fur et à mesure, on ajoute la nouvelle partie tracée, qui est un segment ouvert. Par exemple :
223217
224-<figure>
225-<img src="BGRATutorial3c.png" title="BGRATutorial3c.png" />
226-<figcaption>BGRATutorial3c.png</figcaption>
227-</figure>
218+![](/wiki-images/d/d5/BGRATutorial3c.png)
228219
229220Voilà pourquoi nous avons besoin d'un nouveau paramètre pour la fonction *DrawBrush*, qui devient :
230221
231222``` pascal
232223procedure TForm1.DrawBrush(X, Y: Integer; Closed: Boolean);
233224const brushRadius = 20;
234225begin
235226 image.DrawLineAntialias(X,Y,mouseOrigin.X,mouseOrigin.Y,BGRA(0,0,0,128),brushRadius,Closed);
236227 mouseOrigin := Point(X,Y);
237228
238229 PaintImage;
239230end;
240231```
241232
242233On transmet à *DrawLineAntialias* le paramètre *Closed*, indiquant si le segment est complet. Notez l'ordre des coordonnées. Le départ du segment et son point d'arrivée sont échangés. En effet, pour *DrawLineAntialias*, c'est la fin du segment qui est ouverte, alors que dans notre cas, c'est le début du segment qui est ouvert.
243234
244235Il faut mettre à jour la définition de *DrawBrush* dans l'interface.
245236
246237Le gestionnaire *MouseMove* devient :
247238
248239``` pascal
249240procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
250241 Y: Integer);
251242begin
252243 if mouseDrawing then DrawBrush(X,Y,False);
253244end;
254245```
255246
256247Enfin, il faut ajouter un gestionnaire *MouseUp* pour mettre à jour *mouseDrawing* :
257248
258249``` pascal
259250procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
260251 Shift: TShiftState; X, Y: Integer);
261252begin
262253 if Button = mbLeft then
263254 mouseDrawing := False;
264255end;
265256```
266257
267258### Code
268259
269260``` pascal
270261unit UMain;
271262
272263{$mode objfpc}{$H+}
273264
274265interface
275266
276267uses
277268 Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
278269 BGRABitmap, BGRABitmapTypes;
279270
280271type
281272 { TForm1 }
282273
283274 TForm1 = class(TForm)
284275 procedure FormCreate(Sender: TObject);
285276 procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
286277 Shift: TShiftState; X, Y: Integer);
287278 procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
288279 procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
289280 Shift: TShiftState; X, Y: Integer);
290281 procedure FormPaint(Sender: TObject);
291282 private
292283 { private declarations }
293284 image: TBGRABitmap;
294285 mouseDrawing: boolean;
295286 mouseOrigin: TPoint;
296287 procedure DrawBrush(X, Y: Integer; Closed: boolean);
297288 procedure PaintImage;
298289 public
299290 { public declarations }
300291 end;
301292
302293var
303294 Form1: TForm1;
304295
305296implementation
306297
307298{ TForm1 }
308299
309300procedure TForm1.FormCreate(Sender: TObject);
310301begin
311302 image := TBGRABitmap.Create(640,480,BGRAWhite);
312303end;
313304
314305procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
315306 Shift: TShiftState; X, Y: Integer);
316307begin
317308 if Button = mbLeft then
318309 begin
319310 mouseDrawing := True;
320311 mouseOrigin := Point(X,Y);
321312 DrawBrush(X,Y,True);
322313 end;
323314end;
324315
325316procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
326317 Y: Integer);
327318begin
328319 if mouseDrawing then DrawBrush(X,Y,False);
329320end;
330321
331322procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
332323 Shift: TShiftState; X, Y: Integer);
333324begin
334325 if Button = mbLeft then
335326 mouseDrawing := False;
336327end;
337328
338329procedure TForm1.FormPaint(Sender: TObject);
339330begin
340331 PaintImage;
341332end;
342333
343334procedure TForm1.DrawBrush(X, Y: Integer; Closed: Boolean);
344335const brushRadius = 20;
345336begin
346337 image.DrawLineAntialias(X,Y,mouseOrigin.X,mouseOrigin.Y,BGRA(0,0,0,128),brushRadius,Closed);
347338 mouseOrigin := Point(X,Y);
348339
349340 PaintImage;
350341end;
351342
352343procedure TForm1.PaintImage;
353344begin
354345 image.Draw(Canvas,0,0,True);
355346end;
356347
357348initialization
358349 {$I UMain.lrs}
359350
360351end.
361352```
362353
363354### Exécution du programme
364355
365356À présent, le tracé est presque uniforme :
366357
367-<figure>
368-<img src="BGRATutorial3d.png" title="BGRATutorial3d.png" />
369-<figcaption>BGRATutorial3d.png</figcaption>
370-</figure>
358+![](/wiki-images/9/9a/BGRATutorial3d.png)
371359
372360[[BGRABitmap tutorial 2/fr|Tutoriel précédent (chargement d'image)]] \| [[BGRABitmap tutorial 4/fr|Tutoriel suivant (accès direct aux pixels)]]
373361
374362---
375363*From the Free Pascal wiki, page "BGRABitmap tutorial 3/fr", as it stood on 2020-02-10 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history