Dokumen - SPK105303 - STMIK EL RAHMA Materi 2 Grafis

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 MoveTo, LineTo, Rectangle, dan Ellipsi
bentuk
Menampilkan teks
TextOut,
TextHeight,
TextWidth,
dan
TextRext method

Arsiran
FillRect dan FloodFill
Sedan Mengubah grafik/teks
Pen, Brush, dan Font property
g
Manipulasi pixel
Pixels property
Menyalin
dan Draw, StrectDraw, BrushCopy, CopyRect
menggabung gambar
method dan CopyMode property
Renda Call
fungsi-fungsi Handle property
h
Windows GDI
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
Label1
Label2
Label3
Label4
Label5
Button1

Property
Caption
Size
Caption
Caption
Caption
Caption
Caption

Nilai

Primitif Gambar
15
X1
Y1
X2
Y2
&Garis

Button2
Button3
Button4
Button5
Button6
Button7
Button8
Button9
Button10
Button11
PaintBox1
ColorDialog1

Edit1
Edit2
Edit3
Edit4
Form1
Jika perubahan property
dibawah ini:

Caption
Caption
Caption
Caption
Caption
Caption
Caption
Caption
Caption
Caption

&Persegi

&Elips
&Acak
&Rumah
&Mobil
&Bunga
&Kubus
&Hapus
&Warna
&Selesai

Text
Text
Text
Text
Caption
anda benar, maka form

0
0
200

100
Gambar Gambaran...
anda akan terlihat seperti

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;
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 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
{ Private declarations }
public
a,b,c,d : Integer;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
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
a:=STRtoINT(edit1.text);
b:=STRtoINT(edit2.text);
c:=STRtoINT(edit3.text);

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.MoveTo(lebar div 4,atap);

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
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 !