Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package practicalrefactorings.floodfill;
- import java.awt.Color;
- import java.util.ArrayList;
- import static java.util.Arrays.asList;
- import java.util.Collection;
- import java.util.LinkedList;
- import java.util.Queue;
- public class Floodfill {
- private Position start;
- private Grid<Color> original;
- private Grid<Color> floodedGrid;
- private Color color;
- public Grid<Color> fillAt(Grid<Color> original, int startX, int startY, Color color) {
- start = new Position(startX, startY);
- this.original = original;
- this.floodedGrid = new ArrayBackedGrid<>(original.width(), original.height());
- this.color = color;
- getFloodedGrid();
- return floodedGrid;
- }
- private void getFloodedGrid() {
- if (start.x() >= 0 && start.x() < original.width() && start.y() >= 0 && start.y() < original.height()) {
- copyOriginalToFlooded();
- colorReplacement();
- } else {
- throw new IndexOutOfBoundsException("Got " + new Position(start.x(), start.y()) + " but grid is only " + original.width() + "x" + original.height());
- }
- }
- private void copyOriginalToFlooded() {
- for (int x = 0; x < original.width(); x++) {
- for (int y = 0; y < original.height(); y++) {
- floodedGrid.set(original.get(x, y), x, y);
- }
- }
- }
- private void colorReplacement() {
- Queue<Position> left = new LinkedList<>();
- left.add(new Position(start.x(), start.y()));
- Color replacingColor = original.get(start.x(), start.y());
- if (!replacingColor.equals(color)) {
- while (!left.isEmpty()) {
- Position at = left.poll();
- if (at.x() >= 0 && at.x() < floodedGrid.width() && at.y() >= 0 && at.y() < floodedGrid.height()) {
- Collection<Position> uncoloredNeighbors = getUncoloredPositions(replacingColor, at);
- left.addAll(uncoloredNeighbors);
- }
- }
- }
- }
- private Collection<Position> getUncoloredPositions(Color replacingColor, Position at) {
- floodedGrid.set(color, at.x(), at.y());
- Collection<Position> neighbors = asList(
- new Position(at.x() + 1, at.y()),
- new Position(at.x(), at.y() + 1),
- new Position(at.x() - 1, at.y()),
- new Position(at.x(), at.y() - 1)
- );
- Collection<Position> uncoloredNeighbors = new ArrayList<>();
- getPositionForColoring(replacingColor, neighbors, uncoloredNeighbors);
- return uncoloredNeighbors;
- }
- private void getPositionForColoring(Color replacingColor, Collection<Position> neighbors, Collection<Position> uncoloredNeighbors) {
- for (Position position : neighbors) {
- if (position.x() >= 0 && position.x() < floodedGrid.width() && position.y() >= 0 && position.y() < floodedGrid.height()) {
- Color colorAtPosition = floodedGrid.get(position.x(), position.y());
- if (colorAtPosition.equals(replacingColor)) {
- uncoloredNeighbors.add(position);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement