Saya selalu mendengar bahwa kanvas di Delphi sederhana dan nyaman digunakan. Sekarang saya menggunakan kanvas untuk mengimplementasikan representasi grafis dari mekanisme pohon sederhana. Sistem mendukung pemilihan simpul, gerakan, menyelamatkan pohon, pohon pembuka, dll. Rekursi dan pointer digunakan untuk kenyamanan.
Program ini ditulis dengan berantakan, selamat datang untuk berkomunikasi: [email protected]
Kode sumber adalah sebagai berikut:
unit u_tree;
antarmuka
penggunaan
Windows, pesan, sysutils, kelas, grafik, kontrol, formulir, dialog,
Extctrls, stdctrls, jpeg, menu, inifiles32;
jenis
Tobj = Rekam
Objid: String;
CenterX: Integer;
Centery: Integer;
Typeno: integer;
Dipilih: Boolean;
Fnode: string;
menunjukkan: boolean;
akhir;
Tfrm_tree = class (tform)
Panel1: tpanel;
PaintBox1: TPaintBox;
Panel2: tpanel;
Label1: tlabel;
Button2: tbutton;
Button1: tbutton;
Button3: tbutton;
Button4: tbutton;
Tombol5: tbutton;
Button6: tbutton;
MainMenu1: tmainmenu;
Fadestream1: tmenuitem;
Acakrandomselection1: tMenuitem;
Savedialog1: tsavedialog;
OpenDialog1: Topendialog;
Button7: tbutton;
Prosedur PaintBox1Mousedown (Pengirim: Tobject; Tombol: Tmousebutton;
Shift: tshiftate; x, y: integer);
Prosedur FormCreate (pengirim: Tobject);
Prosedur Button1Click (pengirim: Tobject);
Prosedur Button2Click (pengirim: Tobject);
Prosedur PaintBox1Paint (pengirim: TOBJEK);
Prosedur Button3Click (pengirim: Tobject);
Prosedur Button4Click (pengirim: Tobject);
Prosedur Button5Click (pengirim: Tobject);
Prosedur Button6Click (pengirim: Tobject);
Prosedur PaintBox1MouseUp (Pengirim: Tobject; Tombol: Tmousebutton;
Shift: tshiftate; x, y: integer);
Prosedur PaintBox1MoUseMove (Pengirim: Tobject; Shift: TshiftState; X,
Y: Integer);
Prosedur fadestream1click (pengirim: TOBJEK);
Prosedur RandomRandomSelection1Click (pengirim: TOBJEK);
Tombol Prosedur7Click (pengirim: TOBJEKS);
Pribadi
{Deklarasi Pribadi}
Toolno: Integer; // 1 Draw Dots, 2 Pilih 3 Lihat 4 Pindahkan 5 Sub-Move
beginx, begin, endx, endy: integer;
diklik: Boolean;
Olst: tlist;
Selid: string;
Root: Boolean;
Searilid: Integer;
Prosedur DrawNode (ID: String);
Prosedur addObj (id: string; x, y: integer; typeno: integer; dipilih: boolean; fnode: string; menunjukkan: boolean);
fungsi getobj (id: string): tobj;
fungsi getpobj (id: string): pointer;
fungsi getSelect: tobj;
Fungsi Haveselect: Boolean;
fungsi clickobj (x, y: integer): string;
Prosedur Drawfull;
Prosedur Setselected (x, y: integer);
function setshowsel (x, y: integer): tobj;
Prosedur SetFNode (ID: String);
Prosedur setCnode (id: string);
Prosedur Clearshowed;
Prosedur ClearCanvas;
Prosedur MOVEOBJ (DX, DY: Integer);
Prosedur Movenode (DX, DY: Integer; ID: String);
Prosedur Movelocal (DX, DY: Integer);
//prosedur
publik
{Deklarasi Publik}
akhir;
var
FRM_TREE: TFRM_TREE;
Pelaksanaan
{$ R *.dfm}
{TForm1}
Prosedur tfrm_tree.drawnode (id: string);
var
Oldbrushcolor: Tcolor;
Oldpencolor: Tcolor;
OBJ: TOBJ;
Mulai
obj: = getobj (id);
dengan frm_tree.paintbox1.canvas lakukan
Mulai
Jika Obj.Medar itu
Mulai
Oldbrushcolor: = sikat.color;
Oldpencolor: = pen.color;
Jika Obj.PoStle, maka
Mulai
Pen.color: = rgb (255,0,0);
akhir;
Sikat.color: = $ 00ff31ff;
Ellipse (obj.centerx-10, obj.centery-10, obj.centerx+10, obj.centery+10);
Pen.color: = $ 00ff31ff;
Jika obj.typeno> 0 lalu
Mulai
moveto (obj.centerx, obj.centery);
lineto (getobj (obj.fnode) .centerx, getobj (obj.fnode) .Centery);
akhir;
pen.color: = oldpencolor;
sikat.color: = oldbrushcolor;
akhir;
akhir;
akhir;
Prosedur TFRM_TREE.Paintbox1mousedown (pengirim: Tobject;
Tombol: Tmousebutton;
var
Curobj: Tobj;
Mulai
jika tombol = mbleft lalu
Mulai
Kasus Toolno dari
1:
Mulai
Searilid: = Searilid+1;
Jika root maka
Mulai
Addobj (inttoStr (searilid), x, y, 0, false, '', true);
Drawnode (inttoStr (searilid));
Root: = false;
akhir
kalau tidak
Mulai
Jika Haveelect maka
Mulai
Addobj (inttoStr (searilid), x, y, 1, false, getSelect.objid, true);
Drawnode (inttoStr (searilid));
label1.caption: = 'Tambahkan simpul, id:'+inttoStr (searilid);
akhir
kalau tidak
Mulai
label1.caption: = 'Silakan pilih node!';
akhir;
akhir;
akhir;
2:
Mulai
setSelected (x, y);
akhir;
3: // Lihat
Mulai
// clearcanvas;
Curobj: = setShowsel (x, y);
Jika curobj.objid <> '' lalu
Mulai
Clearshowed;
Curobj: = setShowsel (x, y);
Curobj. ditunjukkan: = true;
setFnode (curobj.fnode);
setCnode (curobj.objid);
Drawfull;
akhir;
akhir;
4: // ponsel
Mulai
jika clickoBj (x, y) <> '' maka diklik: = true;
beginx: = x;
Awal: = y;
akhir;
5:
Mulai
jika clickoBj (x, y) <> '' maka diklik: = true;
beginx: = x;
Awal: = y;
akhir;
akhir;
akhir
kalau tidak
Mulai
setSelected (x, y);
akhir;
akhir;
Prosedur TFRM_TREE.FORMCREATE (pengirim: TOBJEKS);
Mulai
Olst: = tlist.create;
Toolno: = 0;
Root: = true;
Selid: = '';
Searilid: = 0;
diklik: = false;
dengan PaintBox1.Canvas lakukan
Mulai
Sikat. Color: = Clwhite;
Fillrect (rect (0,0, catbox1.width, paintbox1.height));
akhir;
akhir;
Prosedur TFRM_TREE.BUTTON1CLICK (pengirim: TOBJEKS);
Mulai
Toolno: = 1;
akhir;
Prosedur TFRM_TREE.BUTTON2CLICK (pengirim: TOBJEKS);
Mulai
Toolno: = 2;
akhir;
Prosedur tfrm_tree.addobj (id: string; x, y, typeno: integer;
dipilih: boolean;
var
Obj: ^tobj;
Mulai
baru (OBJ);
obj.objid: = id;
obj.centerx: = x;
Obj.Centery: = y;
obj.typeno: = typeno;
Obj.SPESTECTED: = Dipilih;
obj.fnode: = fnode;
OBJ.SHOWED: = Tunjukkan;
Olst.add (OBJ);
akhir;
fungsi tfrm_tree.getobj (id: string): tobj;
var
I, J: Integer;
Mulai
J: = olst.count;
untuk i: = 0 hingga j-1 lakukan
Mulai
jika tobj (olst.items [i]^). Objid = ID lalu
Mulai
Hasil: = Tobj (olst.items [i]^);
Merusak;
akhir;
akhir;
akhir;
fungsi tfrm_tree.getselect: tobj;
var
I, J: Integer;
Mulai
J: = olst.count;
untuk i: = 0 hingga j-1 lakukan
Mulai
jika tobj (olst.items [i]^). Dipilih lalu
Mulai
Hasil: = Tobj (olst.items [i]^);
Merusak;
akhir;
akhir;
akhir;
fungsi tfrm_tree.haveselect: boolean;
var
I, J: Integer;
Mulai
Hasil: = false;
J: = olst.count;
untuk i: = 0 hingga j-1 lakukan
Mulai
jika tobj (olst.items [i]^). Dipilih lalu
Mulai
Hasil: = true;
Merusak;
akhir;
akhir;
akhir;
Prosedur tfrm_tree.drawfull;
var
I, J: Integer;
Mulai
//Paintbox1.canvas.fillrect(rect(0,0,paintbox1.width,paintbox1.height));
Clearcanvas;
J: = olst.count;
untuk i: = 0 hingga j-1 lakukan
Mulai
Drawnode (tobj (olst.items [i]^). Objid);
akhir;
akhir;
Prosedur TFRM_TREE.Paintbox1Paint (pengirim: TOBJEKS);
Mulai
Drawfull;
akhir;
Prosedur tfrm_tree.setselected (x, y: integer);
var
I, J: Integer;
Mulai
J: = olst.count;
untuk i: = 0 hingga j-1 lakukan
Mulai
Tobj (olst.items [i]^). Dipilih: = false;
if (tobj (olst.items [i]^). centerx-10 <x) dan (tobj (olst.items [i]^). Centerx+10> x)
dan (tobj (olst.items [i]^). centery-10 <y) dan (tobj (olst.items [i]^). Centery+10> y) kemudian
Mulai
Tobj (olst.items [i]^). Dipilih: = true;
Label1.caption: = 'Memilih ID simpul:'+ tobj (olst.items [i]^). Objid;
akhir;
akhir;
Drawfull;
akhir;
Prosedur tfrm_tree.button3click (pengirim: tobject);
Mulai
Toolno: = 3;
akhir;
fungsi tfrm_tree.setshowsel (x, y: integer): tobj;
var
I, J: Integer;
Mulai
J: = olst.count;
untuk i: = 0 hingga j-1 lakukan
Mulai
Tobj (olst.items [i]^). Dipilih: = false;
if (tobj (olst.items [i]^). centerx-10 <x) dan (tobj (olst.items [i]^). Centerx+10> x)
dan (tobj (olst.items [i]^). centery-10 <y) dan (tobj (olst.items [i]^). Centery+10> y) kemudian
Mulai
Tobj (olst.items [i]^). Menunjukkan: = true;
Label1.caption: = 'lihat id node:'+ tobj (olst.items [i]^). Objid;
Hasil: = Tobj (olst.items [i]^);
Merusak;
akhir;
akhir;
akhir;
Prosedur tfrm_tree.clearshowed;
var
I, J: Integer;
Mulai
J: = olst.count;
untuk i: = 0 hingga j-1 lakukan
Mulai
Tobj (olst.items [i]^). Menunjukkan: = false;
akhir;
akhir;
Prosedur TFRM_TREE.SETFNode (ID: String);
var
Curobj:^tobj;
Mulai
Jika id <> '' maka
Mulai
// baru (Curobj);
Curobj: = getPobj (id);
Sedangkan curobj^.typeno = 1 lakukan
Mulai
CuroBj^.Showed: = true;
Curobj: = getPobj (Curobj^.fnode);
akhir;
CuroBj^.Showed: = true;
// buang (curobj);
akhir;
akhir;
Prosedur TFRM_TREE.SETCNODE (ID: String);
var
Curobj:^tobj;
I, J: Integer;
Mulai
// curobj: = getobj (id);
J: = olst.count;
untuk i: = 0 hingga j-1 lakukan
Mulai
jika tobj (olst.items [i]^). fnode = id lalu
Mulai
Curobj: = getPobj (tobj (olst.items [i]^). Objid);
CuroBj^.Showed: = true;
setCnode (curobj^.objid);
akhir;
akhir;
akhir;
Prosedur TFRM_TREE.ClearCanvas;
Mulai
//Paintbox1.canvas
Paintbox1.canvas.fillrect (Rect (0,0, Paintbox1.width, Paintbox1.height));
akhir;
Prosedur tfrm_tree.button4click (pengirim: tobject);
Mulai
diklik: = false;
Paintbox1.canvas.fillrect (Rect (0,0, Paintbox1.width, Paintbox1.height));
Olst.clear;
Root: = true;
Selid: = '';
Searilid: = 0;
{dengan paintbox1.canvas lakukan
Mulai
Pen.width: = 2;
Pen.color: = clblack;
Pen.Style: = psclear;
Kuas.
Sikat. Color: = Clwhite;
Persegi panjang (0,0, paintbox1.width, paintbox1.height);
akhir;}
akhir;
Prosedur TFRM_TREE.BUTTON5CLICK (pengirim: TOBJEKS);
var
I, J: Integer;
Mulai
J: = olst.count;
untuk i: = 0 hingga j-1 lakukan
Mulai
tobj (olst.items [i]^). Menunjukkan: = true;
akhir;
Drawfull;
akhir;
fungsi tfrm_tree.getpobj (id: string): pointer;
var
I, J: Integer;
Mulai
Hasil: = nil;
J: = olst.count;
untuk i: = 0 hingga j-1 lakukan
Mulai
jika tobj (olst.items [i]^). Objid = ID lalu
Mulai
Hasil: = olst.items [i];
Merusak;
akhir;
akhir;
akhir;
fungsi tfrm_tree.clickobj (x, y: integer): string;
var
I, J: Integer;
Mulai
Hasil: = '';
J: = olst.count;
setSelected (x, y);
untuk i: = 0 hingga j-1 lakukan
Mulai
if (tobj (olst.items [i]^). centerx-10 <x) dan (tobj (olst.items [i]^). Centerx+10> x)
dan (tobj (olst.items [i]^). centery-10 <y) dan (tobj (olst.items [i]^). Centery+10> y) kemudian
Mulai
Label1.caption: = 'Klik Node ID:'+ TOBJ (Olst.Items [i]^). Objid;
Hasil: = Tobj (olst.items [i]^). Objid;
Merusak;
akhir;
akhir;
akhir;
Prosedur tfrm_tree.button6click (pengirim: tobject);
Mulai
Toolno: = 4;
akhir;
Prosedur tfrm_tree.moveobj (dx, dy: integer);
var
I, J: Integer;
Mulai
J: = olst.count;
untuk i: = 0 hingga j-1 lakukan
Mulai
Tobj (olst.items [i]^). Centerx: = tobj (olst.items [i]^). Centerx+dx;
Tobj (olst.items [i]^). Centery: = tobj (olst.items [i]^). Centery+dy;
akhir;
// Drawfull;
akhir;
Prosedur TFRM_TREE.Paintbox1MouseUp (pengirim: TOBJEKS; Tombol: TMousebutton;
Shift: tshiftate; x, y: integer);
Mulai
Kasus Toolno dari
4:
Mulai
Jika diklik
Mulai
endx: = x;
Endy: = y;
moveobj ((endx-beginx), (endy-beginy));
akhir;
diklik: = false;
akhir;
5:
Mulai
diklik: = false;
akhir;
akhir;
akhir;
Prosedur TFRM_TREE.PaintBOX1MOUSEMOVE (pengirim: Tobject; Shift: TshiftState;
X, Y: Integer);
Mulai
if (diklik) lalu
Mulai
Kasus Toolno dari
4:
Mulai
moveobj ((X-beginx), (y-beginy));
beginx: = x; Awal: = y;
Drawfull;
akhir;
5:
Mulai
movenode ((x-beginx), (y-beginy), getSelect.objid);
movelocal ((x-beginx), (y-beginy));
beginx: = x; Awal: = y;
Drawfull;
akhir;
akhir;
akhir;
akhir;
Prosedur tfrm_tree.fadestream1click (pengirim: tobject);
var
Selfile: String;
Curid: String;
Curobj: Tobj;
lstdate: tinifile32;
I, J: Integer;
Mulai
J: = olst.count;
Jika Savedialog1.Eksecute maka
Mulai
Selfile: = saveDialog1.filename;
lstdate: = tinifile32.create (selfile+'. dat');
lstdate.writeInteger ('judul', 'num', j);
untuk i: = 0 hingga j-1 lakukan
Mulai
curobj: = tobj (olst.items [i]^);
Curid: = Curobj.objid;
lstdate.writeString (curid, 'objid', curobj.objid);
lstdate.writeInteger (Curid, 'centerx', curobj.centerx);
lstdate.writeInteger (Curid, 'Centery', Curobj.Centery);
lstdate.writeInteger (curid, 'typeno', curobj.typeno);
lstdate.writebool (curid, 'dipilih', curobj.seleksi);
lstdate.writeString (curid, 'fnode', curobj.fnode);
lstdate.writebool (curid, 'menunjukkan', curobj.showed);
akhir;
akhir;
akhir;
Prosedur tfrm_tree.randomrandomselection1click (pengirim: tobject);
var
Selfile: String;
// curid: string;
lstdate: tinifile32;
I, J: Integer;
Mulai
Jika OpenDialog1.Eksecute maka
Mulai
Selfile: = OpenDialog1.FileName;
diklik: = false;
Paintbox1.canvas.fillrect (Rect (0,0, Paintbox1.width, Paintbox1.height));
Olst.clear;
Root: = true;
Selid: = '';
Searilid: = 0;
lstdate: = tinifile32.create (selfile);
j: = lstdate.readInteger ('judul', 'num', 0);
untuk i: = 1 ke j lakukan
Mulai
addobj (lstdate.readstring (inttostr (i), 'objid', ''), lstdate.readinteger (inttostr (i), 'centre', 0), lstdate.readinteger (inttostr (i), 'centery', 0) , lstdate.readInteger (inttostr (i), 'typeno', 0), lstdate.readbool (inttostr (i), 'dipilih', benar), lstdate.readstring (inttoStr (i), 'fnode', '), lstdate.readbool (inttostr (i), 'menunjukkan', true));
akhir;
Searilid: = j;
Root: = false;
Drawfull;
akhir;
akhir;
Prosedur TFRM_TREE.BUTTON7CLICK (pengirim: TOBJEKS);
Mulai
Toolno: = 5;
akhir;
Prosedur tfrm_tree.movenode (dx, dy: integer; id: string);
var
I, J: Integer;
Curobj:^tobj;
Mulai
J: = olst.count;
untuk i: = 0 hingga j-1 lakukan
Mulai
jika tobj (olst.items [i]^). fnode = id lalu
Mulai
Curobj: = getPobj (tobj (olst.items [i]^). Objid);
Curobj^.centerx: = Curobj^.Centerx+dx;
Curobj^.Centery: = Curobj^.Centery+dy;
movenode (dx, dy, curobj^.objid);
akhir;
akhir;
akhir;
Prosedur TFRM_TREE.Movelocal (DX, DY: Integer);
var
I, J: Integer;
// Curobj: Tobj;
Mulai
J: = olst.count;
untuk i: = 0 hingga j-1 lakukan
Mulai
Jika tobj (olst.items [i]^). Dipilih lalu
Mulai
tobj (olst.items [i]^). Centerx: = tobj (olst.items [i]^). Centerx+dx;
tobj (olst.items [i]^). Centery: = tobj (olst.items [i]^). Centery+dy;
Merusak;
akhir;
akhir;
akhir;
akhir.