BAB V PEMROGRAMAN GRAFIS
A. DESAIN FORM
Delphi menyediakan Canvas untuk tempat penggambaran. Canvas memiliki Pen, Brush, Color yang dapat digunakan untuk menggambar.
Selain itu juga mempunya method MoveTo, LineTo, Circle, Ellipse dan lain-lain yang memungkinkan programmer menggambar bentuk-bentuk
primitif dengan mudah. Berikut ini adalah tabel method pokok pada Canvas:
Level Operasi
Tools
Tinggi Menggambar garis
dan bentuk MoveTo, LineTo, Rectangle, dan
Ellipsi Menampilkan teks
TextOut, TextHeight, TextWidth, dan TextRext method
Arsiran FillRect dan FloodFill
Sedang Mengubah grafikteks
Pen, Brush, dan Font property Manipulasi pixel
Pixels property Menyalin
dan menggabung
gambar Draw, StrectDraw, BrushCopy,
CopyRect method
dan CopyMode property
Rendah Call fungsi-fungsi
Windows GDI Handle property
Pada bagian ini kita akan mempelajari bagaimana bentuk-bentuk dasar primitif grafik yang didukung oleh delphi. Titik, garis, persegi,
dan elips adalah contoh bentuk dasar grafik, yang daripadanya dapat digambar bentuk yang lebih kompleks.
Berikut ini desain formnya:
Gambar 5.1. Desain Form Grafis
Kemudian anda ubah propertinya dari Object Inspector:
Nama Object Property
Nilai
Label1 Caption
Size Primitif Gambar
15 Label2
Caption X1
Label3 Caption
Y1 Label4
Caption X2
Label5 Caption
Y2 Button1
Caption Garis
Button2 Caption
Persegi Button3
Caption Elips
Button4 Caption
Acak Button5
Caption Rumah
Button6 Caption
Mobil Button7
Caption Bunga
Button8 Caption
Kubus Button9
Caption Hapus
Button10 Caption
Warna Button11
Caption Selesai
PaintBox1 ColorDialog1
Edit1 Text
Edit2 Text
Edit3 Text
200 Edit4
Text 100
Form1 Caption
Gambar Gambaran... Jika perubahan property anda benar, maka form anda akan terlihat
seperti terlihat pada gambar 5.2.
Gambar 5.2. Desain Form Setelah Perubahan B. SOURCE CODE
Berikut ini kode untuk tombol Garis, Persegi, Elips, Acak, Hapus, Warna, Rumah dan Selesai.
unit Ugrafik; interface
uses Windows, Messages, SysUtils, Classes, Graphics,
Controls, Forms, Dialogs, ExtCtrls, StdCtrls;
type TForm1 = classTForm
Label1: TLabel; Label2: TLabel;
Label3: TLabel; Label4: TLabel;
Label5: TLabel; Edit1: TEdit;
Edit2: TEdit; Edit3: TEdit;
Edit4: TEdit; Button1: TButton;
Button2: TButton; Button3: TButton;
Button4: TButton; Button5: TButton;
Button6: TButton;
Button7: TButton; Button8: TButton;
ColorDialog1: TColorDialog; PaintBox1: TPaintBox;
Button9: TButton; Button10: TButton;
Button11: TButton; procedure Button1ClickSender: TObject;
procedure Button11ClickSender: TObject; procedure Button2ClickSender: TObject;
procedure Button3ClickSender: TObject; procedure Button4ClickSender: TObject;
procedure Button9ClickSender: TObject; procedure Button10ClickSender: TObject;
procedure FormCreateSender: TObject; procedure Button5ClickSender: TObject;
private { Private declarations }
public a,b,c,d : Integer;
{ Public declarations } end;
var Form1: TForm1;
implementation {R .DFM}
procedure TForm1.Button1ClickSender: TObject; begin
a:=STRtoINTedit1.text; b:=STRtoINTedit2.text;
c:=STRtoINTedit3.text; d:=STRtoINTedit4.text;
PaintBox1.Canvas.MoveToa,b; PaintBox1.Canvas.LineToc,d;
end; procedure TForm1.Button11ClickSender: TObject;
begin close;
end; procedure TForm1.Button2ClickSender: TObject;
begin a:=STRtoINTedit1.text;
b:=STRtoINTedit2.text;
c:=STRtoINTedit3.text; d:=STRtoINTedit4.text;
PaintBox1.Canvas.Rectanglea,b,c,d; end;
procedure TForm1.Button3ClickSender: TObject; begin
a:=STRtoINTedit1.text; b:=STRtoINTedit2.text;
c:=STRtoINTedit3.text; d:=STRtoINTedit4.text;
PaintBox1.Canvas.Ellipsea,b,c,d; end;
procedure TForm1.Button4ClickSender: TObject; Var i : integer;
begin For i:=1 to 1000 Do
Begin PaintBox1.Canvas.Pixels[RandomPaintBox1.Width,
RandomPaintBox1.height]:= RGBRandom256,Random256,Random256;
Application.ProcessMessages; End;
end; procedure TForm1.Button9ClickSender: TObject;
begin paintBox1.Hide;
PaintBox1.Show; end;
procedure TForm1.Button10ClickSender: TObject; begin
ColorDialog1.Execute; PaintBox1.Color := ColorDialog1.Color;
end; procedure TForm1.FormCreateSender: TObject;
begin PaintBox1.Canvas.Pen.Color := clBlack;
end; procedure TForm1.Button5ClickSender: TObject;
Var tinggi,lebar,atap,tengah :integer;
begin lebar := PaintBox1.Width;
tengah:= lebar div 2; tinggi:= PaintBox1.Height;
atap := tinggi div 3; gambar atap:
PaintBox1.Canvas.MoveTo0,atap; PaintBox1.Canvas.LineTolebar,atap;
PaintBox1.Canvas.LineTolebar - lebar div 4,0; PaintBox1.Canvas.LineTolebar div 4,0;
PaintBox1.Canvas.LineTo0,atap; gambar dinding:
PaintBox1.Canvas.MoveTolebar div 4,atap; PaintBox1.Canvas.LineTolebar div 4,tinggi -1;
PaintBox1.Canvas.LineTolebar -
lebar div
4,tinggi -1; PaintBox1.Canvas.LineTolebar
- lebar
div 4,atap;
gambar pintu: with PaintBox1.canvas DO
Begin Pen.Color := clBlue;
Pen.Width := 5; MoveTotengah - lebar div 8, tinggi -1;
LineTotengah + lebar div 8, tinggi -1; LineTotengah + lebar div 8, tinggi div 2;
LineTotengah - lebar div 8, tinggi div 2; LineTotengah - lebar div 8, tinggi -1;
MoveTotengah, tinggi -1; LineTotengah, tinggi div 2;
Pen.Color := clBlack; Pen.Width := 1;
End; end;
end. C. LATIHAN
1. Lengkapi program diatas untuk tombol Mobil, Bunga, dan Kubus 2. Lengkapi pula fasilitas program untuk mengganti warna garis
3. Buat aplikasi sederhana semacam PaintBrush milik Windows
BAB VI APLIKASI JAM DIGITAL DAN ANALOG