Skip to content

Instantly share code, notes, and snippets.

@WOLOHAHA
Created August 11, 2014 11:40
Show Gist options
  • Save WOLOHAHA/161b7932955af74583ab to your computer and use it in GitHub Desktop.
Save WOLOHAHA/161b7932955af74583ab to your computer and use it in GitHub Desktop.
Implement the "paint fill" function that one might see on many image editing programs. That is, given a screen( represented by a two-dimensional array of colors), a point, and a new color, fill in the surrounding area until the color changes from the original color.
package POJ;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Main {
/**
*
* 9.7 Implement the "paint fill" function that one might see on many image editing programs. That is,
* given a screen( represented by a two-dimensional array of colors), a point, and a new color, fill in
* the surrounding area until the color changes from the original color.
*
*
*/
public static void main(String[] args) {
Main so = new Main();
}
public boolean paintFill(Color[][] screen, int y, int x, Color ncolor){
if(screen[y][x]==ncolor)
return false;
return paintFill(screen,y,x,screen[y][x],ncolor);
}
private boolean paintFill(Color[][] screen, int y, int x, Color ocolor, Color ncolor) {
// TODO Auto-generated method stub
if(y<0||x<0||y>=screen.length||x>=screen[0].length)
return false;
if(screen[y][x]==ocolor){
screen[y][x]=ncolor;
paintFill(screen, y-1, x, ocolor, ncolor);
paintFill(screen, y+1, x, ocolor, ncolor);
paintFill(screen, y, x-1, ocolor, ncolor);
paintFill(screen, y, x+1, ocolor, ncolor);
}
return true;
}
}
enum Color{
Red, Pink, Yellow, Green, White
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment