/**
* 功能:实现许多图片编辑软件都支持的“填充颜色”功能。 * 给定一个屏幕(以二维数组表示,元素为颜色值)、一个点和一个新的颜色值,将新颜色填入这个店的周围区域,知道原来的颜色值全都改变。*/
- /**
- * 思路:假设要对一个像素(比如红色)调用paintFill,即对周围的像素逐一调用paintFill,
- * 向外扩张,一旦碰到非红色的像素就停止填充。
- *
- * 注意:碰到图像问题,要注意screen[y][x]中x和y的顺序。x表示水平轴(即自左向右),实际上对应于列数,而非行数。y的值等于行数。
- * screen
- * x
- * y
- * ncolor
- *
- */
- public static boolean paintFill(Color[][] screen,int x,int y,Color ncolor){
- if(screen[y][x]==ncolor)
- return false;
- return paintFill(screen, x, y, screen[y][x], ncolor);
- }
- public static boolean paintFill(Color[][] screen,int x,int y,Color ocolor,Color ncolor){
- if(x<0||x>=screen[0].length||y<0||y>=screen.length)
- return false;
- if(screen[y][x]==ocolor){
- screen[y][x]=ncolor;
- paintFill(screen, x-1, y, ocolor, ncolor);//左
- paintFill(screen, x+1, y, ocolor, ncolor);//右
- paintFill(screen, x, y-1, ocolor, ncolor);//上!!!
- paintFill(screen, x, y+1, ocolor, ncolor);//下!!!
- }
- return true;
- }
[java]
- enum Color{
- Black,White,Red,Yellow,Green
- }