Minggu, 16 Februari 2020

Membuat Garis Objek Grafik Sederhana - Pratikum Grafika Komputer


MEMBUAT GARIS OBJEK GRAFIKA SEDERHANA



Kali ini kita akan membahas tentang membuat Garis Objek Sederhana. Menggunakan Aplikasi Delphi7. Selamat belajar, mudah-mudahan membantu. Thanks.

----------------------------------------------------------------------
public
    { Public declarations }
    procedure tampilkan_coloref(var image : TImage; warna : colorref);
    procedure clear_layar;
    procedure reset_data;
    procedure gambar_object;
    function buka_warna(var image : TImage): colorref;
    function jenis_pen:Tpenstyle;
    function jenis_brush:Tbrushstyle;
end;

var
  Form1: TForm1;


implementation

{$R *.dfm}

type
  Tdata = record
    warna_pen   : colorref;
    warna_brush : colorref;
    Pt : integer;
    Style_pen : Tpenstyle;
    Style_brush : Tbrushstyle;
    Jenis : string [10];
    x1,y1,x2,y2 : integer;
    curve  : array of tpoint;
    NCurve : integer;
  end;

var data : array of tdata;
    NData : integer;
    warna_pen   : colorref;
    warna_brush : colorref;
    layar : tbitmap;
    start_draw : boolean;

procedure tform1.tampilkan_coloref(var image : TImage; warna : colorref);
begin
  image.Canvas.Pen.Color := clblack;
  image.Canvas.Pen.Width := 1;
  image.Canvas.Brush.Color := warna;
  image.Canvas.Rectangle(0,0,image.Width,image.Height);
end;


procedure tform1.clear_layar;
begin
  layar := tbitmap.Create;
  layar.Width := 700;
  layar.Height := 300;
end;

procedure TForm1.FormCreate(Sender: TObject);
var  i : integer;
begin
  warna_pen := clblack;
  warna_brush := clwhite;
  tampilkan_coloref(image2,warna_pen);
  tampilkan_coloref(image3,warna_brush);
  combobox1.Clear;
  for i := 1 to 10 do begin
    combobox1.Items.Add(inttostr(i));
  end;
  combobox1.ItemIndex := 0;
  clear_layar;
  image1.Picture.Bitmap := layar;
  reset_data;
  start_draw := false;
end;

procedure tform1.reset_data;
begin
  NData := 0;
  data := nil;
  setlength(data,ndata + 1);
  image1.Picture.Bitmap := layar;
  label5.Caption := 'Total objek : '+ inttostr(nData);
end;

function Tform1.buka_warna(var image : TImage): colorref;
begin
  if colordialog1.Execute = true then
  begin
    result := colordialog1.Color;
    tampilkan_coloref(image,result);
  end;
end;


function tform1.jenis_pen:Tpenstyle;
begin
   if radiobutton1.Checked = true then result := psSolid          ;
   if radiobutton2.Checked = true then result := psDash           ;
   if radiobutton3.Checked = true then result := psDot            ;
   if radiobutton4.Checked = true then result := psDashDot        ;
   if radiobutton5.Checked = true then result := psDashDotDot     ;
   if radiobutton6.Checked = true then result := psClear          ;
   if radiobutton7.Checked = true then result := psInsideFrame;   ;
end;

function tform1.jenis_brush:Tbrushstyle;
begin
  if radiobutton14.Checked = true then result := bsSolid;
  if radiobutton15.Checked = true then result := bsClear           ;
  if radiobutton16.Checked = true then result := bsHorizontal            ;
  if radiobutton17.Checked = true then result := bsVertical        ;
  if radiobutton18.Checked = true then result := bsFDiagonal     ;
  if radiobutton19.Checked = true then result := bsBDiagonal          ;
  if radiobutton20.Checked = true then result := BsCross   ;
  if radiobutton21.Checked = true then result := bsDiagCross;   ;
end;

procedure tform1.gambar_object;
var i,xa,xb,xc,ya,yb,yc,j : integer;
begin
  if ndata > 0 then
  begin
    image1.Picture.Bitmap := layar;
    for i:= 1 to ndata do begin
      if data[i].Jenis = 'Titik' then
      begin
        image1.Canvas.Pen.Style := pssolid;
        image1.Canvas.Brush.Style := bssolid;
        image1.Canvas.Pen.Width := data[i].Pt;
        image1.Canvas.Pen.Color:= data[i].warna_pen;
        image1.Canvas.Brush.Color:= data[i].warna_pen;
        image1.Canvas.Ellipse(data[i].x1-1,data[i].y1-1,data[i].x1+1,data[i].y1+1);
      end;
      if data[i].Jenis = 'Garis' then
      begin
        image1.Canvas.Pen.Style := data[i].Style_pen;
        image1.Canvas.Brush.Style := bssolid;
        image1.Canvas.Pen.Width := data[i].Pt;
        image1.Canvas.Pen.Color:= data[i].warna_pen;
        image1.Canvas.Brush.Color:= data[i].warna_pen;
        image1.Canvas.MoveTo(data[i].x1,data[i].y1);
        image1.Canvas.LineTo(data[i].x2,data[i].y2);
      end;
      if data[i].Jenis = 'Segitiga' then
      begin
        image1.Canvas.Pen.Style := data[i].Style_pen;
        image1.Canvas.Pen.Width := data[i].Pt;
        image1.Canvas.Pen.Color:= data[i].warna_pen;
        image1.Canvas.Brush.Style := data[i].Style_brush;
        image1.Canvas.Brush.Color:= data[i].warna_brush;
        xa := data[i].x1;
        if data[i].x2>data[i].x1 then
        begin
          xb := data[i].x1-abs(data[i].x1-data[i].x2);
          xc := data[i].x2;
        end
        else
        begin
          xc := data[i].x1+abs(data[i].x1-data[i].x2);
          xb := data[i].x2;
        end;

        ya := data[i].y1 - ((data[i].y2-data[i].y1));
        yb := data[i].y2;
        image1.Canvas.Polygon([point(xa,ya),point(xb,yb),point(xc,yb)]);
      end;

      if data[i].Jenis = 'Kotak' then
      begin
        image1.Canvas.Pen.Style := data[i].Style_pen;
        image1.Canvas.Pen.Width := data[i].Pt;
        image1.Canvas.Pen.Color:= data[i].warna_pen;
        image1.Canvas.Brush.Style := data[i].Style_brush;
        image1.Canvas.Brush.Color:= data[i].warna_brush;
        if data[i].x2>data[i].x1 then
        begin
          xa := data[i].x1-abs(data[i].x1-data[i].x2);
          xb := data[i].x2;
        end
        else
        begin
          xb := data[i].x1+abs(data[i].x1-data[i].x2);
          xa := data[i].x2;
        end;
        ya := data[i].y1 - ((data[i].y2-data[i].y1));
        yb := data[i].y2;
        image1.Canvas.Rectangle(xa,ya,xb,yb);
      end;
      if data[i].Jenis = 'Lingkaran' then
      begin
        image1.Canvas.Pen.Style := data[i].Style_pen;
        image1.Canvas.Pen.Width := data[i].Pt;
        image1.Canvas.Pen.Color:= data[i].warna_pen;
        image1.Canvas.Brush.Style := data[i].Style_brush;
        image1.Canvas.Brush.Color:= data[i].warna_brush;
        if data[i].x2>data[i].x1 then
        begin
          xa := data[i].x1-abs(data[i].x1-data[i].x2);
          xb := data[i].x2;
        end
        else
        begin
          xb := data[i].x1+abs(data[i].x1-data[i].x2);
          xa := data[i].x2;
        end;
        ya := data[i].y1 - ((data[i].y2-data[i].y1));
        yb := data[i].y2;
        image1.Canvas.Ellipse(xa,ya,xb,yb);
      end;

      if data[i].Jenis = 'Kurva' then
      begin
        image1.Canvas.Pen.Style := data[i].Style_pen;
        image1.Canvas.Pen.Width := data[i].Pt;
        image1.Canvas.Pen.Color:= data[i].warna_pen;
        image1.Canvas.Brush.Style := data[i].Style_brush;
        image1.Canvas.Brush.Color:= data[i].warna_brush;

        image1.Canvas.MoveTo(data[i].x1,data[i].y1);
        for j := 1 to data[i].NCurve do begin
          image1.Canvas.LineTo(data[i].curve[j].X,data[i].curve[j].y);
        end;
      end;


    end;
  end;

end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if radiobutton8.Checked = true then
  begin
    ndata := ndata + 1;
    setlength(data,ndata + 1);
    data[ndata].warna_pen := warna_pen;
    data[ndata].Pt := strtoint(combobox1.Text);
    data[ndata].Jenis := 'Titik';
    data[ndata].x1 := x;
    data[ndata].y1 := y;
  end;
  if radiobutton9.Checked = true then
  begin
    ndata := ndata + 1;
    setlength(data,ndata + 1);
    data[ndata].warna_pen := warna_pen;
    data[ndata].Style_pen := jenis_pen;
    data[ndata].Pt := strtoint(combobox1.Text);
    data[ndata].Jenis := 'Garis';
    data[ndata].x1 := x;
    data[ndata].y1 := y;
    data[ndata].x2 := x;
    data[ndata].y2 := y;
    start_draw := true;
  end;
  if radiobutton10.Checked = true then
  begin
    ndata := ndata + 1;
    setlength(data,ndata + 1);
    data[ndata].warna_pen := warna_pen;
    data[ndata].warna_brush := warna_brush;
    data[ndata].Style_pen := jenis_pen;
    data[ndata].Style_brush := jenis_brush;
    data[ndata].Pt := strtoint(combobox1.Text);
    data[ndata].Jenis := 'Segitiga';
    data[ndata].x1 := x;
    data[ndata].y1 := y;
    data[ndata].x2 := x;
    data[ndata].y2 := y;
    start_draw := true;
  end;
  if radiobutton11.Checked = true then
  begin
    ndata := ndata + 1;
    setlength(data,ndata + 1);
    data[ndata].warna_pen := warna_pen;
    data[ndata].warna_brush := warna_brush;
    data[ndata].Style_pen := jenis_pen;
    data[ndata].Style_brush := jenis_brush;
    data[ndata].Pt := strtoint(combobox1.Text);
    data[ndata].Jenis := 'Kotak';
    data[ndata].x1 := x;
    data[ndata].y1 := y;
    data[ndata].x2 := x;
    data[ndata].y2 := y;
    start_draw := true;
  end;
  if radiobutton12.Checked = true then
  begin
    ndata := ndata + 1;
    setlength(data,ndata + 1);
    data[ndata].warna_pen := warna_pen;
    data[ndata].warna_brush := warna_brush;
    data[ndata].Style_pen := jenis_pen;
    data[ndata].Style_brush := jenis_brush;
    data[ndata].Pt := strtoint(combobox1.Text);
    data[ndata].Jenis := 'Lingkaran';
    data[ndata].x1 := x;
    data[ndata].y1 := y;
    data[ndata].x2 := x;
    data[ndata].y2 := y;
    start_draw := true;
  end;
  if radiobutton13.Checked = true then
  begin
    ndata := ndata + 1;
    setlength(data,ndata + 1);
    data[ndata].warna_pen := warna_pen;
    data[ndata].Style_pen := jenis_pen;
    data[ndata].Pt := strtoint(combobox1.Text);
    data[ndata].Jenis := 'Kurva';
    data[ndata].x1 := x;
    data[ndata].y1 := y;
    data[ndata].x2 := x;
    data[ndata].y2 := y;
    data[ndata].NCurve := 1;
    data[ndata].curve := nil;
    setlength(data[ndata].curve, data[ndata].ncurve + 1);
    data[ndata].curve[data[ndata].NCurve].x := x;
    data[ndata].curve[data[ndata].NCurve].y := y;


    start_draw := true;
  end;
  label5.Caption := 'Total objek : '+ inttostr(nData);
  gambar_object;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  warna_pen :=buka_warna(image2);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  warna_brush :=buka_warna(image3);
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  label3.Caption := 'Koordinat('+inttostr(x)+':'+inttostr(y)+')';
  if (radiobutton9.Checked = true)or
     (radiobutton10.Checked = true)or
     (radiobutton11.Checked = true)or
     (radiobutton12.Checked = true)
  then
  begin
    if start_draw = true then
    begin
      data[ndata].x2 := x;
      data[ndata].y2 := y;
      gambar_Object;
    end;
  end;

    label3.Caption := 'Koordinat('+inttostr(x)+':'+inttostr(y)+')';
  if (radiobutton13.Checked = true)
  then
  begin
    if start_draw = true then
    begin
      data[ndata].NCurve := data[ndata].NCurve + 1;
      setlength(data[ndata].curve, data[ndata].ncurve + 1);
      data[ndata].curve[data[ndata].NCurve].x := x;
      data[ndata].curve[data[ndata].NCurve].y := y;
      data[ndata].x2 := x;
      data[ndata].y2 := y;
      gambar_Object;
    end;
  end;


end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if (radiobutton9.Checked  = true)or
     (radiobutton10.Checked = true)or
     (radiobutton11.Checked = true)or
     (radiobutton12.Checked = true)or
     (radiobutton13.Checked = true)
  then
  begin
    if start_draw = true then
    begin
      data[ndata].x2 := x;
      data[ndata].y2 := y;
      start_draw := false;
      gambar_Object;
    end;
  end;

end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  reset_data;
end;

procedure TForm1.Newproject1Click(Sender: TObject);
begin
  reset_data;
end;

procedure TForm1.Save1Click(Sender: TObject);
begin
  if savepicturedialog1.Execute = true then
  begin
    image1.Picture.SaveToFile(savepicturedialog1.FileName+'.bmp');
  end;
end;

end.

---------------------------------------------------------------------------------------------------
Previous Post
Next Post

1 komentar: