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