Browse Source

Importation du code depuis GitHub

gasc 2 years ago
commit
0f37235b10

+ 16
- 0
README.md View File

@@ -0,0 +1,16 @@
1
+# Cours de programmation orientée objet (I3MIIL11)
2
+Mes TD de programmation orientée objet (INSA Toulouse - 3 MIC) d'après [les sujets de D. Le Botlan](http://wwwperso.insa-toulouse.fr/~lebotlan/Y/java3mic.html).
3
+
4
+## Compilation
5
+Pour le TD6, il n'y a pas de script pour compiler, vous pouvez le faire ainsi (sous linux uniquement) :
6
+```bash
7
+$ mkdir build
8
+$ javac *.java
9
+$ mv *.class build/
10
+$ cd build
11
+$ java ReadFS /dir/
12
+```
13
+ou en one-liner :
14
+```
15
+rm -rf build; javac *.java; mkdir build; mv *.class build/; cd build; java ReadFS /dir/; cd ..; rm -rf build
16
+```

+ 71
- 0
TD1-5/Anim.java View File

@@ -0,0 +1,71 @@
1
+/** Boucle principale de la simulation */
2
+
3
+import java.awt.Color ;
4
+
5
+public class Anim {
6
+
7
+    /** Effectue une pause de la duree indiquee en millisecondes */
8
+    public static void pause(int duree) {
9
+	try {
10
+	    Thread.currentThread().sleep(duree) ;
11
+	} catch (InterruptedException e) {} 
12
+    }
13
+
14
+    /** Boucle principale */
15
+    public void go() {
16
+	int compteur = 0;
17
+
18
+	Plateau plat ;
19
+	Robot[] robots ;
20
+
21
+	plat = new Plateau(800, 600) ;
22
+	robots = new Robot[7] ;
23
+
24
+	robots[0] = new Robot("Aphrodite", "Images/mini1.png", 200, 100, plat, Color.RED, 0) ; 
25
+	robots[1] = new Robot("Arès", "Images/mini2.png", 600, 100, plat, Color.GREEN, 1) ; 
26
+	robots[2] = new Indestructible("Zeus", "Images/mini3.png", 200, 500, plat, Color.YELLOW, 2) ; 
27
+	robots[3] = new Cyborg("Déméter", "Images/mini7.png", 600, 500, plat, Color.PINK, 7, 3) ;
28
+	robots[4] = new Cyborg("Poséidon", "Images/mini4.png", 400, 300, plat, Color.BLUE, 5, 4) ;
29
+	robots[5] = new Killer("Hermès", "Images/mini5.png", 700, 300, plat, Color.GRAY, 5, 3) ;
30
+	robots[6] = new Killer("Athéna", "Images/mini6.png", 100, 300, plat, Color.ORANGE, 6, 8) ;
31
+
32
+
33
+	// On repete la boucle d'animation sans arret
34
+	while (true) {
35
+
36
+	    // On fait evoluer chaque robot
37
+	    for (int i = 0 ; i < robots.length ; i++) {
38
+			robots[i].bouge () ;
39
+	    }
40
+
41
+	    // Puis on teste les collisions deux a deux
42
+	    for (int i = 0 ; i < robots.length ; i++) {
43
+			for (int j = i+1 ; j < robots.length ; j++) {
44
+				robots[i].testeCollision(robots[j]) ;
45
+			}
46
+	    }
47
+
48
+		// Pour chaque robot, on met un petit cercle
49
+		if (compteur % 10 == 0){
50
+			for (int i = 0 ; i < robots.length ; i++) {
51
+				plat.setColor(robots[i].col);
52
+				plat.drawCircle(robots[i].x, robots[i].y, 5);
53
+			}
54
+		}
55
+		compteur++;
56
+
57
+	    // Petite pause
58
+	    java.awt.Toolkit.getDefaultToolkit().sync(); // Sinon l'animation est saccadée - probablement à cause du Window Manager.
59
+	    this.pause(12) ;
60
+	}
61
+
62
+    }
63
+    
64
+
65
+    public static void main(String[] args) {
66
+	Anim an = new Anim() ;
67
+	Images.init () ;
68
+	an.go () ;
69
+    }
70
+
71
+}

+ 26
- 0
TD1-5/Cyborg.java View File

@@ -0,0 +1,26 @@
1
+import java.awt.Color ;
2
+
3
+public class Cyborg extends Robot { 
4
+    /** Nombre total de collision */
5
+    private int nombreCollision = 0;
6
+
7
+    /** Nombre maximun de collision */
8
+    private int maxCollision = 6;
9
+
10
+    public Cyborg (String name, String nomImage, int init_x, int init_y, Plateau pt, Color col, int maxCollision, int sn) {
11
+		super(name, nomImage, init_x, init_y, pt, col, sn);
12
+        this.maxCollision = maxCollision;
13
+    }
14
+
15
+    @Override public void collision(Robot autre) {
16
+        //System.out.println("AH AH pauvre malheureux, tu viens de colisionner un CYBORG !");
17
+        doLogsThings(autre);
18
+        vx = this.randomV() ;
19
+		vy = this.randomV() ;
20
+
21
+        nombreCollision++ ;
22
+        if (nombreCollision > maxCollision) {
23
+            this.explose();
24
+        }
25
+    }
26
+}

+ 49
- 0
TD1-5/ImagePanel.java View File

@@ -0,0 +1,49 @@
1
+/**
2
+ *  Cette classe gère une image de fond + des sprites qui peuvent se déplacer
3
+ *  (Bas-niveau)
4
+ */
5
+
6
+import javax.swing.* ;
7
+import java.awt.* ;
8
+import java.util.* ;
9
+
10
+public class ImagePanel extends JPanel {
11
+
12
+    // L'image de fond qui est dessinée dans le cadre à chaque refresh.
13
+    private Image image ;
14
+
15
+    // La liste des sprites à dessiner.
16
+    private Vector<Sprite> sprites = new Vector<Sprite> () ;
17
+
18
+    public void setImage(Image image) {
19
+	this.image = image;
20
+    }
21
+
22
+    // Crée un nouveau sprite, aux coordonnées indiquées.
23
+    public Sprite addSprite(String imgName, int x, int y) {
24
+	Sprite s = new Sprite(imgName, x, y, this) ;
25
+	sprites.add(s) ;
26
+	return s ;
27
+    }
28
+
29
+    // Refresh image & dessine les sprites
30
+    public void paintComponent(Graphics g) {
31
+	if(image != null) {
32
+	    g.drawImage(image, 0, 0, this);
33
+	    
34
+	    for (Sprite s : sprites) {
35
+		s.dessine (g, this) ;
36
+	    }
37
+	}
38
+    }
39
+    
40
+    public Dimension getPreferredSize() {
41
+	int w, h;
42
+	if(image == null) {
43
+	    return new Dimension(0, 0);
44
+	}
45
+	w = image.getWidth(null);
46
+	h = image.getHeight(null);
47
+	return new Dimension(w > 0 ? w : 0, h > 0 ? h : 0);
48
+    }
49
+}

+ 29
- 0
TD1-5/Images.java View File

@@ -0,0 +1,29 @@
1
+import javax.imageio.* ;
2
+//import java.awt.* ;
3
+import java.awt.image.* ;
4
+import java.io.* ;
5
+
6
+public class Images {
7
+
8
+    public static BufferedImage[] explosion ;
9
+
10
+    public static BufferedImage get(String imgfile) {
11
+	try {
12
+	    // Read from a file
13
+	    File file = new File(imgfile);
14
+	    return ImageIO.read(file);
15
+	} catch (IOException e) {
16
+	    System.err.println("Le fichier " + imgfile + " est introuvable.") ;
17
+	    System.exit(1) ;
18
+	    return null ;
19
+	}
20
+    }
21
+
22
+    public static void init() {
23
+	explosion = new BufferedImage[18] ;
24
+	for (int i = 0 ; i < explosion.length ; i++) {
25
+	    explosion[i] = get("Images/ex" + (i+1) + ".png") ;
26
+	}
27
+    }
28
+
29
+}

BIN
TD1-5/Images/ex1.png View File


BIN
TD1-5/Images/ex10.png View File


BIN
TD1-5/Images/ex11.png View File


BIN
TD1-5/Images/ex12.png View File


BIN
TD1-5/Images/ex13.png View File


BIN
TD1-5/Images/ex14.png View File


BIN
TD1-5/Images/ex15.png View File


BIN
TD1-5/Images/ex16.png View File


BIN
TD1-5/Images/ex17.png View File


BIN
TD1-5/Images/ex18.png View File


BIN
TD1-5/Images/ex2.png View File


BIN
TD1-5/Images/ex3.png View File


BIN
TD1-5/Images/ex4.png View File


BIN
TD1-5/Images/ex5.png View File


BIN
TD1-5/Images/ex6.png View File


BIN
TD1-5/Images/ex7.png View File


BIN
TD1-5/Images/ex8.png View File


BIN
TD1-5/Images/ex9.png View File


BIN
TD1-5/Images/mini1.png View File


