title: Draw/erase button in java
body: <p>I am struggling to find a way to implement a way that whenever you click a draw button it will decide whether you are drawing or erasing.</p>
  
  <pre><code> private void drawButtonActionPerformed (java.awt.event.ActionEvent evt) {
  
          solveButton.setEnabled(true);
  
          slider.setEnabled(true);
  
          captureButton.setEnabled(true);
  
      } 
  </code></pre>
  
  <p>My code at the moment lets automatically decides whether to draw or erase based on whether there is an obstacle where you are clicking</p>
  
  <pre><code> private class MouseHandler implements MouseListener, MouseMotionListener {
  
          int cur_row, cur_col, cur_val;
  
  
  
          @Override
          public void mousePressed(MouseEvent evt) {
  
  
              int row = (evt.getY() - 10) / squareSize;
              int col = (evt.getX() - 10) / squareSize;
              if (row &gt;= 0 &amp;&amp; row &lt; rows &amp;&amp; col &gt;= 0 &amp;&amp; col &lt; columns) {
  
                  cur_row = row;
                  cur_col = col;
                  cur_val = grid[row][col];
  
                  if (cur_val == EMPTY) {
                      grid[row][col] = OBST;
                  }
                  if (cur_val == OBST) {
                      grid[row][col] = EMPTY;
                  }
                  repaint();
              }
          }
  
          @Override
          public void mouseDragged(MouseEvent evt) {
  
              int row = (evt.getY() - 10) / squareSize;
              int col = (evt.getX() - 10) / squareSize;
              if (row &gt;= 0 &amp;&amp; row &lt; rows &amp;&amp; col &gt;= 0 &amp;&amp; col &lt; columns) {
  
                  if ((row * columns + col != cur_row * columns + cur_col) &amp;&amp; (cur_val == STARTNODE || cur_val == ENDNODE)) {
                      int new_val = grid[row][col];
                      if (new_val == EMPTY) {
                          grid[row][col] = cur_val;
                          if (cur_val == STARTNODE) {
                              startnodeStart.row = row;
                              startnodeStart.col = col;
                          } else {
                              endnodePos.row = row;
                              endnodePos.col = col;
                          }
                          grid[cur_row][cur_col] = new_val;
                          cur_row = row;
                          cur_col = col;
                          if (cur_val == STARTNODE) {
                              startnodeStart.row = cur_row;
                              startnodeStart.col = cur_col;
                          } else {
                              endnodePos.row = cur_row;
                              endnodePos.col = cur_col;
                          }
                          cur_val = grid[row][col];
                      }
                  } else if (grid[row][col] != STARTNODE &amp;&amp; grid[row][col] != ENDNODE) {
                      grid[row][col] = OBST;
                  }
  
              }
  
              repaint();
  
          }
  
          @Override
          public void mouseReleased(MouseEvent evt) {
          }
  
          @Override
          public void mouseEntered(MouseEvent evt) {
          }
  
          @Override
          public void mouseExited(MouseEvent evt) {
          }
  
          @Override
          public void mouseMoved(MouseEvent evt) {
          }
  
          @Override
          public void mouseClicked(MouseEvent evt) {
          }
  
      }
  </code></pre>
  
  <p>Pls help me thanks.</p>