Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.image.BufferedImage;
  3. import java.util.PriorityQueue;
  4.  
  5. public class FloodFill {
  6.     Canvas cnvs = Canvas.getInstance();
  7.     public void AlgFloodFill(int x, int y, int CenterColour, int newColor, BufferedImage b) {
  8.        
  9.         if (CenterColour == newColor) {
  10.             return;
  11.         }
  12.        
  13.         if (b.getRGB(x, y) != CenterColour) {
  14.             return;
  15.         }
  16.        
  17.         PriorityQueue q = new PriorityQueue();
  18.        
  19.         if ((x >= 0) && (y >= 0) && ( x < cnvs.getlength()) && (y < cnvs.getheight())) {
  20.             b.setRGB(x, y, newColor);
  21.         }
  22.        
  23.         System.out.println("DCCCMMM NU MERGEEE");
  24.         q.add(x);
  25.         q.add(y);
  26.        
  27.         while (!q.isEmpty()) {
  28.             int nx = (int) q.poll();
  29.             int ny = (int) q.poll();
  30.             if ((nx -1 >= 0) && (ny >= 0) && ( nx - 1 < cnvs.getlength()) && (ny < cnvs.getheight()))  {
  31.                 if  (b.getRGB(nx - 1, ny) == CenterColour) {
  32.                     b.setRGB(nx - 1, ny, newColor);
  33.                     q.add(nx - 1);
  34.                     q.add(ny);
  35.                 }
  36.             }
  37.            
  38.             if ((nx + 1 >= 0) && (ny >= 0) && ( nx + 1 < cnvs.getlength()) && (ny < cnvs.getheight())){
  39.                 if (b.getRGB(nx + 1, ny) == CenterColour)  {
  40.                     b.setRGB(nx + 1, ny, newColor);
  41.                     q.add(nx + 1);
  42.                     q.add(ny);
  43.                 }
  44.             }
  45.            
  46.             if ((nx >= 0) && (ny - 1 >= 0) && ( nx < cnvs.getlength()) && (ny - 1 < cnvs.getheight())) {
  47.                 if  (b.getRGB(nx, ny - 1) == CenterColour){
  48.                     b.setRGB(nx, ny - 1, newColor);
  49.                     q.add(nx);
  50.                     q.add(ny - 1);
  51.                 }
  52.             }
  53.            
  54.             if ((nx >= 0) && (ny + 1 >= 0) && ( nx < cnvs.getlength()) && (ny + 1 < cnvs.getheight())){
  55.                 if (b.getRGB(nx, ny + 1) == CenterColour)  {
  56.                     b.setRGB(nx, ny + 1, newColor);
  57.                     q.add(nx);
  58.                     q.add(ny + 1);
  59.                 }
  60.             }
  61.         }
  62.        
  63.     }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement