20
1 Luís Sousa Instituto Superior Técnico March 2015 Chapter 11 Programming with Lazarus (topics)

Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

  • Upload
    vudang

  • View
    300

  • Download
    8

Embed Size (px)

Citation preview

Page 1: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

1

Luís Sousa

Instituto Superior TécnicoMarch 2015

Chapter 11

Programming with Lazarus(topics)

Page 2: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

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

Page 3: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

Lazarus IDE

• Multiwindow interface

3

code

Object

Inspector

Messages

Main

interface

Visual

Form

Page 4: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

Lazarus Main Interface

• Menus, Buttons, tab Toobars

4

Page 5: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

First RAD Program in Lazarus

• File->New

5

Page 6: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

First RAD Program in Lazarus

• Unit and Form default names

6

Page 7: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

First RAD Program in Lazarus

• Structure of the codeunit <unitname>;

interfaceuses <packages>;type <type definitions>;

var <var definitions>;

implementation..end.

7

Page 8: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

First RAD Program in Lazarus

• Form properties

• The object inspectorshows the propertiesof each component

8

Page 9: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

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

Page 10: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

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;

Page 11: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

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;

Page 12: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

• 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;

Page 13: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

• Rename objects

• Insert a calendar anda label on the form

Add Objects to the Program (cont.)

13

Page 14: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

• 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;

Page 15: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

• 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

Page 16: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

• Compile the package• On success, Install the package• Accept Lazarus to recompile itself• New set of tabs related to GLScene

Install GLScene library

16

Page 17: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

• 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

Page 18: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

• Create a scene: camera, lights, objects

• Double-click

• Right-click on Scene objects to add acamera and add light

First GLScene program

18

Page 19: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

• 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

Page 20: Chapter 11 Programming with Lazarus - fenix.tecnico.ulisboa.pt · What is Lazarus •Lazarus is an Object Oriented Programming (OOP), Pascal based, for modern Rapid Application Development

20