chapter 11 programming with lazarus - fenix.tecnico.ulisboa.pt · what is lazarus •lazarus is an...
TRANSCRIPT
1
Luís Sousa
Instituto Superior TécnicoMarch 2015
Chapter 11
Programming with Lazarus(topics)
What is Lazarus
• Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development (RAD) programs.
• Compile programs with Free Pascal
• Freeware, http://www.lazarus-ide.org/
• Open source, cross-platform, extendable with packages
• Compatible with “Delphi”, a commercial RAD environment
2
Lazarus IDE
• Multiwindow interface
3
code
Object
Inspector
Messages
Main
interface
Visual
Form
Lazarus Main Interface
• Menus, Buttons, tab Toobars
4
First RAD Program in Lazarus
• File->New
5
First RAD Program in Lazarus
• Unit and Form default names
6
First RAD Program in Lazarus
• Structure of the codeunit <unitname>;
interfaceuses <packages>;type <type definitions>;
var <var definitions>;
implementation..end.
7
First RAD Program in Lazarus
• Form properties
• The object inspectorshows the propertiesof each component
8
First RAD Program in Lazarus
• Save the project (*.lpi) and unit (*.pas)
• Run the code
• Program does:
-minimize, maximize,close;
-sizeable
• Close the program
9
Add Objects to the Program
• Button properties
• The object inspectorshows the propertiesof the component
• Double click the button to createits (empty) code
10
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
...
procedure TForm1.Button1Click(Sender: TObject);
begin
end;
Add Objects to the Program (cont.)
• Button properties
• The object inspectorshows the propertiesof the component
• Double click the button to createits (empty) code
11
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
...
procedure TForm1.Button1Click(Sender: TObject);
begin
end;
• In Class TForm1
• Insert a simple message to answer “OnClick” event
Add Objects to the Program (cont.)
12
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage ('I have clicked Button 1');
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private { private declarations }
public { public declarations }
end;
• Rename objects
• Insert a calendar anda label on the form
Add Objects to the Program (cont.)
13
• Add the code to obtain the todays age
• Use predefined functions/procedures. Search appropriate routines on the web, its sintax and examples
• Use “DateUtils” unit; “DaysBetween” function
Add Objects to the Program (cont.)
14
uses ..., DateUtils;
...
procedure TPersonAge.AgeButtonClick(Sender: TObject);
var ndays:integer;
begin
ndays:=DaysBetween(now,BirthCalendar.DateTime);
ShowMessage('Number of days='+IntToStr(ndays));
end;
• Open source graphics library, based on OpenGL. Works on Lazarus (LCL) and Delphi (VLC, C++)
• Download from www.glscene.org
• Copy zip file to “\Lazarus\components” folder (any folder is accepted), and extract it
• Find “GLScene_DesignTime.lpk” package file and double-click on it.
The GLScene library
15
• Compile the package• On success, Install the package• Accept Lazarus to recompile itself• New set of tabs related to GLScene
Install GLScene library
16
• Create a New Application in Lazarus• Insert GLScene ( ) and GLSceneViewer ( )on form• Change the GLSceneViewer’s
Background color in Object Inspector
First GLScene program
17
• Create a scene: camera, lights, objects
• Double-click
• Right-click on Scene objects to add acamera and add light
First GLScene program
18
• Create a DummyCube
• Insert a Sphere and a Cone as child's ofthe DummyCube1
• Define the GLSceneViewer1 camera as GLCamera1
• Define GLCamera1’s target object as DummyCube1, and position to (5,5,5)
• Modify GLCone1 position to (2,0,0)
First GLScene program (cont.)
19
20