别的不多说了,贴一张图,exercise 7执行结果:
下面是代码(包含习题3到11):
#include "stdafx.h"
#include <iostream>
//#include "../../std_lib_facilities.h"
#include "../../GUI/Simple_window.h" // get access to our window library
//#include "../../GUI/Graph.h" // get access to our graphics library facilities
void ex03(Simple_window& win)
{
Closed_polyline letter_b;
letter_b.add(Point(100, 100));
letter_b.add(Point(150, 100));
letter_b.add(Point(170, 125));
letter_b.add(Point(170, 150));
letter_b.add(Point(150, 175));
letter_b.add(Point(100, 175));
letter_b.add(Point(150, 175));
letter_b.add(Point(175, 200));
letter_b.add(Point(175, 225));
letter_b.add(Point(150, 250));
letter_b.add(Point(100, 250));
letter_b.set_color(Color::blue);
letter_b.set_style(Line_style(Line_style::solid, 4));
win.attach(letter_b);
win.wait_for_button();
}
void ex04(Simple_window& win)
{
// create squares
Graph_lib::Rectangle rect1(Point(100, 100), 100, 100);
rect1.set_fill_color(Color::white);
win.attach(rect1);
Graph_lib::Rectangle rect2(Point(200, 100), 100, 100);
rect2.set_fill_color(Color::red);
win.attach(rect2);
Graph_lib::Rectangle rect3(Point(300, 100), 100, 100);
rect3.set_fill_color(Color::white);
win.attach(rect3);
Graph_lib::Rectangle rect4(Point(100, 200), 100, 100);
rect4.set_fill_color(Color::red);
win.attach(rect4);
Graph_lib::Rectangle rect5(Point(200, 200), 100, 100);
rect5.set_fill_color(Color::white);
win.attach(rect5);
Graph_lib::Rectangle rect6(Point(300, 200), 100, 100);
rect6.set_fill_color(Color::red);
win.attach(rect6);
Graph_lib::Rectangle rect7(Point(100, 300), 100, 100);
rect7.set_fill_color(Color::white);
win.attach(rect7);
Graph_lib::Rectangle rect8(Point(200, 300), 100, 100);
rect8.set_fill_color(Color::red);
win.attach(rect8);
Graph_lib::Rectangle rect9(Point(300, 300), 100, 100);
rect9.set_fill_color(Color::white);
win.attach(rect9);
win.wait_for_button();
}
void ex05(Simple_window& win) {
using Graph_lib::Polygon;
using Graph_lib::Rectangle;
int x = x_max() * 2 / 3;
int y = y_max() * 3 / 4;
Rectangle r{ Point{ 100,100 }, x, y };
r.set_fill_color(Color::red);
r.set_style(Line_style(Line_style::solid, 15));
win.attach(r);
win.wait_for_button();
}
void ex07(Simple_window& win)
{
Line_style house_style(Line_style::solid, 4);
Graph_lib::Rectangle house(Point(100, 370), 640, 330);
house.set_style(house_style);
house.set_fill_color(Color::dark_red);
win.attach(house);
Graph_lib::Rectangle door(Point(370, 500), 100, 200);
door.set_style(house_style);
door.set_fill_color(Color::dark_green);
win.attach(door);
Graph_lib::Rectangle win1(Point(210, 525), 50, 75);
win1.set_style(house_style);
win1.set_fill_color(Color::cyan);
win.attach(win1);
Graph_lib::Rectangle win2(Point(580, 525), 50, 75);
win2.set_style(house_style);
win2.set_fill_color(Color::cyan);
win.attach(win2);
Graph_lib::Polygon roof;
roof.add(Point(50, 370));
roof.add(Point(420, 270));
roof.add(Point(790, 370));
roof.set_style(house_style);
roof.set_fill_color(Color::dark_blue);
win.attach(roof);
Graph_lib::Polygon chimney;
chimney.add(Point(590, 250));
chimney.add(Point(620, 250));
chimney.add(Point(620, 324));
chimney.add(Point(590, 316));
chimney.set_style(house_style);
chimney.set_fill_color(Color::dark_magenta);
win.attach(chimney);
win.wait_for_button();
}
void ex08(Simple_window& win)
{
Line_style oly_style(Line_style(Line_style::solid, 40));
Circle c1(Point(300, 300), 220);
c1.set_style(oly_style);
c1.set_color(Color::blue);
win.attach(c1);
Circle c2(Point(812, 300), 220);
c2.set_style(oly_style);
win.attach(c2);
Circle c3(Point(1324, 300), 220);
c3.set_style(oly_style);
c3.set_color(Color::red);
win.attach(c3);
Circle c4(Point(556, 540), 220);
c4.set_style(oly_style);
c4.set_color(Color::yellow);
win.attach(c4);
Circle c5(Point(1068, 540), 220);
c5.set_style(oly_style);
c5.set_color(Color::green);
win.attach(c5);
win.wait_for_button();
}
void ex09(Simple_window& win)
{
win.resize(640, 480);
Image img(Point(50, 50), "image.jpg");
win.attach(img);
win.set_label("Breaking the sound barrier");
Text txt(Point(50, 280), "Breaking the sound barrier");
win.attach(txt);
win.wait_for_button();
}
// draw the file diagram from §12.8
void ex10(Simple_window& win)
{
Graph_lib::Rectangle fltk1(Point(400, 10), 120, 40);
fltk1.set_fill_color(Color::yellow);
win.attach(fltk1);
Graph_lib::Rectangle fltk2(Point(406, 16), 120, 40);
fltk2.set_fill_color(Color::yellow);
win.attach(fltk2);
Graph_lib::Rectangle fltk3(Point(412, 22), 120, 40);
fltk3.set_fill_color(Color::yellow);
win.attach(fltk3);
Text fltk_txt(Point(418, 42), "FLTK headers");
win.attach(fltk_txt);
Graph_lib::Rectangle win_rect(Point(389, 130), 166, 80);
win_rect.set_fill_color(Color::yellow);
win.attach(win_rect);
Text win_txt0(Point(389, 127), "Window.h:");
win_txt0.set_font(Font::helvetica_bold);
win.attach(win_txt0);
Text win_txt1(Point(395, 150), "// window interface:");
win.attach(win_txt1);
Text win_txt2(Point(395, 170), "class Window {...};");
win_txt2.set_font(Font::helvetica_bold);
win.attach(win_txt2);
Text win_txt3(Point(395, 190), "...");
win.attach(win_txt3);
Open_polyline win_fltk;
win_fltk.add(Point(472, 130));
win_fltk.add(Point(472, 62));
win_fltk.set_style(Line_style(Line_style::solid,2));
win_fltk.set_color(Color::black);
win.attach(win_fltk);
Graph_lib::Polygon win_fltk_arw;
win_fltk_arw.add(Point(472, 62));
win_fltk_arw.add(Point(475, 69));
win_fltk_arw.add(Point(469, 69));
win_fltk_arw.set_fill_color(Color::black);
win.attach(win_fltk_arw);
// add rest, would be much easier if not everything were passed by reference
// and would thus stop existing when leaving a scope
win.wait_for_button();
}
// draw a series of regular polygons one inside the other
void ex11(Simple_window& win)
{
Graph_lib::Polygon tri;
tri.add(Point(200, 20));
tri.add(Point(300, 20));
tri.add(Point(250, 106));
win.attach(tri);
Open_polyline square;
square.add(Point(200, 20));
square.add(Point(200, 120));
square.add(Point(300, 120));
square.add(Point(300, 20));
win.attach(square);
Open_polyline pentagon;
pentagon.add(Point(200, 20));
pentagon.add(Point(169, 115));
pentagon.add(Point(250, 179));
pentagon.add(Point(331, 115));
pentagon.add(Point(300, 20));
win.attach(pentagon);
Open_polyline hexagon;
hexagon.add(Point(200, 20));
hexagon.add(Point(150, 107));
hexagon.add(Point(200, 194));
hexagon.add(Point(300, 194));
hexagon.add(Point(350, 107));
hexagon.add(Point(300, 20));
win.attach(hexagon);
win.wait_for_button();
}
int main()
{
try {
Point p(100, 100);
Simple_window win(p, 1800, 800, "My window");
ex07(win);
}
catch (exception& e) {
cerr << "exception: " << e.what() << endl;
keep_window_open();
}
catch (...) {
cerr << "exception\n";
keep_window_open();
}
}