1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
| public class FloodFill {
public static final int SLOW = 10; // utilisé pour ralentir le remplissage, pour le voir progresser
public static final Color FILL_COLOR = Color.RED;
public static void main(String[] args) {
FloodFill test = new FloodFill();
AbsractShape testShape1 = new ShapeTest1();
AbsractShape testShape2 = new ShapeTest2();
AbsractShape testShape3 = new ShapeTest3();
AbsractShape testShape4 = new ShapeTest4();
AbsractShape[] shapes = { testShape1, testShape2, testShape3, testShape4 };
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel panel = new JPanel() {
protected void paintComponent(java.awt.Graphics g) {
super.paintComponent(g);
testShape1.draw(g.create(10,10, testShape1.getWidth(), testShape1.getHeight()));
testShape2.draw(g.create(120,10, testShape2.getWidth(), testShape2.getHeight()));
testShape3.draw(g.create(10,120, testShape3.getWidth(), testShape3.getHeight()));
testShape4.draw(g.create(120,120, testShape4.getWidth(), testShape4.getHeight()));
}
};
mainPanel.add( panel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
JCheckBox speed = new JCheckBox("Ralentir");
speed.addChangeListener((e)-> test.setSpeed( speed.isSelected()?SLOW:0 ));
JButton buttonFillIn = new JButton("Remplir (in)");
JButton buttonFillOut = new JButton("Remplir (out)");
buttonPanel.add( speed );
buttonPanel.add( buttonFillIn );
buttonPanel.add( buttonFillOut );
mainPanel.add( buttonPanel, BorderLayout.SOUTH);
buttonFillIn.addActionListener( e -> fill(test, shapes, buttonFillIn, buttonFillOut, panel, true) );
buttonFillOut.addActionListener( e -> fill(test, shapes, buttonFillIn, buttonFillOut, panel, false) );
frame.getContentPane().add(mainPanel);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void fill(FloodFill floodFill, AbsractShape[] shapes, JButton buttonFillIn,
JButton buttonFillOut, Component panel, boolean paintIn) {
buttonFillIn.setEnabled(false);
buttonFillOut.setEnabled(false);
new Thread() {
@Override
public void run() {
for(AbsractShape shape : shapes) {
shape.clear();
}
panel.repaint();
for(AbsractShape shape : shapes) {
floodFill.fill( panel, shape.getGrid(), paintIn?shape.getInPoint():shape.getOutPoint(), FILL_COLOR);
}
SwingUtilities.invokeLater(() -> { buttonFillIn.setEnabled(true); buttonFillOut.setEnabled(true); });
}
}.start();
}
private long speed;
public void setSpeed(long speed) {
this.speed=speed;
}
public void fill(Component component, int[][] grid, Point fillPoint, Color fill) {
final int startx = fillPoint.x;
final int starty = fillPoint.y;
final int height = grid.length;
final int width;
int maxwidth = 0;
for (int y = 0; y < height; y++) {
int currentWidth = grid[y].length;
if (currentWidth > maxwidth) {
maxwidth = currentWidth;
}
}
width = maxwidth;
final int originalColor = grid[starty][startx];
final int fillColor = fill.getRGB();
int minx=width;
int maxx=0;
int miny=height;
int maxy=0;
for(int x=0; x<width; x++) {
for( int y=0; y<height; y++) {
if ( grid[y][x]!=originalColor ) {
if ( x<minx) minx=x;
if ( x>maxx) maxx=x;
if ( y<miny) miny=y;
if ( y>maxy) maxy=y;
}
}
}
if ( minx==maxx ) {
// cas particulier : ligne verticale
if ( startx==minx ) {
// draw in
for(int y=miny; y<=maxy; y++) {
fill(component, grid, startx, y, fillColor);
}
}
else {
// draw out
for(int y=0; y<height; y++) {
for(int x=0; x<width; x++) {
if ( x!=minx || y<miny || y>maxy ) {
fill(component, grid, x, y, fillColor);
}
}
}
}
}
else if ( miny==maxy ) {
// cas particulier : ligne horizontal
if ( starty==miny ) {
// draw in
for(int x=minx; x<=maxx; x++) {
fill(component, grid, x, starty, fillColor);
}
}
else {
// draw out
for(int y=0; y<height; y++) {
for(int x=0; x<width; x++) {
if ( y!=miny || x<minx || x>maxx ) {
fill(component, grid, x, y, fillColor);
}
}
}
}
}
else {
// + 2 pour ajouter des bordures afin de simuler un rectangle autour (pour le remplissage à l'extérieur de la forme)
int[][] nbgrid = new int[height+2][width+2];
for(int y=0; y<nbgrid.length; y++) {
for(int x=0; x<nbgrid[y].length; x++) {
if ( x==0 || y==0 || x==nbgrid[y].length-1 || y==nbgrid.length-1 ) {
nbgrid[y][x]=1;
}
else if ( x>0 && x-1<width && y>0 && y-1<height ) {
if ( grid[y-1][x-1]!=originalColor ) { // on copie les points de la bordure
nbgrid[y][x]=1;
}
}
}
}
Point p = new Point(startx+1, starty+1);
int color = -1;
while (color < 0)
{
if ( checkBounds(p.x,p.y,nbgrid) ) {
nbgrid[p.y][p.x] = color--;
}
if (!search(nbgrid, p, 0))
{
if ( checkBounds(p.x, p.y, nbgrid) ) {
nbgrid[p.y][p.x] = 1;
if ( nbgrid[p.y][p.x]==1 ) {
fill(component, nbgrid, grid, p, fillColor);
}
}
color += 2;
search(nbgrid, p, color);
}
}
}
}
private void fill(Component component, int[][] nbgrid, int[][] grid, Point p, int fillColor) {
if ( nbgrid[p.y][p.x]==1 ) {
fill(component, grid, p.x-1, p.y-1, fillColor);
}
}
private boolean search(int[][] nbgrid, Point p, int color) {
if ( checkBounds(p.x-1, p.y, nbgrid) ) {
if (nbgrid[p.y][p.x-1] == color) {
p.x--;
return true;
}
}
else {
return false;
}
if ( checkBounds(p.x+1, p.y, nbgrid) ) {
if (nbgrid[p.y][p.x+1] == color) {
p.x++;
return true;
}
}
else {
return false;
}
if ( checkBounds(p.x, p.y-1, nbgrid) ) {
if (nbgrid[p.y-1][p.x] == color) {
p.y--;
return true;
}
}
else {
return false;
}
if ( checkBounds(p.x, p.y+1, nbgrid) ) {
if (nbgrid[p.y+1][p.x] == color) {
p.y++;
return true;
}
}
else {
return false;
}
return false;
}
private boolean checkBounds(int x, int y, int[][] grid) {
return checkBounds(y, 0, grid.length) && checkBounds(x, 0, grid[y].length);
}
private boolean checkBoundsIncluded(int x, int min, int max) {
return x>=min && x<=max;
}
private boolean checkBounds(int x, int min, int max) {
return x>=min && x<max;
}
private void fill(Component component, int[][] grid, int x, int y, int fillColor) {
grid[y][x]=fillColor;
component.repaint();
slowDown();
}
private void slowDown() {
if ( speed>0 ) {
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
private static abstract class AbsractShape {
private final static int BORDER_COLOR = Color.BLACK.getRGB(); // La couleur de bordure
private final int width;
private final int height;
private final int[][] grid;
private final int[][] clearGrid;
public AbsractShape() {
grid = createGrid();
height = grid.length;
clearGrid = new int[height][];
int maxwidth = 0;
for (int y = 0; y < height; y++) {
int currentWidth = grid[y].length;
clearGrid[y] = Arrays.copyOf(grid[y], currentWidth);
if (currentWidth > maxwidth) {
maxwidth = currentWidth;
}
}
width = maxwidth;
}
public final int[][] getGrid() {
return grid;
}
/**
* Le point de départ du remplissage à l'intérieur de la forme
* @return
*/
public abstract Point getInPoint();
/**
* Le point de départ du remplissage à l'extérieur de la forme
* @return
*/
public abstract Point getOutPoint();
public void clear() {
for (int y = 0; y < height; y++) {
for( int x=0; x<width; x++) {
grid[y][x]=clearGrid[y][x];
}
}
}
public final int getWidth() {
return width;
}
public final int getHeight() {
return height;
}
public Dimension getSize() {
return new Dimension(width, height);
}
protected int[][] createGrid() {
BufferedImage image = createImage();
return grabArray(image);
}
private int[][] grabArray(BufferedImage image) {
final int[][] array = new int[image.getHeight()][image.getWidth()];
for(int y=0; y<array.length; y++) {
for(int x=0; x<array[y].length; x++) {
int pixel = image.getRGB(x, y);
array[y][x] = pixel<0?BORDER_COLOR:0;
}
}
return array;
}
/*Doit être une image ARGB, ou les lignes sont en Color.WHITE */
protected abstract BufferedImage createImage();
public final void draw(Graphics graphics) {
final BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
final int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer())
.getData();
for (int y = 0; y < grid.length; y++) {
for (int x = 0; x < grid[y].length; x++) {
pixels[y * width + x] = grid[y][x];
}
}
graphics.drawImage(image, 0, 0, null);
}
}
public static class ShapeTest1 extends AbsractShape {
@Override
protected BufferedImage createImage() {
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = image.createGraphics();
try {
graphics.setColor(Color.WHITE);
final int[] px = new int[]{20, 30, 40, 50, 60, 60, 50, 60, 50, 40, 30, 20, 10,};
final int[] py = new int[]{10, 10, 20, 10, 20, 30, 40, 50, 60, 60, 50, 99, 99};
graphics.drawPolygon(
px,
py,
px.length);
return image;
}
finally {
graphics.dispose();
}
}
@Override
public Point getInPoint() {
return new Point(50,50);
}
@Override
public Point getOutPoint() {
return new Point(5,5);
}
}
public static class ShapeTest2 extends AbsractShape {
@Override
protected BufferedImage createImage() {
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = image.createGraphics();
try {
graphics.setColor(Color.WHITE);
graphics.drawRect(10, 10, 80, 80);
graphics.drawRect(30, 30, 20, 20);
return image;
}
finally {
graphics.dispose();
}
}
@Override
public Point getInPoint() {
return new Point(20,20);
}
@Override
public Point getOutPoint() {
return new Point(40,40);
}
}
public static class ShapeTest3 extends AbsractShape {
@Override
protected BufferedImage createImage() {
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = image.createGraphics();
try {
graphics.setColor(Color.WHITE);
graphics.drawLine(10,10,
70,
10);
return image;
}
finally {
graphics.dispose();
}
}
@Override
public Point getInPoint() {
return new Point(20,10);
}
@Override
public Point getOutPoint() {
return new Point(60,60);
}
}
public static class ShapeTest4 extends AbsractShape {
@Override
protected BufferedImage createImage() {
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = image.createGraphics();
try {
graphics.setColor(Color.WHITE);
Area area = new Area(new Ellipse2D.Double(10,10,80,80));
area.subtract(new Area(new Ellipse2D.Double(30,30,40,40)));
area.subtract(new Area(new Rectangle2D.Double(45,0,10,60)));
((Graphics2D)graphics).draw(area);
return image;
}
finally {
graphics.dispose();
}
}
@Override
public Point getInPoint() {
return new Point(30,30);
}
@Override
public Point getOutPoint() {
return new Point(50,50);
}
}
} |
Partager