Difference between revisions of "BGRABitmap tutorial 3"

From Lazarus wiki
Jump to navigationJump to search
m (wiki lang)
Line 1: Line 1:
 
{{BGRABitmap_tutorial_3}}
 
{{BGRABitmap_tutorial_3}}
 +
 +
{{BGRABitmap_tutorial_index}}
  
 
This tutorial shows you how to draw on a bitmap with the mouse.
 
This tutorial shows you how to draw on a bitmap with the mouse.

Revision as of 15:56, 3 April 2011

Deutsch (de) English (en) español (es) français (fr)


Home | Tutorial 1 | Tutorial 2 | Tutorial 3 | Tutorial 4 | Tutorial 5 | Tutorial 6 | Tutorial 7 | Tutorial 8 | Tutorial 9 | Tutorial 10 | Tutorial 11 | Tutorial 12 | Tutorial 13 | Tutorial 14 | Tutorial 15 | Tutorial 16 | Edit

This tutorial shows you how to draw on a bitmap with the mouse.

Create a new project

Create a new project and add a reference to BGRABitmap, the same way as in the first tutorial.

Create a new image

Add a private variable to the main form to store the image : <delphi> TForm1 = class(TForm)

 private
   { private declarations }
   image: TBGRABitmap;
 public
   { public declarations }
 end; </delphi>

Create the image when the form is created. To do this, double-click on the form, a procedure should appear in the code editor. Add the create instruction : <delphi>procedure TForm1.FormCreate(Sender: TObject); begin

 image := TBGRABitmap.Create(640,480,BGRAWhite);  //create a 640x480 image

end; </delphi>

Draw the bitmap

Add an OnPaint handler. To do this, select the main form, then go to the object inspector, in the event tab, and double-click on the OnPaint line. Then, add the drawing code : <delphi>procedure TForm1.FormPaint(Sender: TObject); begin

 PaintImage;

end; </delphi>

Add the PaintImage procedure : <delphi>procedure TForm1.PaintImage; begin

 image.Draw(Canvas,0,0,True);

end; </delphi>

After writing this, put the cursor on PaintImage and press Ctrl-Shift-C to add the declaration to the interface.

Handle mouse

With the object inspector, add handlers for MouseDown and MouseMove events : <delphi>procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;

 Shift: TShiftState; X, Y: Integer);

begin

 if Button = mbLeft then DrawBrush(X,Y);

end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,

 Y: Integer);

begin

 if ssLeft in Shift then DrawBrush(X,Y);

end;</delphi>

Add the DrawBrush procedure : <delphi>procedure TForm1.DrawBrush(X, Y: Integer); const radius = 5; begin

 image.GradientFill(X-radius,Y-radius, X+radius,Y+radius,
   BGRABlack,BGRAPixelTransparent, gtRadial,
   PointF(X,Y), PointF(X+radius,Y), dmDrawWithTransparency);
 PaintImage;

end;</delphi>

After writing this, put the cursor on DrawBrush and press Ctrl-Shift-C to add the declaration to the interface.

This procedure draws as radial gradient (gtRadial) :

  • the bounding rectangle is (X-radius,Y-radius, X+radius,Y+radius).
  • the center is black, the border is transparent
  • the center is at (X,Y) and the border at (X+radius,Y)

Code

<delphi>unit UMain;

{$mode objfpc}{$H+}

interface

uses

 Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
 BGRABitmap, BGRABitmapTypes;

type

 { TForm1 }
 TForm1 = class(TForm)
   procedure FormCreate(Sender: TObject);
   procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
     Shift: TShiftState; X, Y: Integer);
   procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
   procedure FormPaint(Sender: TObject);
 private
   { private declarations }
   image: TBGRABitmap;
   procedure DrawBrush(X, Y: Integer);
   procedure PaintImage;
 public
   { public declarations }
 end; 

var

 Form1: TForm1; 

implementation

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject); begin

 image := TBGRABitmap.Create(640,480,BGRAWhite);

end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;

 Shift: TShiftState; X, Y: Integer);

begin

 if Button = mbLeft then DrawBrush(X,Y);

end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,

 Y: Integer);

begin

 if ssLeft in Shift then DrawBrush(X,Y);

end;

procedure TForm1.FormPaint(Sender: TObject); begin

 PaintImage;

end;

procedure TForm1.DrawBrush(X, Y: Integer); const radius = 20; begin

 image.GradientFill(X-radius,Y-radius, X+radius,Y+radius,
   BGRABlack,BGRAPixelTransparent,gtRadial,
   PointF(X,Y), PointF(X+radius,Y), dmDrawWithTransparency);
 PaintImage;

end;

procedure TForm1.PaintImage; begin

 image.Draw(Canvas,0,0,True);

end;

initialization

 {$I UMain.lrs}

end.</delphi>

Run the program

You should be able to draw on the form.

BGRATutorial3.png

Previous tutorial (image loading) | Next tutorial (direct pixel access)