Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C++, Using Visual Studios. Create Toolbar Before the game of life can be animated, there needs to be a way of controlling the game.

In C++, Using Visual Studios.

Create Toolbar

  1. Before the game of life can be animated, there needs to be a way of controlling the game. This will be accomplished by adding a tool bar with some control buttons.
  2. Add a toolbar variable to the main window header file of type wxToolBar*.
  3. Similar to the status bar, the toolbar can be initialized in the constructor by calling the CreateToolBar() method.

Create Icons

  1. The toolbar is going to need some icons. This checkpoint has a few included for use. Open the Icons.zip file and place the .xpm files in the project directory with the .h and .cpp project files.
  2. The first button to be created is the play button. This will start the game of life going through its generations automatically.
    1. At the top of the main window cpp file, include play.xpm as if it was a custom .h file.
    2. If you open play.xpm in a text editor, you will see that it is a C style array with the name play_xpm. This variable name will be used in the next step.
    3. This file can be used to create bitmap calling wxBitmap playIcon(play_xpm).
    4. The icon can now be added to the toolbar by calling AddTool on the toolbar variable.
    5. AddTool takes in three arguments. An ID, a name, and an image. The name can be any string like "Play" or "Go". The image is the wxBitmap that we just created. The ID is important because it is going to be used in the event table. The ID needs to be a unique number, so don't use anything low like 0 or 1. Something over 10,000 would be a good idea.
    6. Create new event handler method in the main window header file that takes in a wxCommandEvent& variable as a parameter. This event handler will be called when the play button is clicked.
    7. For now, implement an empty method for the play button.
    8. Finally, add an entry to the event table for the event EVT_MENU. That entry will take in the unique numeric ID that was given to the button as well as the method name. Don't forget the class name and the scope resolution operator.
  3. Repeat step 2 for the pause, next, and trash buttons. The trash icon will be used for clearing the grid.
  4. Once the tools are all added to the toolbar object, the Realize method of the toolbar object needs to be called. This will render the icons onto the screen.
    1. This step may cause the window resize event to fire before the drawing panel has been created. In that event, the program will crash. To get around this, go to the main window resize method and find the line where the drawing panel's SetSize method is being called. Before running that line, add a check to make sure that the drawing panel is not nullptr. That should fix the crash.

I have attached the code I already have from previous steps also.

image text in transcribedimage text in transcribed
\fMainWindow.cpp + X MainWindow.h Drawing Panel.cpp App.h App.cpp Drawing Panel.h GameOfLife (Global Scope) #include "MainWindow. h" #include "DrawingPanel. h" WxBEGIN_EVENT_TABLE(MainWindow , wxFrame) EVT_SIZE (MainWindow: : OnSizeChange) WXEND_EVENT_TABLE(); 8 MainWindow: : MainWindow () 9 : wxFrame (nullptr, wxID_ANY, wxT("Game of Life"), wxPoint(0, 0), wxSize(200, 200)) // First Steps 10 boxSizer = new wxBoxSizer(wxVERTICAL); // Window Resizing 12 gameBoard . resize(gridSize, std: : vector(gridSize, false)); /ew 13 drawPanel = new DrawingPanel(this, gameBoard); // Click Handler 14 15 16 boxSizer->Add (drawPanel, 1, wxEXPAND | wxALL); // Window Resizing 17 this->SetSizer(boxSizer); // Window Resizing 18 19 this->Bind (wxEVT_SIZE, &MainWindow: : OnSizeChange, this); // Window Resizing 20 22 MainWindow: : MainWindow (const wxString& title) 23 : wxFrame (NULL, wxID_ANY, title) 24 25 statusBar = CreateStatusBar(); 26 27 generation = 0; 28 LivingCells = 0; 29 30 SetStatusText() ; 31 32 this->Layout() ; 35 avoid MainWindow: : SetStatusText() 36 37 wxString statusText; 38 statusText . Printf("Generation: *d Living Cells: *d", generation, livingCells) ; 39 statusBar->SetStatusText(statusText); 40 41 42 avoid MainWindow: : OnSizeChange (wxSizeEvent& event) // Window Resizing 43 44 wxSize windowSize = event . GetSize(); // Window Resizing 45 drawPanel->SetSize(windowSize); // Window Resizing event . Skip() ; MainWindow : : ~MainWindow()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

Students also viewed these Programming questions