BIN
TD1-5/Images/mini2.png View File


BIN
TD1-5/Images/mini3.png View File


BIN
TD1-5/Images/mini4.png View File


BIN
TD1-5/Images/mini5.png View File


BIN
TD1-5/Images/mini6.png View File


BIN
TD1-5/Images/mini7.png View File


BIN
TD1-5/Images/mini8.png View File


+ 11
- 0
TD1-5/Indestructible.java View File

@@ -0,0 +1,11 @@
1
+import java.awt.Color ;
2
+
3
+public class Indestructible extends Robot { 
4
+    public Indestructible (String name, String nomImage, int init_x, int init_y, Plateau pt, Color col, int sn) {
5
+		super(name, nomImage, init_x, init_y, pt, col, sn);
6
+    }
7
+
8
+    @Override public void explose() {
9
+        //System.out.println("Tu as essayé de me tuer, mais je suis INDESTRUCTIBLE !");
10
+    }
11
+}

+ 31
- 0
TD1-5/Killer.java View File

@@ -0,0 +1,31 @@
1
+import java.awt.Color ;
2
+
3
+public class Killer extends Robot {
4
+    /** Tableau des robots déjà vu */
5
+    private Integer[] connaissances ;
6
+
7
+    /** Nombre de tolérences */
8
+    private int x ;
9
+
10
+    public Killer (String name, String nomImage, int init_x, int init_y, Plateau pt, Color col, int sn, int x) {
11
+		  super(name, nomImage, init_x, init_y, pt, col, sn);
12
+      connaissances = new Integer[7];
13
+      this.x = x ;
14
+
15
+      for (int i = 0; i < 7; ++i){
16
+        connaissances[i] = 0;
17
+      }
18
+    }
19
+
20
+    @Override public void collision(Robot autre) {
21
+        //System.out.println("Je ne t'ai vu que " + Integer.toString(connaissances[autre.getSN()]) + " fois toi, ça ira...");
22
+        doLogsThings(autre);
23
+        connaissances[autre.getSN()]++;
24
+        
25
+
26
+        if (connaissances[autre.getSN()] > x) {
27
+          autre.explose();
28
+          //System.out.println("Pauvre fou, je suis un KILLER !");
29
+        }
30
+    }
31
+}

+ 21
- 0
TD1-5/Logline.java View File

@@ -0,0 +1,21 @@
1
+import java.util.*;
2
+
3
+public class Logline {
4
+    protected Date d;
5
+    private String msg;
6
+    protected Robot r,tier;
7
+
8
+    public Logline(Date d, String msg, Robot self, Robot tier) {
9
+        this.d = d;
10
+        this.msg = msg;
11
+        this.r = self;
12
+        this.tier = tier;
13
+    }
14
+
15
+    @Override public String toString() {
16
+        if (tier != null) 
17
+            return "[" + d.toString() + "](" + r + ", " + tier + ") " + msg + "\n";
18
+        else
19
+            return "[" + d.toString() + "](" + r + ") " + msg + "\n";
20
+    }
21
+}

+ 24
- 0
TD1-5/Logs.java View File

@@ -0,0 +1,24 @@
1
+import java.util.*;
2
+
3
+public class Logs {
4
+    private Stack<Logline> logsStack = new Stack<Logline>();
5
+
6
+    public void add(String message, Robot emetteur, Robot tiers) {
7
+        logsStack.push(new Logline(new Date(), message, emetteur, tiers));
8
+    }
9
+
10
+    @Override public String toString() {
11
+        String str = "";
12
+        for (Logline ligne : logsStack)
13
+            str += ligne;
14
+        return str;
15
+    }
16
+
17
+    public Logline trouveLigne(Robot emetteur, Robot tiers) throws PasTrouve {
18
+        for (Logline i : logsStack)
19
+            if (i.r == emetteur && i.tier == tiers)
20
+                return i;
21
+        throw (new PasTrouve("Not found."));
22
+        //return null;
23
+    }
24
+}

+ 7
- 0
TD1-5/PasTrouve.java View File

@@ -0,0 +1,7 @@
1
+public class PasTrouve extends Exception {
2
+    String contenu;
3
+
4
+    public PasTrouve(String contenu) {
5
+        this.contenu = contenu;
6
+    }
7
+}

+ 83
- 0
TD1-5/Plateau.java View File

@@ -0,0 +1,83 @@
1
+/**
2
+ *   Ouvre une fenetre pour y afficher les sprites
3
+ *   ou y tracer des points, lignes, etc.
4
+ */
5
+
6
+import javax.swing.* ;
7
+import java.awt.* ;
8
+import java.awt.image.* ;
9
+
10
+public class Plateau {
11
+
12
+    // ImagePanel : contient le cadre et les sprites des robots.
13
+    private ImagePanel panel ;
14
+    
15
+    private JFrame frame ;
16
+    private BufferedImage img ;
17
+    private Graphics2D gr ;
18
+
19
+    private int larg ;
20
+    private int haut ;
21
+
22
+    /**
23
+     *  Crée et affiche une nouvelle fenêtre
24
+     */
25
+    public Plateau (int largeur, int hauteur) {
26
+        this.frame = new JFrame ("Le Monde des Robots");
27
+	this.frame.setResizable(false) ;
28
+	this.larg = largeur ;
29
+	this.haut = hauteur ;
30
+	this.img   = new BufferedImage (largeur, hauteur, BufferedImage.TYPE_3BYTE_BGR) ;
31
+	this.gr    = this.img.createGraphics() ;
32
+	this.panel = new ImagePanel () ;
33
+
34
+	this.panel.setImage(this.img) ;
35
+        this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
36
+        this.frame.setSize(largeur, hauteur);
37
+	
38
+	this.frame.setContentPane(this.panel) ;  
39
+        this.frame.setVisible(true);
40
+
41
+	this.setColor (new Color(180, 180, 200)) ;
42
+	gr.fillRect(0,0,largeur,hauteur) ;
43
+
44
+	this.setColor (Color.black) ;
45
+	this.repaint() ;
46
+    }
47
+
48
+    /** Renvoie la largeur de la fenetre */
49
+    public int getLarg () { return this.larg ;}
50
+
51
+    /** Renvoie la hauteur de la fenetre */
52
+    public int getHaut () { return this.haut ;}
53
+
54
+    /**
55
+     *  Force le reaffichage de la fenetre
56
+     */
57
+    public void repaint() {
58
+	panel.repaint() ;
59
+    }
60
+
61
+    /**
62
+     *  Fixe la couleur du crayon pour les dessins futurs.
63
+     */
64
+    public void setColor (Color col) {
65
+	this.gr.setColor (col) ;
66
+    }
67
+
68
+    /**
69
+     *  Dessine un cercle
70
+     */
71
+    public void drawCircle (int x, int y, int rayon) {
72
+	this.gr.fillOval(x - rayon, y - rayon, 2*rayon, 2*rayon) ;
73
+	panel.repaint (0, x - rayon, y - rayon, 2*rayon, 2*rayon) ;
74
+    }
75
+
76
+    /**
77
+     *  Cree un nouveau sprite a partir du fichier image indique'
78
+     */
79
+    public Sprite addSprite(String imgName, int x, int y) {
80
+	return panel.addSprite(imgName, x, y) ;
81
+    }
82
+
83
+}

+ 201
- 0
TD1-5/Robot.java View File

@@ -0,0 +1,201 @@
1
+import java.awt.Color ;
2
+
3
+public class Robot {
4
+
5
+    /* 
6
+     *  Les attributs de chaque Robot. 
7
+     */
8
+
9
+    /** Position du robot */
10
+    protected int x, y ;
11
+
12
+    /** Vitesse du robot */
13
+    protected int vx, vy ;
14
+
15
+    /** Etat du robot */
16
+    private boolean vivant ;
17
+
18
+    /** Sprite (==image) representant le robot */
19
+    private Sprite image ;
20
+    
21
+    /** Plateau sur lequel le robot evolue */
22
+    private Plateau plat ;
23
+
24
+    /** Ce robot est-il arrete' ? */
25
+    private boolean arrete ;
26
+
27
+	/** Couleur des cercles */
28
+	protected Color col ;
29
+
30
+	/** Serail number */
31
+	private int SN;
32
+
33
+	/** Son nom */
34
+	private String name;
35
+
36
+	/** Nombre max. d'explosion */
37
+	public static final int maxExplosions = 20;
38
+
39
+	/** Compte du nombre d'explosion */
40
+	protected static int nbExplosions;
41
+
42
+	/** Nombre de robots créés */
43
+	private static int nbRobot = 0;
44
+
45
+	/** Logs */
46
+	Logs l = new Logs();
47
+
48
+
49
+    /* 
50
+     *  Constructeur de Robot. 
51
+     */
52
+
53
+    /** Cree un nouveau robot avec l'image indiquee, a la position indiquee sur le plateau
54
+     *  La vitesse de depart est aleatoire
55
+     */
56
+    public Robot (String name, String nomImage, int init_x, int init_y, Plateau pt, Color col, int sn) {
57
+		this.x = init_x ;
58
+		this.y = init_y ;
59
+		this.vivant = true ;
60
+		this.vx = this.randomV() ;
61
+		this.vy = this.randomV() ;
62
+		this.plat = pt ;
63
+		this.image = pt.addSprite (nomImage, this.x, this.y) ;
64
+		this.arrete = false ;
65
+		this.col = col ;
66
+		this.SN = sn;
67
+		this.name = name;
68
+		nbRobot++;
69
+    }
70
+
71
+    /*
72
+     *  Méthodes
73
+     */
74
+
75
+
76
+    /** Renvoie une valeur de vitesse aleatoire */
77
+    public int randomV() { return (int)(Math.random() * 6) - 3 ; }
78
+
79
+    /** Recupere la largeur de l'image du robot */
80
+    public int getLarg() { return this.image.getLarg() ; }
81
+
82
+    /** Recupere la hauteur de l'image du robot */
83
+    public int getHaut() { return this.image.getHaut() ; }
84
+
85
+	/** Retourne le SN */
86
+	public int getSN() { return this.SN ; }
87
+
88
+
89
+    /** Effectue un deplacement elementaire du robot */
90
+    public void bouge() {
91
+
92
+	if (this.vivant) {
93
+
94
+	    this.x += this.vx ;
95
+	    this.y += this.vy ;
96
+	    	    
97
+	    // Si le robot rejoint un bord de la fenetre, il rebondit et change de direction
98
+	    int futurX = this.x + this.vx ;
99
+	    int futurY = this.y + this.vy ;
100
+	    
101
+	    // Collision avec le bord gauche
102
+	    if (futurX < 0) {
103
+		this.vx = Math.abs(this.vx) ;
104
+		this.vy = this.randomV() ;
105
+	    }
106
+
107
+	    // Collision avec le bord droit
108
+	    if (futurX + this.getLarg() > this.plat.getLarg()) {
109
+		this.vx = -Math.abs(this.vx) ;
110
+		this.vy = this.randomV() ;
111
+	    }
112
+
113
+	    // Collision avec le bord haut 
114
+	    if (futurY < 0) {
115
+		this.vy = Math.abs(this.vy) ;
116
+		this.vx = this.randomV() ;
117
+	    }
118
+
119
+	    // Collision avec le bord bas
120
+	    if (futurY + this.getHaut() > this.plat.getHaut()) {
121
+		this.vy = -Math.abs(this.vy) ;
122
+		this.vx = this.randomV() ;
123
+	    }
124
+	}
125
+
126
+	// Redessine le robot au nouvel endroit
127
+	// Force a redessiner, meme si le robot n'a pas bouge.
128
+	this.image.moveTo(this.x, this.y) ;
129
+
130
+    }
131
+
132
+    
133
+    /** Teste si ce robot est en collision avec le robot 'autre' */
134
+    public void testeCollision(Robot autre) {
135
+
136
+	// Pour etre en collision, il faut une intersection sur les X ET sur les Y
137
+	boolean enCollision = ((this.x + this.getLarg() >= autre.x) && (this.x < autre.x + autre.getLarg()) &&
138
+			       (this.y + this.getHaut() >= autre.y) && (this.y < autre.y + autre.getHaut())) ;
139
+
140
+	// Si on est en collision avec un robot different de soi-meme, on reagit et l'autre aussi.
141
+	if (this.vivant && autre.vivant && (autre != this) && enCollision) {
142
+
143
+	    // Si les deux robots sont deja arretes, on ne fait rien.
144
+	    if (this.arrete && autre.arrete) {}
145
+	    else {
146
+		this.collision(autre) ;
147
+		autre.collision(this) ;
148
+	    }
149
+	}
150
+    }
151
+
152
+    /** Arrêter ce robot. */
153
+    public void arreterRobot() {
154
+	this.vx = 0 ;
155
+	this.vy = 0 ;
156
+	this.arrete = true ;
157
+    }
158
+
159
+    /** Fait exploser ce robot */
160
+    public void explose() {
161
+		if (nbExplosions >= maxExplosions) {
162
+			return;
163
+		}
164
+		this.image.playSequence(Images.explosion, false) ;
165
+		this.vivant = false ;
166
+		nbExplosions++;
167
+
168
+		l.add("Explosion", this, null);
169
+		System.out.println(l.toString());
170
+    }
171
+
172
+	protected void doLogsThings(Robot autre){
173
+		direBonjour(autre);
174
+		l.add("Collision", this, autre);
175
+	}
176
+
177
+    /** Reaction a une collision : on s'arrete */
178
+    public void collision(Robot autre) {
179
+		//System.out.println("[Collision entre " + this + " et " + autre +"]");
180
+		doLogsThings(autre);
181
+		this.arreterRobot() ;
182
+    }
183
+
184
+	/** toString */
185
+	@Override public String toString() {
186
+		return this.name + " at " + this.x + ", " + this.y;
187
+		//return "Robot (" + this.name + ", sn. : " + this.SN + ") at " + this.x + ", " + this.y;
188
+	}
189
+
190
+	public void direBonjour(Robot autre) {
191
+		Logline line;
192
+
193
+		try {
194
+			line = l.trouveLigne(this, autre);
195
+			System.out.println("   Rebonjour, on s'est déjà croisé le " + line.d.toString());
196
+		}
197
+		catch (PasTrouve e){
198
+			System.out.println("   Bonjour, on ne se connaît pas.");
199
+		}
200
+	}
201
+}

+ 95
- 0
TD1-5/Sprite.java View File

@@ -0,0 +1,95 @@
1
+/**
2
+ *   Un sprite est une image qui a vocation a se deplacer.
3
+ *   (bas niveau)
4
+ */
5
+
6
+import java.awt.* ;
7
+import java.awt.image.* ;
8
+
9
+public class Sprite {
10
+
11
+    // Position, dimension et image du sprite
12
+    public int x, y ;
13
+    private int width, height ;
14
+    public BufferedImage image ;
15
+
16
+    // ImagePanel ou se trouve le sprite
17
+    private ImagePanel ip ;
18
+
19
+    // Animation du robot : sequence d'images
20
+    private BufferedImage[] seq ;
21
+
22
+    // Index dans la sequence. -1 signifie qu'on ne la joue pas.
23
+    private int indexSeq ; 
24
+
25
+    // Nombre d'expositions de chaque image de la sequence
26
+    private final int dureeExposition = 6 ;
27
+    private int compteurExposition ;
28
+
29
+    private boolean repeat ;
30
+    
31
+
32
+
33
+    /** Renvoie la largeur du sprite */
34
+    public int getLarg() { return this.width ; }
35
+
36
+    /** Renvoie la hauteur du sprite */
37
+    public int getHaut() { return this.height ; }
38
+
39
+    public Sprite (String imgfile, int x, int y, ImagePanel ip) {
40
+	
41
+	this.x = x ;
42
+	this.y = y ;
43
+	this.seq = null ;
44
+	this.indexSeq = -1 ;
45
+	this.ip = ip ;
46
+
47
+	this.image = Images.get(imgfile) ;
48
+	this.width  = this.image.getWidth() ;
49
+	this.height = this.image.getHeight() ;
50
+    }
51
+
52
+    // Indique à l'image panel de se redessiner sur la zone du sprite.
53
+    private void repaint() {
54
+	this.ip.repaint (0, this.x, this.y, width, height) ;
55
+    }
56
+
57
+    
58
+    public void moveTo (int x, int y) {
59
+        // Il faudra redessiner l'endroit d'où il part
60
+	this.repaint() ;
61
+	this.x = x ;
62
+	this.y = y ;
63
+        // Et l'endroit où il arrive.
64
+	this.repaint() ;
65
+    }
66
+
67
+    public void playSequence(BufferedImage[] seq, boolean repeat) {
68
+	this.seq = seq ;
69
+	this.indexSeq = 0 ;
70
+	this.compteurExposition = this.dureeExposition ;
71
+	this.repeat = repeat ;
72
+    }
73
+
74
+    public void dessine(Graphics g, ImagePanel im) {
75
+
76
+	if (this.indexSeq >= 0 && this.indexSeq < this.seq.length) {
77
+	    this.image = this.seq[this.indexSeq] ;
78
+
79
+	    this.width  = this.image.getWidth() ;
80
+	    this.height = this.image.getHeight() ;
81
+
82
+	    if (this.compteurExposition-- <= 0) {
83
+		this.compteurExposition = this.dureeExposition ;
84
+		this.indexSeq ++ ;
85
+	    }
86
+
87
+	    if (this.indexSeq >= this.seq.length && this.repeat) {
88
+		this.indexSeq = 0 ;
89
+	    }
90
+	}
91
+
92
+	g.drawImage(this.image, this.x, this.y, im) ;
93
+
94
+    }
95
+}

+ 54
- 0
TD1-5/build.sh View File

@@ -0,0 +1,54 @@
1
+#!/bin/bash
2
+
3
+red='\e[0;31m'
4
+green='\e[0;32m'
5
+orange='\e[0;33m'
6
+neutral='\e[0;m'
7
+
8
+printout() {
9
+        echo -e "[${green}INFO${neutral}] $@"
10
+}
11
+
12
+printhelp() {
13
+        echo -e "[${orange}HELP${neutral}] $@"
14
+}
15
+
16
+printerr() {
17
+        echo -e "[${red}ERROR${neutral}] $@"
18
+}
19
+
20
+if test $# -ne 1
21
+then
22
+  printhelp "Veuillez spécifier le nom du programme à lancer."
23
+  printhelp "Utilisation de Anim par défaut"
24
+  a="Anim"
25
+else
26
+  a=$1
27
+fi
28
+
29
+printout "Compilation avec javac..."
30
+javac *.java
31
+
32
+if test $? -eq 0
33
+then
34
+    printout "Succès de la compilation !"
35
+else
36
+    printerr "Echec de la compilation."
37
+    exit 1
38
+fi
39
+
40
+rm build -rf 2>&1 > /dev/null
41
+mkdir build
42
+mv *.class build/
43
+cp -R Images build/
44
+
45
+printout "Lancement du programme..."
46
+cd build
47
+
48
+printhelp "Logs : "
49
+java $a
50
+
51
+cd - 2>&1 > /dev/null
52
+rm build -rf 2>&1 > /dev/null
53
+
54
+exit 0

+ 35
- 0
TD1-5/notes.md View File

@@ -0,0 +1,35 @@
1
+## Notes de TD - POO java
2
+### TD 1
3
+* Pour passer votre programme à un collègue, que devez-vous lui envoyer : les fichiers .java, les fichiers .class, les deux ?
4
+
5
+On ne lui envoie que les .class, ça suffit et il est pas question qu'il nous pique notre code.
6
+    
7
+* Combien d'objets sont créés explicitement dans la méthode go() ?
8
+
9
+What does "Explicitement" means ? Sinon, si c'est à chaque `new`, bah 6.
10
+
11
+* Comment récupère-t-on la taille d'un tableau ?
12
+
13
+`.length`
14
+
15
+* Localisez le constructeur de la classe Robot. A-t-il autant d'arguments que la classe a d'attributs ?
16
+
17
+Nope.
18
+    
19
+* Où est appelé le constructeur de la classe Robot ?
20
+
21
+Anim.java, l. 24 - 27 :
22
+```java
23
+robots[0] = new Robot("Images/mini1.png", 200, 100, plat, Color.RED) ;
24
+```
25
+
26
+* Où est appelée la méthode collision() de la classe Robot ?
27
+
28
+Robot.java > testeCollision > l. 121 - 122 :
29
+```java
30
+this.collision(autre) ;
31
+```
32
+
33
+* Dans la classe Robot, examinez le corps de testeCollision. Cette méthode manipule deux robots, pourtant elle n'a qu'un seul argument. Expliquer ce mystère.
34
+
35
+Il y a l'objet en lui même et son copain.

+ 57
- 0
TD6/Dossier.java View File

@@ -0,0 +1,57 @@
1
+import java.util.* ;
2
+
3
+public class Dossier extends Node {
4
+    // Liste des éléments contenus
5
+    ArrayList<Node> elements = new ArrayList<Node>();
6
+
7
+    public Dossier (String path, int depth) {
8
+        super(path, 0, depth);
9
+        this.depth = depth;
10
+        Iterator<String> son;
11
+        String p;
12
+        long s = 0; 
13
+        long sum = 0;
14
+        try {
15
+            son = FileInfo.getElements(path);
16
+        }
17
+        catch (java.io.IOException e) {
18
+            son =  null;
19
+        }
20
+        while (son.hasNext()) {
21
+            p = son.next();
22
+            try {
23
+                s = FileInfo.size(p);
24
+                sum += s;
25
+            }
26
+            catch (java.io.IOException e) {}
27
+
28
+            if (this.depth + 1 <= maxDepth && nbNode <= maxNode){
29
+                if (FileInfo.isFile(p)) {
30
+                    elements.add(new Fichier(p, s, this.depth + 1));
31
+                }
32
+                else 
33
+                    elements.add(new Dossier(p, this.depth + 1));
34
+            }
35
+        }
36
+        Collections.sort(this.elements, comparator);
37
+        this.size = sum;
38
+    }
39
+
40
+    Comparator<Node> comparator = new Comparator<Node>() {
41
+        @Override public int compare(Node node1, Node node2) {
42
+            return node1.name.compareTo(node2.name);
43
+        }
44
+    };
45
+
46
+    @Override public void afficher() {
47
+        System.out.println("+ (DIR) " + this.name + "[total = " + size + " octets]");
48
+        for (Node i : elements) {
49
+            if (i.depth <= maxDepth){
50
+                for (int j = 0; j < depth; ++j){
51
+                    System.out.print("   ");
52
+                }
53
+                i.afficher();
54
+            }
55
+        }
56
+    }
57
+}

