TP2: Fix the Compactable implementation in Image class.
[TP_POO.git] / TP2 / Image.java
1
2 class Image extends Forme implements Compactable {
3 final int IMAGE_MAX_SIZE = 100;
4 Forme[] formeCollection;
5 int formeNumber;
6
7 Image() {
8 super();
9 formeCollection = new Forme[IMAGE_MAX_SIZE];
10 formeNumber = 0;
11 }
12
13 Image(Point p) {
14 super(p);
15 formeCollection = new Forme[IMAGE_MAX_SIZE];
16 formeNumber = 0;
17 }
18
19 private boolean isEmpty() {
20 return (formeNumber == 0);
21 }
22
23 private boolean isFull() {
24 return (formeNumber >= formeCollection.length);
25 }
26
27 public void dessiner(Piletransformations pile) {
28 for (int i = 0; i < formeNumber; i++) {
29 formeCollection[i].dessiner(pile);
30 }
31 }
32
33 public void deplacer(Point p) {
34 for (int i = 0; i < formeNumber; i++) {
35 formeCollection[i].deplacer(p);
36 }
37 }
38
39 public boolean ajouter(Forme forme) {
40 boolean rtVal = false;
41 if (!isFull()) {
42 formeCollection[formeNumber] = forme;
43 formeNumber++;
44 rtVal = true;
45 }
46 return rtVal;
47 }
48
49 public boolean supprimer(int index) {
50 boolean rtVal = false;
51 if (!isEmpty()) {
52 for (int j = index; (j < formeNumber - 1 && index < formeNumber); j++) {
53 formeCollection[j] = formeCollection[j + 1];
54 }
55 rtVal = true;
56 formeNumber--;
57 }
58 return rtVal;
59 }
60
61 public void afficher() {
62 String className = this.getClass().getSimpleName();
63 System.out.println("---- " + className + " ----");
64 for (int i = 0; i < formeNumber; i++) {
65 formeCollection[i].afficher();
66 }
67 }
68
69 public void compacter(int nElements) {
70 int minIndex = formeNumber - nElements - 1;
71 for (int i = formeNumber - 1; (i > minIndex && !isEmpty()); i--) {
72 supprimer(i);
73 }
74 }
75
76 }