For changing the diagonals you need to convert your matrix to row iterator so that you can iterate over it, then zip this iterator with index for replacing the elements on each row as per the index, which is the diagonal element of the matrix. Below is the code with required comments.
import org.apache.spark.mllib.linalg.DenseMatrix
//creating initial matrix which needs to be changes
val arr = Array(0.0,2.0,1.0,2.0,2.0,0.0,2.0,4.0,1.0,2.0,0.0,3.0,2.0,4.0,3.0,0.0)
val mat = new DenseMatrix(4,4,arr)
//output
//0.0 2.0 1.0 2.0
//2.0 0.0 2.0 4.0
//1.0 2.0 0.0 3.0
//2.0 4.0 3.0 0.0
//make the iterator and change the element at the index of each row
val changedArr = mat.rowIter.zipWithIndex.flatMap(x => {
val ar = x._1.toArray
ar(x._2) = 1.0
ar
}).toArray
//create new matrix from it
val changedMat = new DenseMatrix(mat.numRows, mat.numCols, changedArr)
//output
//1.0 2.0 1.0 2.0
//2.0 1.0 2.0 4.0
//1.0 2.0 1.0 3.0
//2.0 4.0 3.0 1.0