+ 9
- 0
TD6/Fichier.java View File

@@ -0,0 +1,9 @@
1
+public class Fichier extends Node {
2
+    public Fichier (String path, long size, int depth) {
3
+        super(path, size, depth);
4
+    }
5
+
6
+    @Override public void afficher() {
7
+        System.out.println("| " + this.name + "[total = " + size + " octets]");
8
+    }
9
+}

+ 70
- 0
TD6/FileInfo.java View File

@@ -0,0 +1,70 @@
1
+import java.io.* ;
2
+import java.util.* ;
3
+
4
+public class FileInfo {
5
+
6
+    // No constructor
7
+    private FileInfo() { }
8
+    
9
+    /**
10
+     *  @param path an absolute path to a file or directory
11
+     *  @return the name of the file or directory
12
+     */
13
+    public static String getName(String path) {
14
+	return (new File(path)).getName() ;
15
+    }
16
+
17
+    /**
18
+     *  @param path an absolute path to a file
19
+     *  @return the size of the file
20
+     *  @throws java.io.IOException if the file does not exist or if it is a directory.
21
+     */
22
+    public static long size(String path) throws IOException {
23
+	File file = new File(path) ;
24
+	if (file.exists() && file.isFile()) return file.length() ;
25
+	else if (!file.exists ()) throw (new IOException("File " + path + " does not exist.")) ;
26
+	else throw (new IOException(path + " is not a regular file.")) ;
27
+    }
28
+
29
+    /**
30
+     *  @param path an absolute path to a file or a directory
31
+     *  @return true if it is a directory
32
+     */
33
+    public static boolean isDirectory(String path) {
34
+	return (new File(path)).isDirectory() ;
35
+    }
36
+
37
+    /**
38
+     *  @param path an absolute path to a file or a directory
39
+     *  @return true if it is a regular file
40
+     */
41
+    public static boolean isFile(String path) {
42
+	return (new File(path)).isFile() ;
43
+    }
44
+
45
+    /**
46
+     * @paral path an absolute path to a directory
47
+     * @return an iterator over the paths of all elements in this directory.
48
+     * @throws java.io.IOException if it is not a directory.
49
+     */
50
+    public static Iterator<String> getElements(String path) throws IOException {
51
+	File file = new File(path) ;
52
+
53
+	if (!file.exists ()) throw (new IOException("Directory " + path + " does not exist.")) ;
54
+	if (!file.isDirectory()) throw (new IOException(path + " is not a directory.")) ;
55
+
56
+	String[] elements = file.list() ;
57
+	// If the directory could not be read, elements is null here.
58
+	if (elements == null) {
59
+	    elements = new String[0] ;
60
+	}
61
+	
62
+	for (int i = 0 ; i < elements.length ; i++) {
63
+	    elements[i] = path + File.separator + elements[i] ;
64
+	}
65
+        
66
+	// if (elements == null) throw (new IOException("Cannot read directory " + path)) ;
67
+
68
+	return Arrays.asList(elements).iterator() ;
69
+    }
70
+}

+ 33
- 0
TD6/Node.java View File

@@ -0,0 +1,33 @@
1
+public abstract class Node {
2
+    // Node max
3
+    static protected int maxNode = 100;
4
+    static public int nbNode = 0;
5
+
6
+    // Profondeur max
7
+    static protected int maxDepth = 3;
8
+
9
+    // Chemin vers l'élément
10
+    protected String path;
11
+
12
+    // Nom de l'élément
13
+    protected String name;
14
+
15
+    // Taille de l'élément
16
+    protected long size;
17
+
18
+    // Profondeur 
19
+    protected int depth;
20
+
21
+
22
+    // Constructeur
23
+    public Node(String path, long size, int depth) {
24
+        nbNode++;
25
+        this.path = path;
26
+        this.size = size;
27
+        this.depth = depth;
28
+        this.name = FileInfo.getName(path);
29
+    }
30
+
31
+    public abstract void afficher();
32
+
33
+}

+ 6
- 0
TD6/ReadFS.java View File

@@ -0,0 +1,6 @@
1
+public class ReadFS {
2
+    public static void main (String args[]) {
3
+        Dossier first = new Dossier(args[0], 0);
4
+        first.afficher();
5
+    }
6
+}

Loading…
Cancel
Save