SHARE
TWEET

Untitled

a guest Jun 18th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private class PixelBoard extends JPanel implements MouseListener, MouseMotionListener {
  2.     private boolean leftMouse = false;
  3.     private HiddenOptions hiddenOptions;
  4.      
  5. public Bucket(int startPixel, Color groupColor) {
  6.         this.groupColor = groupColor;
  7.         if (groupColor != getColor) {
  8.             bucket = add(startPixel, getColor, bucket);
  9.             getPixels.add(startPixel);
  10.         }
  11.         new Thread(this).start();
  12.     }
  13.  
  14.     @Override
  15.     public void run() {
  16.         while (!getPixels.isEmpty()) {
  17.             getPixels = move(getPixels, groupColor, getColor, bucket);
  18.         }
  19.     }
  20.  
  21.     public ArrayList<Integer> bucket() {
  22.         return bucket;
  23.     }
  24.  
  25.     private ArrayList<Integer> add(int p, Color getColor, ArrayList<Integer> bucket) {
  26.         pixel[p].setColor(getColor);
  27.         pixel[p].setToolTipText(pixel(p));
  28.         container.repaint();
  29.         bucket.add(p);
  30.         return bucket;
  31.     }
  32.  
  33.     private ArrayList<Integer> move(ArrayList<Integer> getPixels, Color groupColor, Color getColor, ArrayList<Integer> bucket) {
  34.         ArrayList<Integer> pix = new ArrayList<Integer>();
  35.         for (int p : getPixels) {
  36.             for (int m : mov(p)) {
  37.                 if (!bucket.contains(m)) {
  38.                     if (pixel[m].getColor().equals(groupColor)) {
  39.                         bucket = add(m, getColor, bucket);
  40.                         pix.add(m);
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.         return pix;
  46.     }
  47.  
  48.             private boolean[] pixelLocations(int index) {
  49.     boolean topLeftCorner = index == 0, topRightCorner = index == pixels - 1;
  50.     boolean bottomLeftCorner = index == lastPixel - pixels, bottomRightCorner = index == lastPixel - 1;
  51.     boolean leftEdge = index % pixels == 0, rightEdge = (index + 1) % pixels == 0;
  52.     boolean topEdge = index > 0 && index < pixels - 1, bottomEdge = index > lastPixel - pixels && index < lastPixel - 1;
  53.     return new boolean[] {topLeftCorner, topRightCorner, bottomLeftCorner, bottomRightCorner, leftEdge, rightEdge, topEdge, bottomEdge};
  54. }
  55.  
  56.  
  57.     private int[] move(int p) {
  58.         int[] move = {p + 1, p - 1, p + pixels, p - pixels};
  59.         boolean[] location = pixelLocations(p);
  60.         if (location[0])
  61.             move = move(move, new int[] {0, 2});
  62.         else if (location[1])
  63.             move = move(move, new int[] {1, 2});
  64.         else if (location[2])
  65.             move = move(move, new int[] {0, 3});
  66.         else if (location[3])
  67.             move = move(move, new int[] {1, 3});
  68.         else if (location[4])
  69.             move = move(move, new int[] {0, 2, 3});
  70.         else if (location[5])
  71.             move = move(move, new int[] {1, 2, 3});
  72.         else if (location[6])
  73.             move = move(move, new int[] {0, 1, 2});
  74.         else if (location[7])
  75.             move = move(move, new int[] {0, 1, 3});
  76.         return move;
  77.     }
  78.  
  79.     private int[] move(int[] move, int[] newMove) {
  80.         int[] m = new int[newMove.length];
  81.         for (int i = 0; i < m.length; i++)
  82.             m[i] = move[newMove[i]];
  83.         return m;
  84.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top