Study Questions: study the source code and answer the following questions: Quest
ID: 3675997 • Letter: S
Question
Study Questions: study the source code and answer the following questions: Question 1. Which of the 3 different ways is the timer used in this program Question 2. What is the period for the timer used? Question 3. Which object and function is called with timer repeated periodic notification? Question 4. What event causes the text to be moved? documentation for the Timer widget http://docs.wxwidgets.org/stable/wx_wxtimer.html#wxtimer -------------------------------------------------------------------------------------------------------------------------------- // Render timer - use of a timer // http://wiki.wxwidgets.org/Making_a_render_loop #include #include #include // prototypes class BasicDrawPane; class MyFrame; // class definitions class RenderTimer : public wxTimer { BasicDrawPane* pane; public: RenderTimer(BasicDrawPane* pane); void Notify(); void start(); }; class BasicDrawPane : public wxPanel { public: BasicDrawPane(wxFrame* parent); void paintEvent(wxPaintEvent& evt); void render( wxDC& dc ); DECLARE_EVENT_TABLE() }; class MyApp: public wxApp { bool OnInit(); MyFrame* frame; public: }; RenderTimer::RenderTimer(BasicDrawPane* pane) : wxTimer() { RenderTimer::pane = pane; } void RenderTimer::Notify() { pane->Refresh(); } void RenderTimer::start() { wxTimer::Start(10); } IMPLEMENT_APP(MyApp) class MyFrame : public wxFrame { RenderTimer* timer; BasicDrawPane* drawPane; public: MyFrame() : wxFrame((wxFrame *)NULL, -1, wxT("Hello wxDC"), wxPoint(50,50), wxSize(500,300)) { drawPane = new BasicDrawPane( this ); timer = new RenderTimer(drawPane); Show(); timer->start(); } ~MyFrame() { delete timer; } void onClose(wxCloseEvent& evt) { timer->Stop(); evt.Skip(); } DECLARE_EVENT_TABLE() }; // event table for MyFrame BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_CLOSE(MyFrame::onClose) END_EVENT_TABLE() bool MyApp::OnInit() { frame = new MyFrame(); frame->Show(); return true; } // event table for BasicDrawPane BEGIN_EVENT_TABLE(BasicDrawPane, wxPanel) EVT_PAINT(BasicDrawPane::paintEvent) END_EVENT_TABLE() BasicDrawPane::BasicDrawPane(wxFrame* parent) : wxPanel(parent) { } void BasicDrawPane::paintEvent(wxPaintEvent& evt) { wxPaintDC dc(this); render(dc); } void BasicDrawPane::render( wxDC& dc ) { static int y = 0; static int y_speed = 2; y += y_speed; if(y200) y_speed = -2; dc.SetBackground( *wxWHITE_BRUSH ); dc.Clear(); dc.DrawText(wxT("Testing"), 40, y); }
Explanation / Answer
By using the program given, I answered the question below:
Question 1. Which of the 3 different ways is the timer used in this program.
In this program, there are three ways that timer used:
The wxTimer class notifies the periodically then derive a class from wxTimer and override it by the Notify function. The Otherone is by wxTimerEvent to pass a wxEvtHandler pointer to the timer and use to connect the timer to an event handler function. And the last one is by by calling Start for Start the timer, and set a time interval in milliseconds.By Calling Stop to stops the timer.
Question 2. What is the period for the timer used?
10 milliseconds is the period for the timer used in this program.
Question 3. Which object and function is called with timer repeated periodic notification?
The sequence for timer notifications is as follows:
• The function BasicDrawPane::paintEvent() create a 'Device Context' for 'this' window as object and call the BasicDrawPane::render()
• The function BasicDrawPane::render() create y as object. It also clears the Device Context and “Testing” on the wxPanel Device Context.
Question 4. What event causes the text to be moved?
Whenever the window is called for Refresh() , it start the window to be repainted by the wxPaintEvent using ‘this’ . The wxPaintEvent& evt is calls BasicDrawPane::paintEvent(wxPaintEvent& evt). That function creates a wxPaintDC from 'this' wxPanel.