PERCOBAAN 4 GRAFIKA KOMPUTER DENGAN DELPHI Pendahuluan Grafik selalu digambarkan pada object kanvas. Dengan konsep kanvasnya, pemrograman grafik menjadi lebih mudah. Fasilitas Grafik ini didefinisikan dalam unit graphics. Unit ini memanfaatkan apa yang disebut dengan GDI (Graphics Device Interface) yang disediakan oleh windows dan mengekapsulasi fungsi fungsinya, sehingga implementasi grafik pada aplikasi windows menjadi mudah. Program 1 unit Warna_Bl { } { Color Blending} { } interface uses Windows, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Buttons, ExtCtrls, StdCtrls, ComCtrls, jpeg; type TForm1 = class(tform) SpeedButton1: TSpeedButton; Shape1: TShape; 21 P age
Shape2: TShape; Shape3: TShape; Shape4: TShape; LblWarna1: TLabel; LblWarna2: TLabel; LblHasil: TLabel; ColorDialog1: TColorDialog; Label7: TLabel; Label8: TLabel; Label9: TLabel; procedure SpeedButton1Click(Sender: TObject); procedure Shape2MouseDown(Sender: TObject; Button: TMouseButton; procedure Shape3MouseDown(Sender: TObject; Button: TMouseButton; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } var Form1: TForm1; implementation {$R *.dfm} { COLOR BLENDING } procedure TForm1.SpeedButton1Click(Sender: TObject); var Clr1, Clr2, Clr3: TColor; R1, R2, R3, G1, G2, G3, B1, B2, B3: Byte; { warna kuning } Clr1:=ColorToRGB(Shape2.Brush.Color); R1:=GetRValue(Clr1); G1:=GetGValue(Clr1); B1:=GetBValue(Clr1); { warna merah } Clr2:=ColorToRGB(Shape3.Brush.Color); 22 P age
R2:=GetRValue(Clr2); G2:=GetGValue(Clr2); B2:=GetBValue(Clr2); { warna gabungan } R3:=Round((R1+R2)/2); G3:=Round((G1+G2)/2); B3:=Round((B1+B2)/2); Clr3:=RGB(R3, G3, B3); Shape4.Brush.Color:=Clr3; { label komponen warna } LblWarna1.Caption:=Format('R=%d G=%d B=%d', [R1, G1, B1]); LblWarna2.Caption:=Format('R=%d G=%d B=%d', [R2, G2, B2]); LblHasil.Caption :=Format('R=%d G=%d B=%d', [R3, G3, B3]); procedure TForm1.Shape2MouseDown(Sender: TObject; Button: TMouseButton; if ColorDialog1.Execute then Shape2.Brush.Color:=ColorDialog1.Color; procedure TForm1.Shape3MouseDown(Sender: TObject; Button: TMouseButton; if ColorDialog1.Execute then Shape3.Brush.Color:=ColorDialog1.Color; procedure TForm1.FormCreate(Sender: TObject); SpeedButton1Click(Sender); end. Tugas Buatlah Untuk komposisi minimal 4 warna 23 P age
Program 2 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ExtCtrls; type TForm1 = class(tform) Image1: TImage; Image2: TImage; BitBtn1: TBitBtn; Edit1: TEdit; Edit2: TEdit; Label1: TLabel; Label2: TLabel; Edit3: TEdit; Edit4: TEdit; 24 P age
Edit5: TEdit; Label3: TLabel; Label4: TLabel; Label5: TLabel; procedure BitBtn1Click(Sender: TObject); procedure Image1MouseDown(Sender: TObject; Button: TMouseButton; private { Private declarations } public { Public declarations } var Form1: TForm1; ex,ye:integer; R,G,B:byte; implementation {$R *.dfm} procedure TForm1.BitBtn1Click(Sender: TObject); var i,j,hasil:byte; for i:=0 to Image1.Height do for j:=0 to Image1.Width do R:=getRValue(Image1.Canvas.Pixels[j,i]); G:=getGValue(Image1.Canvas.Pixels[j,i]); B:=getBValue(Image1.Canvas.Pixels[j,i]); if (R<G) and (R<B) Then BEGIN R:=0; G:=0; B:=0; END; // hasil:=round((r+g+b)/3); image2.canvas.pixels[j,i]:=rgb(r,g,b); // image2.refresh; 25 P age
for i:=0 to Image2.Height do for j:=0 to Image2.Width do R:=getRValue(Image2.Canvas.Pixels[j,i]); G:=getGValue(Image2.Canvas.Pixels[j,i]); B:=getBValue(Image2.Canvas.Pixels[j,i]); if (R>200) and (G>200) THEN BEGIN if (B<G) and (B<R) Then BEGIN R:=0; G:=0; B:=0; END; // hasil:=round((r+g+b)/3); image2.canvas.pixels[j,i]:=rgb(r,g,b); // image2.refresh; procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton; ex:=x; ye:=y; R:=getRValue(Image1.Canvas.Pixels[X,Y]); G:=getGValue(Image1.Canvas.Pixels[X,Y]); B:=getBValue(Image1.Canvas.Pixels[X,Y]); EDIT1.Text:=inttostr(ex); EDIT2.Text:=inttostr(ye); edit3.text:=inttostr(r); edit4.text:=inttostr(g); edit5.text:=inttostr(b); end. Petunjuk Praktikum 26 P age
1. Buatlah program 1 dan program 2 menggunakan Delphi 7 dengan memperhatikan component palete yang digunakan 2. Running program dan Catat error yang terjadi dan solusi perbaikan 3. Buatlah Algoritma dan FlowChart pada kedua program tersebut 27 P age