Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package practicalrefactorings.floodfill;
  7.  
  8. import java.awt.Color;
  9. import java.util.ArrayList;
  10. import static java.util.Arrays.asList;
  11. import java.util.Collection;
  12. import java.util.LinkedList;
  13. import java.util.Queue;
  14.  
  15. public class Floodfill {
  16. private Position start;
  17. private Grid<Color> original;
  18. private Grid<Color> floodedGrid;
  19. private Color color;
  20.  
  21. public Grid<Color> fillAt(Grid<Color> original, int startX, int startY, Color color) {
  22. start = new Position(startX, startY);
  23. this.original = original;
  24. this.floodedGrid = new ArrayBackedGrid<>(original.width(), original.height());
  25. this.color = color;
  26. getFloodedGrid();
  27. return floodedGrid;
  28. }
  29.  
  30. private void getFloodedGrid() {
  31. if (start.x() >= 0 && start.x() < original.width() && start.y() >= 0 && start.y() < original.height()) {
  32. copyOriginalToFlooded();
  33. colorReplacement();
  34. } else {
  35. throw new IndexOutOfBoundsException("Got " + new Position(start.x(), start.y()) + " but grid is only " + original.width() + "x" + original.height());
  36. }
  37. }
  38.  
  39. private void copyOriginalToFlooded() {
  40. for (int x = 0; x < original.width(); x++) {
  41. for (int y = 0; y < original.height(); y++) {
  42. floodedGrid.set(original.get(x, y), x, y);
  43. }
  44. }
  45. }
  46.  
  47. private void colorReplacement() {
  48. Queue<Position> left = new LinkedList<>();
  49. left.add(new Position(start.x(), start.y()));
  50. Color replacingColor = original.get(start.x(), start.y());
  51. if (!replacingColor.equals(color)) {
  52. while (!left.isEmpty()) {
  53. Position at = left.poll();
  54. if (at.x() >= 0 && at.x() < floodedGrid.width() && at.y() >= 0 && at.y() < floodedGrid.height()) {
  55. Collection<Position> uncoloredNeighbors = getUncoloredPositions(replacingColor, at);
  56. left.addAll(uncoloredNeighbors);
  57. }
  58. }
  59. }
  60. }
  61.  
  62. private Collection<Position> getUncoloredPositions(Color replacingColor, Position at) {
  63. floodedGrid.set(color, at.x(), at.y());
  64. Collection<Position> neighbors = asList(
  65. new Position(at.x() + 1, at.y()),
  66. new Position(at.x(), at.y() + 1),
  67. new Position(at.x() - 1, at.y()),
  68. new Position(at.x(), at.y() - 1)
  69. );
  70. Collection<Position> uncoloredNeighbors = new ArrayList<>();
  71. getPositionForColoring(replacingColor, neighbors, uncoloredNeighbors);
  72. return uncoloredNeighbors;
  73. }
  74.  
  75. private void getPositionForColoring(Color replacingColor, Collection<Position> neighbors, Collection<Position> uncoloredNeighbors) {
  76. for (Position position : neighbors) {
  77. if (position.x() >= 0 && position.x() < floodedGrid.width() && position.y() >= 0 && position.y() < floodedGrid.height()) {
  78. Color colorAtPosition = floodedGrid.get(position.x(), position.y());
  79. if (colorAtPosition.equals(replacingColor)) {
  80. uncoloredNeighbors.add(position);
  81. }
  82. }
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement