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_Blend; {‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐} { 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 a g e
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; Shift: TShiftState; X, Y: Integer); procedure Shape3MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; 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; begin {‐‐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 a g e
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]); end; procedure TForm1.Shape2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin if ColorDialog1.Execute then Shape2.Brush.Color:=ColorDialog1.Color; end; procedure TForm1.Shape3MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin if ColorDialog1.Execute then Shape3.Brush.Color:=ColorDialog1.Color; end; procedure TForm1.FormCreate(Sender: TObject); begin SpeedButton1Click(Sender); end; end. Tugas Buatlah Untuk komposisi minimal 4 warna 23 | P a g e
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 a g e
Edit5: TEdit; Label3: TLabel; Label4: TLabel; Label5: TLabel; procedure BitBtn1Click(Sender: TObject); procedure Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); private { Private declarations } public { Public declarations } end; var Form1: TForm1; ex,ye:integer; R,G,B:byte; implementation {$R *.dfm} procedure TForm1.BitBtn1Click(Sender: TObject); var i,j,Hasil:byte; begin for i:=0 to Image1.Height do begin for j:=0 to Image1.Width do begin R:=getRValue(Image1.Canvas.Pixels[j,i]); G:=getGValue(Image1.Canvas.Pixels[j,i]); B:=getBValue(Image1.Canvas.Pixels[j,i]); if (R
for i:=0 to Image2.Height do begin for j:=0 to Image2.Width do begin 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
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 a g e