| 1 | import java.util.Comparator; |
| 2 | |
| 3 | @ClassPreamble ( |
| 4 | author = "Jérôme Benoit", |
| 5 | date = "09/11/2009" |
| 6 | ) |
| 7 | public class Image extends Forme implements Compactable, |
| 8 | Comparable<Image>, |
| 9 | Comparator<Image> { |
| 10 | final int IMAGE_MAX_SIZE = 100; |
| 11 | Forme[] formeCollection; |
| 12 | int formeNumber; |
| 13 | |
| 14 | Image() { |
| 15 | super(); |
| 16 | formeCollection = new Forme[IMAGE_MAX_SIZE]; |
| 17 | formeNumber = 0; |
| 18 | } |
| 19 | |
| 20 | Image(Point p) { |
| 21 | super(p); |
| 22 | formeCollection = new Forme[IMAGE_MAX_SIZE]; |
| 23 | formeNumber = 0; |
| 24 | } |
| 25 | |
| 26 | public int getSize() { |
| 27 | return formeNumber; |
| 28 | } |
| 29 | |
| 30 | private boolean isEmpty() { |
| 31 | return (formeNumber == 0); |
| 32 | } |
| 33 | |
| 34 | private boolean isFull() { |
| 35 | return (formeNumber >= formeCollection.length); |
| 36 | } |
| 37 | |
| 38 | public void dessiner(Piletransformations pile) { |
| 39 | for (int i = 0; i < formeNumber; i++) { |
| 40 | formeCollection[i].dessiner(pile); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | public void deplacer(Point p) { |
| 45 | for (int i = 0; i < formeNumber; i++) { |
| 46 | formeCollection[i].deplacer(p); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | public boolean ajouter(Forme forme) { |
| 51 | boolean rtVal = false; |
| 52 | if (!isFull()) { |
| 53 | formeCollection[formeNumber] = forme; |
| 54 | formeNumber++; |
| 55 | rtVal = true; |
| 56 | } |
| 57 | return rtVal; |
| 58 | } |
| 59 | |
| 60 | public boolean supprimer(int index) { |
| 61 | boolean rtVal = false; |
| 62 | if (!isEmpty()) { |
| 63 | for (int j = index; (j < formeNumber - 1 && index < formeNumber); j++) { |
| 64 | formeCollection[j] = formeCollection[j + 1]; |
| 65 | } |
| 66 | rtVal = true; |
| 67 | formeNumber--; |
| 68 | } |
| 69 | return rtVal; |
| 70 | } |
| 71 | |
| 72 | public void afficher() { |
| 73 | String className = this.getClass().getSimpleName(); |
| 74 | System.out.println("---- " + className + " ----"); |
| 75 | for (int i = 0; i < formeNumber; i++) { |
| 76 | formeCollection[i].afficher(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | public void compacter(int nElements) { |
| 81 | // Heavy solution |
| 82 | /* int minIndex = formeNumber - nElements - 1; |
| 83 | for (int i = formeNumber - 1; (i > minIndex && !isEmpty()); i--) { |
| 84 | supprimer(i); |
| 85 | } */ |
| 86 | // Lightweight solution |
| 87 | if (formeNumber - nElements > 0) { |
| 88 | formeNumber -= nElements; |
| 89 | } else { |
| 90 | formeNumber = 0; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public int compareTo(Image image) { |
| 95 | return formeNumber - image.getSize(); |
| 96 | } |
| 97 | |
| 98 | public int compare(Image image1, Image image2) { |
| 99 | return (int)(image1.getpOri().getY() - image2.getpOri().getY()); |
| 100 | } |
| 101 | |
| 102 | } |