Anda di halaman 1dari 8

Media pembelajaran polygon berbasis Delphi – Semesin https://www.project.semesin.com/2020/02/24/media-pembelajaran-polyg...

1 dari 8 16/03/2024 22:00


Media pembelajaran polygon berbasis Delphi – Semesin https://www.project.semesin.com/2020/02/24/media-pembelajaran-polyg...

1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Cl
7 Dialogs, StdCtrls, ExtCtrls, Buttons, Gri
8
9 type
10 TForm1 = class(TForm)
11 Panel1: TPanel;
12 Panel2: TPanel;
13 Image1: TImage;
14 GroupBox1: TGroupBox;
15 GroupBox3: TGroupBox;
16 GroupBox2: TGroupBox;
17 LabelPosisi: TLabel;
18 ButtonClose: TButton;
19 LabelVertex: TLabel;
20 GroupBox4: TGroupBox;
21 StringGrid1: TStringGrid;
22 ButtonDelete: TButton;
23 ButtonDraw: TButton;
24 ButtonEdit: TButton;
25 ButtonInsert: TButton;
26 ButtonClear: TButton;
27 ButtonSave: TButton;
28 ButtonOpen: TButton;
29 Label1: TLabel;
30 procedure ButtonDrawClick(Sender: TObje

2 dari 8 16/03/2024 22:00


Media pembelajaran polygon berbasis Delphi – Semesin https://www.project.semesin.com/2020/02/24/media-pembelajaran-polyg...

31 procedure ButtonEditClick(Sender: TObje


32 procedure Image1MouseMove(Sender: TObje
33 Y: Integer);
34 procedure Image1Click(Sender: TObject);
35 procedure FormCreate(Sender: TObject);
36 procedure ButtonCloseClick(Sender: TObj
37 procedure ButtonSaveClick(Sender: TObje
38 procedure ButtonLoadClick(Sender: TObje
39 procedure refreshCanvas();
40 procedure StringGrid1SelectCell(Sender:
41 var CanSelect: Boolean);
42 procedure ButtonDeleteClick(Sender: TOb
43 procedure FormResize(Sender: TObject);
44 procedure ButtonInsertClick(Sender: TOb
45 procedure ButtonClearClick(Sender: TObj
46 private
47 { Private declarations }
48 public
49 { Public declarations }
50 end;
51
52 type Point = record
53 X : integer;
54 Y : integer;
55 end;
56
57 var
58 Form1: TForm1;
59 modeOperasi : integer;
60 posisi : Point;
61 tinggiCanvas : integer;
62 startPolygon : boolean;
63 polygon : array[0..20] of Point;
64 polygonCount : integer;
65 editIndex : integer;
66
67 const
68 modeIdle = 0;
69 modeDraw = 1;
70 modeEdit = 3;
71 modeInsert = 4;
72
73 implementation
74
75 {$R *.dfm}
76
77 procedure TForm1.refreshCanvas();
78 var
79 i : integer;
80 begin
81 StringGrid1.RowCount := polygonCount + 1;
82
83 i := 0;
84 while true do

3 dari 8 16/03/2024 22:00


Media pembelajaran polygon berbasis Delphi – Semesin https://www.project.semesin.com/2020/02/24/media-pembelajaran-polyg...

85 begin
86 if polygon[i].X = ‐1 then
87 break;
88 StringGrid1.Cells[0,i+1] := inttostr(i+
89 StringGrid1.Cells[1,i+1] := inttostr(po
90 StringGrid1.Cells[2,i+1] := inttostr(po
91
92 if i = 0 then
93 begin
94 image1.Canvas.rectangle(0,0,image1.wi
95 image1.Canvas.MoveTo(polygon[0].X, ti
96 end
97 else
98 begin
99 if (modeOperasi = modeInsert) and (ed
100 begin
101 Image1.Canvas.Pen.Color := clRed;
102 end
103 else
104 begin
105 Image1.Canvas.Pen.Color := clBlack;
106 end;
107 image1.Canvas.LineTo(polygon[i].X, ti
108 end;
109
110 i := i + 1;
111 if i = 20 then
112 break;
113
114 end;
115
116 if modeOperasi = modeEdit then
117 begin
118 image1.Canvas.Rectangle(polygon[editInd
119 polygon[editIndex].X+5, tinggiCanvas
120 end;
121
122 end;
123
124 procedure TForm1.FormCreate(Sender: TObject
125 begin
126 StringGrid1.Cells[0,0] := 'No';
127 StringGrid1.Cells[1,0] := 'X';
128 StringGrid1.Cells[2,0] := 'Y';
129 end;
130
131
132 procedure TForm1.FormResize(Sender: TObject
133 begin
134 tinggiCanvas := Image1.Height;
135
136 end;
137
138 procedure TForm1.ButtonDrawClick(Sender: TO

4 dari 8 16/03/2024 22:00


Media pembelajaran polygon berbasis Delphi – Semesin https://www.project.semesin.com/2020/02/24/media-pembelajaran-polyg...

139 begin
140 buttonDraw.Font.Style := [fsBold];
141 ButtonEdit.Font.Style := [];
142 ButtonInsert.Font.Style := [];
143
144 modeOperasi := modeDraw;
145 startPolygon := true;
146 polygonCount := 0;
147 polygon[polygonCount].X := ‐1;
148
149 refreshCanvas();
150 end;
151
152 procedure TForm1.ButtonCloseClick(Sender: T
153 begin
154
155 if (modeOperasi = modeDraw) and (polygonC
156 begin
157 polygon[polygonCount] := polygon[0];
158 polygonCount := polygonCount + 1;
159
160 polygon[polygonCount].X := ‐1;
161
162 LabelVertex.Caption := 'Vertex ' + intt
163
164 refreshCanvas();
165
166 buttonDraw.Font.Style := [];
167 ButtonEdit.Font.Style := [];
168 ButtonInsert.Font.Style := [];
169 modeOperasi := modeIdle;
170 end;
171
172 end;
173
174 procedure TForm1.ButtonEditClick(Sender: TO
175 begin
176 buttonDraw.Font.Style := [];
177 ButtonEdit.Font.Style := [fsBold];
178 ButtonInsert.Font.Style := [];
179 modeOperasi := modeEdit;
180 editIndex := 1;
181 refreshCanvas();
182 end;
183
184 procedure TForm1.ButtonInsertClick(Sender:
185 begin
186 buttonDraw.Font.Style := [];
187 ButtonEdit.Font.Style := [];
188 ButtonInsert.Font.Style := [fsBold];
189 modeOperasi := modeInsert;
190 editIndex := 1;
191 refreshCanvas();
192 end;

5 dari 8 16/03/2024 22:00


Media pembelajaran polygon berbasis Delphi – Semesin https://www.project.semesin.com/2020/02/24/media-pembelajaran-polyg...

193
194
195 procedure TForm1.ButtonClearClick(Sender: T
196 begin
197 polygonCount := 0;
198 polygon[polygonCount].X := ‐1;
199
200 refreshCanvas();
201 end;
202
203 procedure TForm1.ButtonDeleteClick(Sender:
204 var
205 i : integer;
206 begin
207 if editIndex <> 0 then
208 begin
209 for i := editIndex to polygonCount do
210 begin
211 polygon[i].X := polygon[i+1].X;
212 polygon[i].Y := polygon[i+1].Y;
213 end;
214 polygonCount := polygonCount ‐ 1;
215 refreshCanvas();
216 end;
217 end;
218
219
220 procedure TForm1.Image1MouseMove(Sender: TO
221 Y: Integer);
222 begin
223 posisi.X := X;
224 posisi.Y := tinggiCanvas ‐ Y;
225 LabelPosisi.Caption := 'Posisi ' + inttos
226 end;
227
228 procedure TForm1.Image1Click(Sender: TObjec
229 var
230 i : integer;
231 begin
232 if (modeOperasi = modeDraw) and (polygonC
233 begin
234 polygon[polygonCount].X := posisi.X;
235 polygon[polygonCount].Y := posisi.Y;
236 polygonCount := polygonCount + 1;
237 polygon[polygonCount].X := ‐1;
238 LabelVertex.Caption := 'Vertex ' + intt
239
240 refreshCanvas();
241 end
242 else if (modeOperasi = modeEdit) and (edi
243 begin
244 if (editIndex = 1) or (editIndex = poly
245 begin
246 polygon[1].X := posisi.X;

6 dari 8 16/03/2024 22:00


Media pembelajaran polygon berbasis Delphi – Semesin https://www.project.semesin.com/2020/02/24/media-pembelajaran-polyg...

247 polygon[1].Y := posisi.Y;


248 polygon[polygonCount‐1].X := posisi.X
249 polygon[polygonCount‐1].Y := posisi.Y
250 end
251 else if (editIndex > 0) then
252 begin
253 polygon[editIndex].X := posisi.X;
254 polygon[editIndex].Y := posisi.Y;
255
256 end;
257
258 refreshCanvas();
259 end
260 else if (modeOperasi = modeInsert) and (e
261 begin
262 for i := polygonCount downto editIndex
263 begin
264 polygon[i+1].X := polygon[i].X;
265 polygon[i+1].Y := polygon[i].Y;
266 end;
267 polygon[editIndex].X := posisi.X;
268 polygon[editIndex].Y := posisi.Y;
269 polygonCount := polygonCount + 1;
270 refreshCanvas();
271 end;
272
273 end;
274
275
276 procedure TForm1.ButtonSaveClick(Sender: TO
277 var
278 Stream: TStream;
279 begin
280 Stream:= TFileStream.Create('polygon.bin'
281 Stream.WriteBuffer(polygonCount, SizeOf(i
282 Stream.WriteBuffer(polygon, (polygonCount
283 Stream.Free;
284 end;
285
286 procedure TForm1.ButtonLoadClick(Sender: TO
287 var
288 Stream: TStream;
289 begin
290 Stream:= TFileStream.Create('polygon.bin'
291 Stream.ReadBuffer(polygonCount, SizeOf(in
292 Stream.ReadBuffer(polygon, (polygonCount+
293 Stream.Free;
294
295 refreshCanvas();
296 LabelVertex.Caption := 'Vertex ' + inttos
297
298 end;
299
300 procedure TForm1.StringGrid1SelectCell(Send

7 dari 8 16/03/2024 22:00


Media pembelajaran polygon berbasis Delphi – Semesin https://www.project.semesin.com/2020/02/24/media-pembelajaran-polyg...

301 ARow: Integer; var CanSelect: Boolean);


302 begin
303 editIndex := ARow ‐ 1;
304 refreshCanvas();
305 end;
306
307 end.


8 dari 8 16/03/2024 22:00

Anda mungkin juga menyukai