最终效果:
//绘制一个8x8的红白交替的国际象棋棋盘。
#include"../实践/GUI/Simple_window.h"
#include"GUI/Graph.h"
int main()
try
{
using namespace Graph_lib;
//建立窗口。
Point tl(100, 100);
Simple_window win(tl, 600, 400, "My Window");
//-----------------------------------------------------------------------------
//建立能够保存已命命和未命名对象的向量。
Vector_ref<Graph_lib::Rectangle>vr;
//------------------------------------------------------------------------------
//向向量中添加棋盘元素,并将其展出。
for(int i=0;i<8;i++)
for (int j = 0; j < 8; j++)
{
vr.push_back(new Graph_lib::Rectangle(Point(i * 20, j * 20), 20, 20));
if ((i + j) % 2 == 0)vr[vr.size() - 1].set_fill_color(Color::white);
else vr[vr.size() - 1].set_fill_color(Color::red);
win.attach(vr[vr.size() - 1]);
}
//-------------------------------------------------------------------------------
//将控制权交给显示引擎,使GUI系统在屏幕上显示一个对象,并等待用户按下窗口中的“Next”,
//以便执行下面的程序。
win.wait_for_button();
//-------------------------------------------------------------------------------
}
catch (exception& e) {
cerr << "error: " << e.what() << '\n';
return 1;
}
catch (...) {
cerr << "Oops: unknown exception!\n";
return 2;
};
//-----------------------------------------------------------------------------------