Question: I need help fixing my code to what the instructions ask for. Window Resizing In C++ Make a window resize event in the main window

I need help fixing my code to what the instructions ask for. 

Window Resizing In C++

  1. Make a window resize event in the main window header file. This will look like our previous event handlers, but will take in a wxSizeEvent& as its parameter.
  2. In the constructor for the main window, call bind using something similar to this this->Bind(wxEVT_SIZE, &MainWindow::OnSizeChange, this);
  3. Next it is time to implement the size change method. Inside of the method, save the size of the window as a wxSize and pass it along to a SetSize method that needs to be created for the drawing panel object.
  4. In order to stop other events from being processed when resizing happens, add event.Skip() as the final line in the size change method.

Drawing Panel Set Size

  1. Make a method in your drawing panel header file for setting the size. It should take in a wxSize& as a parameter.
  2. Inside of this method implementation, two commands need to be run. First call SetSize on the base wxPanel class. Then call Refresh(); Calling refresh will cause the OnPaint event to be called again.

 

 


wxBEGIN_EVENT_TABLE(DrawingPanel, wxPanel)
EVT_PAINT(DrawingPanel::OnPaint)
EVT_LEFT_UP(DrawingPanel::OnMouseUp)
wxEND_EVENT_TABLE()

DrawingPanel::DrawingPanel(wxFrame* parent, std::vector>& gameBoard) : wxPanel(parent, wxID_ANY, wxPoint(0, 0), wxSize(200, 200)), _gameBoard(gameBoard)
{
this->SetBackgroundStyle(wxBG_STYLE_PAINT);

gridSize = 30;
CalculateCellSize();


}
DrawingPanel::~DrawingPanel()
{

}

void DrawingPanel::CalculateCellSize()
{
wxSize panelSize = GetParent()->GetClientSize();
cellWidth = panelSize.GetWidth() / gridSize;
cellHeight = panelSize.GetHeight() / gridSize;
}

void DrawingPanel::OnPaint(wxPaintEvent& event)
{
wxAutoBufferedPaintDC dc(this);
dc.Clear();

wxGraphicsContext* context = wxGraphicsContext::Create(dc);
if (!context)
{
 return;
}

context->SetPen(*wxBLACK);
context->SetBrush(*wxWHITE);


for (int row = 0; row < gridSize; row++)
{
 for (int col = 0; col < gridSize; col++)
 {
  int x = col * cellWidth;
  int y = row * cellHeight;
  // Checks the corresponding bool in the game board
  if (_gameBoard[row][col])
  {
   context->SetBrush(*wxLIGHT_GREY); // Living cell color
  }
  else
  {
   context->SetBrush(*wxWHITE); // Dead cell color
  }

  context->DrawRectangle(x, y, cellWidth, cellHeight);
 }
}

}
void DrawingPanel::SetGridSize(int size)
{
gridSize = size;
CalculateCellSize(); // Recalculates cell size based on the new grid size
}

void DrawingPanel::OnMouseUp(wxMouseEvent& event)
{
int mouseX = event.GetX();
int mouseY = event.GetY();

int row = mouseY / cellHeight;
int col = mouseX / cellWidth;

if (row >= 0 && row < gridSize && col >= 0 && col < gridSize)
{
 // Toggles the state of the clicked cell
 _gameBoard[row][col] = !_gameBoard[row][col];
 Refresh();
}
}

 

 


wxBEGIN_EVENT_TABLE(MainWindow, wxFrame)
EVT_SIZE(MainWindow::OnSizeChanged)
EVT_TOOL(TOOLBAR_PLAY_ICON, MainWindow::OnPlay)
EVT_TOOL(TOOLBAR_PAUSE_ICON, MainWindow::OnPause)
EVT_TOOL(TOOLBAR_NEXT_ICON, MainWindow::OnNext)
EVT_TOOL(TOOLBAR_TRASH_ICON, MainWindow::OnTrash)

EVT_TIMER(wxID_ANY, MainWindow::OnTimer)
wxEND_EVENT_TABLE();

MainWindow::MainWindow() :wxFrame(nullptr, wxID_ANY, "Game of Life", wxPoint(0, 0), wxSize(200, 200))
{
_drawingPanel = new DrawingPanel(this, _gameBoard);


_sizer = new wxBoxSizer(wxVERTICAL);

_toolbar = CreateToolBar();

// Adds Play button to the toolbar
wxBitmap playIcon(play_xpm);
_toolbar->AddTool(TOOLBAR_PLAY_ICON, "Play", playIcon);

// Adds Pause button to the toolbar
wxBitmap pauseIcon(pause_xpm);
_toolbar->AddTool(TOOLBAR_PAUSE_ICON, "Pause", pauseIcon);

// Adds Next button to the toolbar
wxBitmap nextIcon(next_xpm);
_toolbar->AddTool(TOOLBAR_NEXT_ICON, "Next", nextIcon);

// Adds Trash button to the toolbar
wxBitmap trashIcon(trash_xpm);
_toolbar->AddTool(TOOLBAR_PLAY_ICON, "Trash", trashIcon);

 

gameTimer = new wxTimer(this);
timerInterval = 50;

 

_toolbar->Realize();


_sizer->Add(_drawingPanel, 1, wxEXPAND | wxALL);

// Initializes the status bar
statusBar = CreateStatusBar();

SetSizer(_sizer);
InitializeGameBoard();

this->Layout();


}
MainWindow::~MainWindow()
{
gameTimer->Stop(); // Stop the timer before destroying the frame
delete gameTimer;
}
void MainWindow::OnSizeChanged(wxSizeEvent& event)
{
_drawingPanel->SetSize(event.GetSize());
_drawingPanel->Refresh();
}

void MainWindow::InitializeGameBoard()
{
_gameBoard.resize(gridSize); // Resizes the game board vector to the grid size

for (int i = 0; i < gridSize; i++)
{
 _gameBoard[i].resize(gridSize); // Resizes each sub-vector to the grid size
}

_drawingPanel->SetGridSize(gridSize); // Passes the grid size to the drawing panel
}

void MainWindow::UpdateStatusBar()
{
wxString statusText = wxString::Format("Generation: %d   Living Cells: %d", generationCount, livingCellsCount);
statusBar->SetStatusText(statusText, 0);
}

// Event handler for the Play button
void MainWindow::OnPlay(wxCommandEvent& event)
{
gameTimer->Start(timerInterval);
}

// Event handler for the Pause button
void MainWindow::OnPause(wxCommandEvent& event)
{
gameTimer->Stop();
}

// Event handler for the Next button
void MainWindow::OnNext(wxCommandEvent& event)
{
CalculateNextGeneration();
}

// Event handler for the Trash button
void MainWindow::OnTrash(wxCommandEvent& event)
{
for (int row = 0; row < gridSize; ++row)
{
 for (int col = 0; col < gridSize; ++col)
 {
  _gameBoard[row][col] = false;
 }
}

generationCount = 0;
livingCellsCount = 0;

UpdateStatusBar();

_drawingPanel->Refresh();
}


void MainWindow::OnTimer(wxTimerEvent& event)
{
CalculateNextGeneration();
}

int MainWindow::GetNeighborCount(int row, int col)
{
int count = 0;

for (int r = row - 1; r <= row + 1; r++)
{
 for (int c = col - 1; c <= col + 1; c++)
 {

  // Makes sure index is valid
  if (r >= 0 && r < gridSize && c >= 0 && c < gridSize)
  {

   
   if (!(r == row && c == col))
   {

    if (_gameBoard[r][c])
    {
     count++;
    }

   }

  }

 }
}

return count;
}

void MainWindow::CalculateNextGeneration()
{
// Sandbox with the same data type as the game board
std::vector> sandbox(gridSize, std::vector(gridSize, false));

// Iterates through the game board to calculate the next generation
int newLivingCellsCount = 0;

for (int row = 0; row < gridSize; ++row)
{
 for (int col = 0; col < gridSize; ++col)
 {
  // Counts the number of living neighbors for the current cell
  int neighborCount = GetNeighborCount(row, col);

 
  if (_gameBoard[row][col])
  {
   if (neighborCount < 2 || neighborCount > 3)
   {
    // Cell dies in the next generation
    sandbox[row][col] = false;
   }
   else
   {
    // Cell survives to the next generation
    sandbox[row][col] = true;
    ++newLivingCellsCount;
   }
  }
  else
  {
   if (neighborCount == 3)
   {
    // Dead cell becomes alive in the next generation
    sandbox[row][col] = true;
    ++newLivingCellsCount;
   }
  }
 }
}

// Updates the game board with the new generation using swap
_gameBoard.swap(sandbox);

// Updates counts and status bar
++generationCount;
livingCellsCount = newLivingCellsCount;
UpdateStatusBar();


_drawingPanel->Refresh();
}

 

Step by Step Solution

3.39 Rating (168 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Based on the instructions provided heres the modified code with the necessary changes ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Algorithms Questions!