画板的代码
prolog吧
全部回复
仅看楼主
level 1
somaniya 楼主
?- retractall(last_poit(_, _)), retractall(picture(_)), window( _, _, win_func(_), "Draw", 200, 100, 500, 400).win_func(init) :- menu( pop_up, _, _, menu_file(_), "&File"), menu( pop_up, _, _, menu_color(_), "&Color"), menu( pop_up, _, _, menu_pen(_), "&Pen"), menu( normal, _, _, menu_cut(_), "C&ut the line").win_func(mouse_click(X, Y)) :- last_poit(A, B), picture(Pic), add_point(Pic, [X, Y]), set(last_poit(X, Y)), set(picture(Pic)), line(A, B, X, Y).win_func(mouse_click(X, Y)) :- make_picture(Pic), set(last_poit(X, Y)), add_point(Pic, [X, Y]), set(picture(Pic)).win_func(paint) :- picture(Pic), paint2(Pic).make_picture(Pic) :- picture(Pic).make_picture([[-1, 1], [T, C]|_]) :- pen(T, C).paint2([[A, B]|Pic]) :- var(Pic), !, fail.paint2([[-1, 4]|Pic]) :- % end !, paint2(Pic).paint2([[-1, 0]|Pic]) :- % cut !, paint2(Pic).paint2([[-1, 1], [T, C]|Pic]) :- % pen !, pen(T, C), paint2(Pic).paint2([[A, B], [-1, D]|Pic]) :- !, paint2([[-1, D]|Pic]).paint2([[A, B], [C, D]|Pic]) :- !, line(A, B, C, D), paint2([[C, D]|Pic]). add_point(Pic, P) :- var(Pic), !, Pic=[P|_].add_point([Head|Pic], P) :- add_point(Pic, P).menu_file(init) :- menu( normal, _, _, menu_file_open(_), "&Open"), menu( normal, _, _, menu_file_save(_), "Save &As").menu_file_open(press) :- F is select_file(o, _, "Draw Files (*.dr)\0*.dr\0All Files (*.*)\0*.*\0"), H is open(F, "rb"), read_pic(H, Pic), close(H), set(picture(Pic)), update_window(_).read_pic(H, [[X, Y]|Pic]) :- X is get(H, l), Y is get(H, l), [X, Y]\=[-1, 4], % end of file !, read_pic(H, Pic).read_pic(H, Pic).menu_file_save(press) :- F is select_file(s, _, "Draw Files (*.dr)\0*.dr\0All Files (*.*)\0*.*\0"), H is open(F, "wb"), picture(Pic), save_pic(H, Pic), close(H).save_pic(H, []) :- put(-1, H, l), put(4, H, l).save_pic(H, [[X, Y]|Pic]) :- put(X, H, l), put(Y, H, l), save_pic(H, Pic).menu_color(init) :- menu( normal, _, _, menu_color_red(_), "&Red"), menu( normal, _, _, menu_color_black(_), "&Black"), menu( normal, _, _, menu_color_custom(_), "&Custom").menu_color_red(press) :- C is rgb(255, 0, 0), make_pen(_, C).menu_color_black(press) :- C is rgb(0, 0, 0), make_pen(_, C).menu_color_custom(press) :- C is select_color(_, 0xff), % 0xff is Red or rgb(0xff, 0, 0). 0xff=255 make_pen(_, C). % 0xff00 is Green or rgb(0, 0xff, 0), etc.menu_pen(init) :- menu( normal, _, _, menu_pen_theen(_), "&Thin"), menu( normal, _, _, menu_pen_thick(_), "Thic&k").menu_pen_theen(press) :- make_pen(1, _).menu_pen_thick(press) :- make_pen(6, _).make_pen(T, C) :- pen(T, C), picture(Pic), add_point(Pic, [-1, 1]), % pen flag add_point(Pic, [T, C]), set(picture(Pic)), !, retract(last_poit(_, _)).menu_cut(press) :- picture(Pic), add_point(Pic, [-1, 0]), % cut flag set(picture(Pic)), retract(last_poit(_, _)).
2008年10月17日 13点10分 1
1