MODUL V
GRAFIK
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 Sedan
g
Mengubah grafik/teks Pen, Brush, dan Font property Manipulasi pixel Pixels property
Menyalin dan
menggabung gambar Draw, StrectDraw, BrushCopy, CopyRectmethod dan CopyMode property Renda
h CallWindows GDIfungsi-fungsi 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 Grafik.
Kemudian anda ubah propertinya dari Object Inspector:
Nama Object Property Nilai
Label1 Caption
Size Primitif Gambar15
Label2 Caption X1
Label3 Caption Y1
Label4 Caption X2
Label5 Caption Y2
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 0
Edit2 Text 0
Edit3 Text 200
Edit4 Text 100
Form1 Caption Gambar Gambaran...
Jika perubahan property anda benar, maka form anda akan terlihat seperti dibawah ini:
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 = class(TForm) Label1: TLabel;
Edit2: TEdit;
ColorDialog1: TColorDialog; PaintBox1: TPaintBox; Button9: TButton; Button10: TButton; Button11: TButton;
procedure Button1Click(Sender: TObject); procedure Button11Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button9Click(Sender: TObject); procedure Button10Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Button5Click(Sender: TObject); private
procedure TForm1.Button1Click(Sender: TObject); begin
a:=STRtoINT(edit1.text); b:=STRtoINT(edit2.text); c:=STRtoINT(edit3.text); d:=STRtoINT(edit4.text);
PaintBox1.Canvas.MoveTo(a,b); PaintBox1.Canvas.LineTo(c,d);
end;
procedure TForm1.Button11Click(Sender: TObject); begin
close;
end;
procedure TForm1.Button2Click(Sender: TObject); begin
d:=STRtoINT(edit4.text);
PaintBox1.Canvas.Rectangle(a,b,c,d);
end;
procedure TForm1.Button3Click(Sender: TObject); begin
a:=STRtoINT(edit1.text); b:=STRtoINT(edit2.text); c:=STRtoINT(edit3.text); d:=STRtoINT(edit4.text);
PaintBox1.Canvas.Ellipse(a,b,c,d);
end;
procedure TForm1.Button4Click(Sender: TObject);
Var i : integer;
begin
For i:=1 to 1000 Do Begin
PaintBox1.Canvas.Pixels[Random(PaintBox1.Width), Random(PaintBox1.height)]:=
RGB(Random(256),Random(256),Random(256)); Application.ProcessMessages;
End;
end;
procedure TForm1.Button9Click(Sender: TObject); begin
paintBox1.Hide; PaintBox1.Show;
end;
procedure TForm1.Button10Click(Sender: TObject); begin
ColorDialog1.Execute;
PaintBox1.Color := ColorDialog1.Color;
end;
procedure TForm1.FormCreate(Sender: TObject); begin
PaintBox1.Canvas.Pen.Color := clBlack;
end;
procedure TForm1.Button5Click(Sender: 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.MoveTo(0,atap); PaintBox1.Canvas.LineTo(lebar,atap);
PaintBox1.Canvas.LineTo(lebar - lebar div 4,0); PaintBox1.Canvas.LineTo(lebar div 4,0);
PaintBox1.Canvas.LineTo(0,atap); // gambar dinding:
PaintBox1.Canvas.LineTo(lebar div 4,tinggi -1);
PaintBox1.Canvas.LineTo(lebar - lebar div 4,tinggi -1); PaintBox1.Canvas.LineTo(lebar - lebar div 4,atap); // gambar pintu:
with PaintBox1.canvas DO Begin
Pen.Color := clBlue; Pen.Width := 5;
MoveTo(tengah - lebar div 8, tinggi -1); LineTo(tengah + lebar div 8, tinggi -1); LineTo(tengah + lebar div 8, tinggi div 2); LineTo(tengah - lebar div 8, tinggi div 2); LineTo(tengah - lebar div 8, tinggi -1); MoveTo(tengah, tinggi -1);
LineTo(tengah, tinggi div 2); Pen.Color := clBlack;
Pen.Width := 1;
End; end;
end.
C. LATIHAN