Page 426 - AP Computer Science A, 7th edition
P. 426

∗ connected region of pixels[row][col] the newColor.
∗ – Does nothing if oldColor is the same as newColor.
∗ @param row the given row
∗ @param col the given column
∗ @param newColor the new color for painting
∗ @param oldColor the current color of
pixels[row][col] ∗/
public void paintRegion(int row, int col, String newColor,
String oldColor)
Solution
(a) public ColorGrid(String s, int numRows, int numCols)
{
rows = numRows;
cols = numCols;
pixels = new String[numRows][numCols]; int stringIndex = 0;
for (int r = 0; r < numRows; r++)
}
for (int c = 0; c < numCols; c++) {
pixels[r][c] = s.substring(stringIndex, stringIndex + 1);
stringIndex++; } }
(b) public void paintRegion(int row, int col, String newColor,
String oldColor)
{
if (row >= 0 && row < rows && col >= 0 && col < cols)
if (!pixels[row][col].equals(newColor) && pixels[row][col].equals(oldColor))
{
pixels[row][col] = newColor; paintRegion(row + 1, col, newColor,








































































   424   425   426   427   428