Bonjour tous le monde, s'il vous plaît, je n'arrive pas à bien comprendre ce code, normalement c'est un code qui coupe l'image en plusieurs morceaux et les stocke dans un tableau mais je ne comprends pas comment il détecte la position originale de l'image.
si quelqu'un arrive à le comprendre s'il vous plaît une explication de votre part est la bienvenue.
Merci beaucoup

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
 
 
 
/**
 *Each piece knows the portion of the image it contains, how its edges are to *be drawn, and what its
 * neighboring pieces are.
 *
 * <p> When two or more Pieces are put together, the result is another
 * Piece object, a MultiPiece.
 *
 * @see MultiPiece
 */
public class Piece implements Serializable
{
 
	// Class constants ------------------------------------------------------
	/** Unique id for the next piece */
	private static int nextId = 0;
 
	/** A Piece must be within this many pixels of "perfect" to be considered
         * close. */
	public static final int posClose = 7;
 
	/** A Piece must be within this many degrees of rotation from another to
         * be considered aligned to it. */
	public static final int rotClose = 5;
 
	// Constructor and fields -----------------------------------------------
	/** Creates a new Piece.  No initial rotation is done.  (This is needed
         * by MultiPiece, which needs to set its subpieces before rotating.)
         * @param data        image data
         * @param imageX      X position of image relative to entire puzzle
         * @param imageY      Y position of image relative to entire puzzle
         * @param imageWidth   width of original image
         * @param imageHeight height of original image
         * @param totalWidth  the width of the entire picture
         * @param totalHeight the height of the entire picture
         */
	protected Piece(int[] data, int imageX, int imageY, int imageWidth, int imageHeight, int totalWidth, int totalHeight)
	{
		id = nextId++;
		neighbors = new HashSet();
		this.curWidth  = this.origWidth  = imageWidth;
		this.curHeight = this.origHeight = imageHeight;
		origData = data;    	
    	this.imageX = imageX;
    	this.imageY = imageY;
    	this.totalWidth = totalWidth;
    	this.totalHeight = totalHeight;
 
    	// computing hash code:
    	if (data != null) {
    		int temp = 0;
    		for(int i = 0; i < data.length; i++) {
	    		temp += data[i];
    		}
    		this.hash = temp;
    	} else {
    		this.hash = 0;
    	}
	}
 
	/** Creates a new Piece.
         * @param data        image data
         * @param imageX      X position of image relative to entire puzzle
         * @param imageY      Y position of image relative to entire puzzle
         * @param imageWidth   width of original image
         * @param imageHeight height of original image
         * @param totalWidth  the width of the entire picture
         * @param totalHeight the height of the entire picture
         * @param rotation    initial rotation
         */
	public Piece(int[] data, int imageX, int imageY, int imageWidth, int imageHeight, int totalWidth, int totalHeight, int rotation)
	{
		this (data, imageX, imageY, imageWidth, imageHeight, totalWidth, totalHeight);
    	forceSetRotation (rotation);
	}
 
	/**
         * A Copy Constructor.
         * @param p
         */
	public Piece(Piece p) {
		this(p.getOrigData(), p.getImageX(), p.getImageY(), 
		p.getImageWidth(), p.getImageHeight(), p.getTotalWidth(), p.getTotalHeight(), p.getRotation());
	}
 
	/** Piece's unique id. */
	protected int id;
 
	/** Location in the image. */
  	int imageX, imageY;
 
  	/** Size of the entire image. */
  	int totalWidth, totalHeight;
 
  	/** Location in the image adjusted by current rotation. */
  	int rotatedX, rotatedY;
 
  	/** Pieces considered to be neighbors to this one.  They are the only
         * ones that can be fitted to it.
         */
  	protected Set neighbors;
 
  	/** Original image size and data. */
  	protected int origWidth, origHeight;
  	int[] origData;
 
  	/** Current size and data, taking rotation into account. */
  	protected int curWidth, curHeight;
  	int[] curData;
 
  	/** Location in the puzzle panel. */
  	int puzzleX, puzzleY;
 
	/** Translation from a piece's upper-left corner to the point last clicked on. */
	int transX, transY;
 
	/** constant hash code of the piece. */
	protected int hash;
 
  	/** Image for this Piece.  We're going to now try making this null for
         * non-atomic pieces. */
  	Image image;
 
  	/** This is measured in integer degrees, 0-359.  0 is unrotated.  
         * 90 is 90 degrees clockwise, etc. */
  	int rotation;
 
  	// Accessors ------------------------------------------------------------
 
  	/** Returns this piece's unique id. */
  	public int getId() {return id;}
 
  	/** Returns this Piece's current rotation.  The rotation is given in
         * integer degrees clockwise, and will always be between 0 and 359
         * inclusive.
         */
  	public int getRotation() { return rotation; }
 
  	/** Sets this Piece's current rotation.  The rotation is given in integer
         * degrees clockwise, and should always be between 0 and 359 inclusive.
         * If the new rotation is different, this Piece's image data will be
         * recomputed.
         * @throws IllegalArgumentException if rotation is not in [0,359]
         */
  	public void setRotation (int rot)
  	{
  		if (rot == rotation) return;
  		forceSetRotation (rot);
  	}
 
  	/** Sets this Piece's current rotation.  Unlike setRotation(), this
         * method forces a recompute of the image.
         * @throws IllegalArgumentException if rotation is not in [0,359]
         */
  	protected void forceSetRotation (int rot)
  	{
  		if ((rot < 0) || (rot > 359)) throw new IllegalArgumentException("invalid rotation: "+rot);
  		// For now, allow only 0, 90, 180, 270.
  		if (rot % 90 != 0) {
  			int newRot = rot / 90;
  			System.out.println ("unsupported rotation "+rot+"; using "+newRot+" instead");
  			rot = newRot;
  		}
  		rotation = rot;
  		recomputeImageData();
  		if (image != null) image.flush();
  		image = Toolkit.getDefaultToolkit().createImage (
  		new MemoryImageSource (curWidth,curHeight, curData, 0, curWidth));
  	}
 
  	/** Fulshes the image, unless it is null. */
  	public void flushImage() {
  		if (image!= null) {
  			image.flush();
  		}
		image = Toolkit.getDefaultToolkit().createImage (
				new MemoryImageSource (curWidth,curHeight, curData, 0, curWidth));
  	}
 
  	/** Sets this Piece's upper-left position relative to the upper-left
         *position of the JigsawPuzzle.
         */
  	public void setPuzzlePosition (int x, int y)
  	{
  		this.puzzleX = x;
  		this.puzzleY = y;
  	}
 
  	/** Returns this Piece's current height in pixels.  This is the height of
         * the smallest rectangle containing all of this Piece's image data at
         * its current rotation.
         */
  	public int getCurrentHeight() { return curHeight; }
 
  	/** Returns this Piece's current width in pixels.  This is the width of
         * the smallest rectangle containing all of this Piece's image data at
         * its current rotation.
         */
  	public int getCurrentWidth() { return curWidth; }
 
  	/** Returns the width of the entire picture. */
  	public int getTotalWidth() { return totalWidth; }
 
  	/** Returns the height of the entire picture. */
  	public int getTotalHeight() { return totalHeight; }
 
  	/** Returns this Piece's image height in pixels.  This is the height of
         * the smallest rectangle containing all of this Piece's image data in
         * its original orientation.
         */
  	public int getImageHeight() { return origHeight; }
 
  	/** Returns this Piece's image width in pixels.  This is the width of the
         * smallest rectangle containing all of this Piece's image data in its
         * original orientation.
         */
  	public int getImageWidth() { return origWidth; }
 
  	/** Returns this Piece's X position in the original image. */
  	public int getImageX() { return imageX; }
 
  	/** Returns this Piece's Y position in the original image. */
  	public int getImageY() { return imageY; }
 
  	/** Returns this Piece's X position in the original image, modified by
         * its current rotation.  The origin is the center of rotation.  */
  	public int getRotatedX() { return rotatedX; }
 
  	/** Returns this Piece's Y position in the original image, modified by
         * its current rotation.  The origin is the center of rotation.  */
  	public int getRotatedY() { return rotatedY; }
 
  	/** Returns this Piece's X position in the original image, modified by
         * the given rotation.  */
  	public int getRotatedX(int rotation) { return rotatedX; }
 
  	/** Returns this Piece's Y position in the original image, modified by
         * the given rotation.  */
  	public int getRotatedY(int rotation) { return rotatedY; }
 
  	/** Returns this Piece's X position in the puzzle. */
  	public int getPuzzleX() { return puzzleX; }
 
  	/** Returns this Piece's Y position in the puzzle. */
  	public int getPuzzleY() { return puzzleY; }
 
  	/** Returns thie Piece's transX - last pressed X position. */
  	public int getTransX() {return transX; }
  	public void setTransX(int x) {transX = x;}
 
	/** Returns thie Piece's transY - last pressed Y position. */
	public int getTransY() {return transY; }
	public void setTransY(int y) {transY = y;}
 
  	public HashSet getNeighbors() {
  		return (HashSet)neighbors;
  	}
 
  	public void setNeighbors(HashSet neighbors) {
  		this.neighbors = neighbors;
  	}
  	/** Returns this Piece's current image.  This will be the Piece's portion
         * of the original image, rotated by this Piece's current rotation.
         */
  	public Image getImage () { return image; }
 
  	/** Returns current piece's data */
  	public int[] getCurrData() {return curData;	}
  	public void setCurrData(int[] data) { curData = data; }
	/** Returns original piece's data */
  	public int[] getOrigData() {
  		return this.origData;
  	}
  	public void setOrigData(int[] data) {
  		this.origData = data;
  	}
 
  	/** Returns a hash value - by the current data */
  	public int hashCode() {
  		return hash+rotation;
  	}
 
 
  	/** Adds a Piece to this Piece's set of neighbors.
         * @throws NullPointerException if neighbor is null
         */
  	public void addNeighbor (Piece neighbor)
  	{
  		if (neighbor == null) throw new NullPointerException ("null neighbor");
  		neighbors.add (neighbor);
  	}
 
  	/** Removes the given Piece from this Piece's set of neighbors. */
  	public void removeNeighbor (Piece neighbor) 
  	{
  		neighbors.remove (neighbor); 
  	}
 
  	/** Moves this Piece to the given location, relative to the puzzle
         * panel's upper-left corner.
         */
  	public void moveTo (int x, int y)
  	{ 
  		setPuzzlePosition (x, y); 
  	}
 
 	/** Returns a string representation of a piece, which can be written to the console. */
  	/*public String toString()
  	{
  		String msg = "";
  		msg += "Piece {" + "\n";
  		msg += "	ID = " + id + "\n";
		msg += "	(imageX, imageY) = ("+imageX+", "+imageY+")" + "\n";
		msg += "	SIZE = "+origWidth+"x"+origHeight+"\n";
		msg += "	Rotation = " + rotation+ "\n";
		msg += "	(rotatedX, rotatedY) = " + "(" + rotatedX+", "+rotatedY+")\n";
		msg += "	(puzzleX, puzzleY) = ("+puzzleX+", "+puzzleY+")\n";
		msg += "	(transX, transY) = (" + transX + ", " + transY + ")\n";
		msg += "}\n";
 
		return msg;
  	}*/
 
  	/** Draws this Piece in the given Graphics object.  The current image
         * will be drawn, at this Piece's current puzzle position.  */
  	protected void draw (Graphics g)
  	{
  		Image img = getImage();
  		if (img != null) g.drawImage (img, getPuzzleX(), getPuzzleY(), null);
  	}
 
  	/** Returns whether this Piece currently contains the given point,
         * relative to the puzzle panel's upper-left corner.
         */
  	public boolean contains (int x, int y)
  	{
  		int puzX = getPuzzleX();
  		int puzY = getPuzzleY();
  		int w = getCurrentWidth();
  		int h = getCurrentHeight();
 
  		return (puzX <= x) && (x <= (puzX+w-1)) && (puzY <= y) && (y <= (puzY+h-1)) &&
  			   (getAlpha(x-puzX, y-puzY) != 0);
  	}
 
  	/** Returns the alpha (transparency) value at the given coordinates in
         * the current image data.
         */
  	protected int getAlpha (int x, int y)
  	{
  		int pixel = curData[y*curWidth + x];
  		return (pixel >> 24) & 0xff;
  	}
 
  	/** Returns whether this piece is located and oriented close enough to
         * the given Piece to be fitted.
         */
  	public boolean isCloseTo (Piece piece)
  	{
  		// Check if pieces are aligned
  		int rot = getRotation();
  		int rotD = Math.abs (piece.getRotation() - getRotation());
  		rotD = Math.min (rotD, 360-rotD);
  		if (rotD > rotClose) return false;
  		// If we're here - pieces are aligned
  		int puzXD = getPuzzleX() - piece.getPuzzleX();
  		int puzYD = getPuzzleY() - piece.getPuzzleY();
  		int rotXD = getRotatedX() - piece.getRotatedX(rot);
  		int rotYD = getRotatedY() - piece.getRotatedY(rot);
  		return (Math.abs(puzXD-rotXD) <= posClose) && (Math.abs(puzYD-rotYD) <= posClose) ;
 
  	}
 
  	// Joining pieces -------------------------------------------------------
 
  	/** Checks whether any of this Piece's neighbors are located and oriented
         * close enough to be joined to this one.
         * @return an array of Pieces, or null if no neighbors were close enough;
         * if the array is non-null, the first Piece will be the new one;
         * subsequent Pieces will be the ones it was built from
         */
  	public Piece[] join ()
  	{
  		ArrayList close = new ArrayList();
  		Iterator iter = neighbors.iterator();
  		while (iter.hasNext()) {
  			Piece piece = (Piece) iter.next();
  			if (piece.isCloseTo (this)) 
  			{
  				close.add (piece);
  			}
  		}
  		if (close.size() == 0) return null;
  		// We can't just return the new MultiPiece, because the JigsawPuzzle
  		// needs to know what pieces the new one was formed from that are
  		// currently in its list.  These might include other MultiPieces, which
  		// wouldn't be in the new Piece's subpiece list.
  		Piece newPiece = MultiPiece.join (this, close);
  		Piece[] ret = new Piece[close.size()+2];
  		ret[0] = newPiece;
  		ret[1] = this;
  		this.image.flush();
  		for (int i = 0; i < close.size(); i++) {
  			Piece piece = (Piece) close.get(i);
  			ret[i+2] = piece;
  			piece.image.flush();
  		}
  		System.gc();
  		return ret;
  	}
 
  	// Bevel drawing --------------------------------------------------------
  	/** Draws bevels on data.  Check every opaque pixel's NW and SE
         * neighbors.  If NW is transparent and SE is opaque, brighten the
         * central pixel.  If it's the other way around, darken it.  If both or
         * neither are transparent, leave it alone.
         */
  	static void bevel (int[] data, int width, int height)
  	{
  		// Scan diagonal NW-SE lines.  The first and last lines can be skipped.
  		for (int i = 0; i < width+height-3; i++) 
  		{
  			int x = Math.max (0, i-height+2);
  			int y = Math.max (0, height-i-2);
  			boolean nw, c, se; // true iff that pixel is opaque
  			nw = c = se = false;
  			c = (((data[y*width+x] >> 24) & 0xff) > 0);
  			while ((x < width) && (y < height)) {
  				if ((x+1 < width) && (y+1 < height))
  				{
  					se = (((data[(y+1)*width+(x+1)] >> 24) & 0xff) > 0);
  				}
  				else 
  				{	
  					se = false;
  				} 
 
  				if (c) 
  				{
  					int datum = data[y*width+x];
  					if ( nw && !se) 
  					{
  						data[y*width+x] =   darker (datum);
  					}
  					if (!nw &&  se) 
  					{
  						data[y*width+x] = brighter (datum);
  					}
  				}
  				nw = c;
  				c = se;
  				x++; y++;
  			}
  		}
  	}
 
  	// This mimics Color.brighter() and Color.darker().  They multiply or
  	// divide R/G/B by 0.7, and trim them to 0 or 255 if needed.  I'm going
  	// to use 7/10 (so it's int arithmetic), and not use Math.  I don't quite
  	// trust inlining yet.  And I certainly don't want to make scads of Color
  	// objects for each pixel.  It's bad enough these are methods, and not
  	// inlined in bevel().
  	static final int fn = 10;
  	static final int fd =  7;
  	static final int maxB =  255 * fd / fn;
  	static final int brighter (int val) {
  		int r = (val >> 16) & 0xff;
  		int g = (val >>  8) & 0xff;
  		int b = (val      ) & 0xff;
  		// Black goes to #030303 gray
  		if ((r==0) && (g==0) && (b==0)) return 0xff030303;
  		r = (r < 3) ? 3 : r;
  		g = (g < 3) ? 3 : g;
  		b = (b < 3) ? 3 : b;
  		r = (r >= maxB) ? 255 : (r * fn / fd);
  		g = (g >= maxB) ? 255 : (g * fn / fd);
  		b = (b >= maxB) ? 255 : (b * fn / fd);
  		return ((((0xff00 | r) << 8) | g) << 8) | b;
  	}
 
  	static final int darker (int val) {
  		int r = (val >> 16) & 0xff;
  		int g = (val >>  8) & 0xff;
  		int b = (val      ) & 0xff;
  		r = r * fd / fn;
  		g = g * fd / fn;
  		b = b * fd / fn;
  		return ((((0xff00 | r) << 8) | g) << 8) | b;
  	}
 
  	// 4-way rotation -------------------------------------------------------
  	/** Sets this Piece's rotated position and size, based on its current
         * rotation.  The entire puzzle is rotated about the origin, and then
         * translated so that its new upper left corner is at the origin.  Each
         * piece's rotated position is then defined by its new upper left corner
         * in the rotated puzzle.
         */
  	protected void setRotatedPosition()
  	{
  		int rot = getRotation();
  		switch (rot) {
  			case   0:	rotatedX =  imageX; rotatedY =  imageY;
  						curWidth = origWidth; curHeight = origHeight; 
  						break;
  			case  90:   rotatedX = totalHeight-imageY-origHeight;
  						rotatedY = imageX;
  						curWidth = origHeight; curHeight = origWidth; 
  						break;
  			case 180:	rotatedX = totalWidth -imageX-origWidth;
  						rotatedY = totalHeight-imageY-origHeight;
  						curWidth = origWidth; curHeight = origHeight; 
  						break;
  			case 270:	rotatedX =  imageY;
  						rotatedY = totalWidth -imageX-origWidth;
  						curWidth = origHeight; curHeight = origWidth; 
  						break;
  			default:    System.out.println ("sRotPos() can't handle rotation: "+rot);
  		}
  	}
 
  	/** Recomputes this Piece's current image data and size from its original
         * image data and rotation.
         */
  	public void recomputeImageData()
  	{
  		setRotatedPosition();
  		if (rotation == 0) {
  			curData = (int[]) origData.clone();
  		} else if (rotation == 90) {
  			curData = new int[origData.length];
  			for (int i = 0; i < curWidth; i++) {
  				for (int j = 0; j < curHeight; j++) {
  					try {
  						curData[j*curWidth+i] = origData[(origHeight-i-1)*origWidth + j];
  					} catch (ArrayIndexOutOfBoundsException ex) {
  						//System.out.println ("ArrayIndexOutOfBoundsException");
  						//System.out.println (" olen="+origData.length+" clen="+curData.length);
  						//System.out.println (" i="+i+" j="+j);
  						//System.out.println (" orgW="+origWidth+" orgH="+origHeight);
  						//System.out.println (" curW="+ curWidth+" curH="+ curHeight);
  						//System.out.println (" cIdx="+ (j*curWidth+i)+" oIdx="+ ((origWidth-j-1)*origHeight + i));
  						throw new NullPointerException();
  					}
  				}
  			}
  		} else if (rotation == 180) {
  			curData = new int[origData.length];
  			for (int i = 0; i < curWidth; i++) {
  				for (int j = 0; j < curHeight; j++) {
  					curData[j*curWidth+i] = origData[(origHeight-j-1)*origWidth + (origWidth-i-1)];
  				}
  			}
  		} else if (rotation == 270) {
  			curData = new int[origData.length];
  			for (int i = 0; i < curWidth; i++) {
  				for (int j = 0; j < curHeight; j++) {
  					curData[j*curWidth+i] = origData[i*origWidth + (origWidth-j-1)];
  				}
  			}
  		} 
  		bevel(curData, curWidth, curHeight);
  		image = Toolkit.getDefaultToolkit().createImage (
  			new MemoryImageSource (curWidth,curHeight, curData, 0, curWidth));
  	}
}