我应该画一个带灯泡的窗口,并有一个按钮可以打开(白色)和关闭(黑色)。这是完整的目标:编写一个程序来显示一个窗口,其中一个圆圈代表一个灯泡,一个标有" ON / OFF"的按钮。灯泡从黑色开始,即关闭。当点击按钮时,灯泡打开,即白色。再次单击该按钮时,指示灯熄灭,依此类推。 但出于某种原因,圆圈没有出现在窗口中这是我的代码:
#include "std_lib_facilities_5.h"
#include "Simple_window.h"
#include "Graph.h"
#include "Window.h"
#include "Point.h"
struct Lines_window : Graph_lib::Window {
Lines_window(Point xy, int w, int h, const string& title);
Circle light_bulb{Point{400,400},150};
private:
Button on;
bool button_pushed;
static void cb_on1(Address, Address); // callback for next_button
void on1(); // action to be done when next_button is pressed
};
Lines_window::Lines_window(Point xy, int w, int h, const string& title)
:Window{xy,w,h,title},
on{Point{x_max()-100,0}, 70, 20, "ON/OFF",cb_on1},
button_pushed{false}
{
attach(on);
}
void Lines_window::cb_on1(Address, Address pw)
// call Simple_window::next() for the window located at pw
{
reference_to<Lines_window>(pw).on1();
}
void Lines_window::on1(){
if (button_pushed)
{
light_bulb.set_fill_color(Color::white);
button_pushed = false;
redraw();
}
else
{
light_bulb.set_fill_color(Color::black);
button_pushed = true;
redraw();
}
redraw();
}
int main ()
{
try{
Lines_window win {Point{100,100},800,800,"Light Bulb"};
return gui_main();
}
catch (exception& e){ //catches range errors and run_time_errors
cerr << "error: "<< e.what() <<'\n';
return 1;
}
catch (...){ //catch any exception
cerr <<"Exception: something went wrong \n";
return 2;
}
}