Importation du code depuis GitHub
16
README.md
Normal file
|
@ -0,0 +1,16 @@
|
|||
# Cours de programmation orientée objet (I3MIIL11)
|
||||
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).
|
||||
|
||||
## Compilation
|
||||
Pour le TD6, il n'y a pas de script pour compiler, vous pouvez le faire ainsi (sous linux uniquement) :
|
||||
```bash
|
||||
$ mkdir build
|
||||
$ javac *.java
|
||||
$ mv *.class build/
|
||||
$ cd build
|
||||
$ java ReadFS /dir/
|
||||
```
|
||||
ou en one-liner :
|
||||
```
|
||||
rm -rf build; javac *.java; mkdir build; mv *.class build/; cd build; java ReadFS /dir/; cd ..; rm -rf build
|
||||
```
|
71
TD1-5/Anim.java
Normal file
|
@ -0,0 +1,71 @@
|
|||
/** Boucle principale de la simulation */
|
||||
|
||||
import java.awt.Color ;
|
||||
|
||||
public class Anim {
|
||||
|
||||
/** Effectue une pause de la duree indiquee en millisecondes */
|
||||
public static void pause(int duree) {
|
||||
try {
|
||||
Thread.currentThread().sleep(duree) ;
|
||||
} catch (InterruptedException e) {}
|
||||
}
|
||||
|
||||
/** Boucle principale */
|
||||
public void go() {
|
||||
int compteur = 0;
|
||||
|
||||
Plateau plat ;
|
||||
Robot[] robots ;
|
||||
|
||||
plat = new Plateau(800, 600) ;
|
||||
robots = new Robot[7] ;
|
||||
|
||||
robots[0] = new Robot("Aphrodite", "Images/mini1.png", 200, 100, plat, Color.RED, 0) ;
|
||||
robots[1] = new Robot("Arès", "Images/mini2.png", 600, 100, plat, Color.GREEN, 1) ;
|
||||
robots[2] = new Indestructible("Zeus", "Images/mini3.png", 200, 500, plat, Color.YELLOW, 2) ;
|
||||
robots[3] = new Cyborg("Déméter", "Images/mini7.png", 600, 500, plat, Color.PINK, 7, 3) ;
|
||||
robots[4] = new Cyborg("Poséidon", "Images/mini4.png", 400, 300, plat, Color.BLUE, 5, 4) ;
|
||||
robots[5] = new Killer("Hermès", "Images/mini5.png", 700, 300, plat, Color.GRAY, 5, 3) ;
|
||||
robots[6] = new Killer("Athéna", "Images/mini6.png", 100, 300, plat, Color.ORANGE, 6, 8) ;
|
||||
|
||||
|
||||
// On repete la boucle d'animation sans arret
|
||||
while (true) {
|
||||
|
||||
// On fait evoluer chaque robot
|
||||
for (int i = 0 ; i < robots.length ; i++) {
|
||||
robots[i].bouge () ;
|
||||
}
|
||||
|
||||
// Puis on teste les collisions deux a deux
|
||||
for (int i = 0 ; i < robots.length ; i++) {
|
||||
for (int j = i+1 ; j < robots.length ; j++) {
|
||||
robots[i].testeCollision(robots[j]) ;
|
||||
}
|
||||
}
|
||||
|
||||
// Pour chaque robot, on met un petit cercle
|
||||
if (compteur % 10 == 0){
|
||||
for (int i = 0 ; i < robots.length ; i++) {
|
||||
plat.setColor(robots[i].col);
|
||||
plat.drawCircle(robots[i].x, robots[i].y, 5);
|
||||
}
|
||||
}
|
||||
compteur++;
|
||||
|
||||
// Petite pause
|
||||
java.awt.Toolkit.getDefaultToolkit().sync(); // Sinon l'animation est saccadée - probablement à cause du Window Manager.
|
||||
this.pause(12) ;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Anim an = new Anim() ;
|
||||
Images.init () ;
|
||||
an.go () ;
|
||||
}
|
||||
|
||||
}
|
26
TD1-5/Cyborg.java
Normal file
|
@ -0,0 +1,26 @@
|
|||
import java.awt.Color ;
|
||||
|
||||
public class Cyborg extends Robot {
|
||||
/** Nombre total de collision */
|
||||
private int nombreCollision = 0;
|
||||
|
||||
/** Nombre maximun de collision */
|
||||
private int maxCollision = 6;
|
||||
|
||||
public Cyborg (String name, String nomImage, int init_x, int init_y, Plateau pt, Color col, int maxCollision, int sn) {
|
||||
super(name, nomImage, init_x, init_y, pt, col, sn);
|
||||
this.maxCollision = maxCollision;
|
||||
}
|
||||
|
||||
@Override public void collision(Robot autre) {
|
||||
//System.out.println("AH AH pauvre malheureux, tu viens de colisionner un CYBORG !");
|
||||
doLogsThings(autre);
|
||||
vx = this.randomV() ;
|
||||
vy = this.randomV() ;
|
||||
|
||||
nombreCollision++ ;
|
||||
if (nombreCollision > maxCollision) {
|
||||
this.explose();
|
||||
}
|
||||
}
|
||||
}
|
49
TD1-5/ImagePanel.java
Normal file
|
@ -0,0 +1,49 @@
|
|||
/**
|
||||
* Cette classe gère une image de fond + des sprites qui peuvent se déplacer
|
||||
* (Bas-niveau)
|
||||
*/
|
||||
|
||||
import javax.swing.* ;
|
||||
import java.awt.* ;
|
||||
import java.util.* ;
|
||||
|
||||
public class ImagePanel extends JPanel {
|
||||
|
||||
// L'image de fond qui est dessinée dans le cadre à chaque refresh.
|
||||
private Image image ;
|
||||
|
||||
// La liste des sprites à dessiner.
|
||||
private Vector<Sprite> sprites = new Vector<Sprite> () ;
|
||||
|
||||
public void setImage(Image image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
// Crée un nouveau sprite, aux coordonnées indiquées.
|
||||
public Sprite addSprite(String imgName, int x, int y) {
|
||||
Sprite s = new Sprite(imgName, x, y, this) ;
|
||||
sprites.add(s) ;
|
||||
return s ;
|
||||
}
|
||||
|
||||
// Refresh image & dessine les sprites
|
||||
public void paintComponent(Graphics g) {
|
||||
if(image != null) {
|
||||
g.drawImage(image, 0, 0, this);
|
||||
|
||||
for (Sprite s : sprites) {
|
||||
s.dessine (g, this) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Dimension getPreferredSize() {
|
||||
int w, h;
|
||||
if(image == null) {
|
||||
return new Dimension(0, 0);
|
||||
}
|
||||
w = image.getWidth(null);
|
||||
h = image.getHeight(null);
|
||||
return new Dimension(w > 0 ? w : 0, h > 0 ? h : 0);
|
||||
}
|
||||
}
|
29
TD1-5/Images.java
Normal file
|
@ -0,0 +1,29 @@
|
|||
import javax.imageio.* ;
|
||||
//import java.awt.* ;
|
||||
import java.awt.image.* ;
|
||||
import java.io.* ;
|
||||
|
||||
public class Images {
|
||||
|
||||
public static BufferedImage[] explosion ;
|
||||
|
||||
public static BufferedImage get(String imgfile) {
|
||||
try {
|
||||
// Read from a file
|
||||
File file = new File(imgfile);
|
||||
return ImageIO.read(file);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Le fichier " + imgfile + " est introuvable.") ;
|
||||
System.exit(1) ;
|
||||
return null ;
|
||||
}
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
explosion = new BufferedImage[18] ;
|
||||
for (int i = 0 ; i < explosion.length ; i++) {
|
||||
explosion[i] = get("Images/ex" + (i+1) + ".png") ;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
BIN
TD1-5/Images/ex1.png
Normal file
After Width: | Height: | Size: 276 B |
BIN
TD1-5/Images/ex10.png
Normal file
After Width: | Height: | Size: 2.1 KiB |
BIN
TD1-5/Images/ex11.png
Normal file
After Width: | Height: | Size: 1.8 KiB |
BIN
TD1-5/Images/ex12.png
Normal file
After Width: | Height: | Size: 1.6 KiB |
BIN
TD1-5/Images/ex13.png
Normal file
After Width: | Height: | Size: 1.4 KiB |
BIN
TD1-5/Images/ex14.png
Normal file
After Width: | Height: | Size: 999 B |
BIN
TD1-5/Images/ex15.png
Normal file
After Width: | Height: | Size: 480 B |
BIN
TD1-5/Images/ex16.png
Normal file
After Width: | Height: | Size: 241 B |
BIN
TD1-5/Images/ex17.png
Normal file
After Width: | Height: | Size: 132 B |
BIN
TD1-5/Images/ex18.png
Normal file
After Width: | Height: | Size: 132 B |
BIN
TD1-5/Images/ex2.png
Normal file
After Width: | Height: | Size: 648 B |
BIN
TD1-5/Images/ex3.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
TD1-5/Images/ex4.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
TD1-5/Images/ex5.png
Normal file
After Width: | Height: | Size: 1.4 KiB |
BIN
TD1-5/Images/ex6.png
Normal file
After Width: | Height: | Size: 1.6 KiB |
BIN
TD1-5/Images/ex7.png
Normal file
After Width: | Height: | Size: 2.1 KiB |
BIN
TD1-5/Images/ex8.png
Normal file
After Width: | Height: | Size: 2.1 KiB |
BIN
TD1-5/Images/ex9.png
Normal file
After Width: | Height: | Size: 2.1 KiB |
BIN
TD1-5/Images/mini1.png
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
TD1-5/Images/mini2.png
Normal file
After Width: | Height: | Size: 8.6 KiB |
BIN
TD1-5/Images/mini3.png
Normal file
After Width: | Height: | Size: 7.7 KiB |
BIN
TD1-5/Images/mini4.png
Normal file
After Width: | Height: | Size: 6.2 KiB |
BIN
TD1-5/Images/mini5.png
Normal file
After Width: | Height: | Size: 4.2 KiB |
BIN
TD1-5/Images/mini6.png
Normal file
After Width: | Height: | Size: 6 KiB |
BIN
TD1-5/Images/mini7.png
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
TD1-5/Images/mini8.png
Normal file
After Width: | Height: | Size: 6.5 KiB |
11
TD1-5/Indestructible.java
Normal file
|
@ -0,0 +1,11 @@
|
|||
import java.awt.Color ;
|
||||
|
||||
public class Indestructible extends Robot {
|
||||
public Indestructible (String name, String nomImage, int init_x, int init_y, Plateau pt, Color col, int sn) {
|
||||
super(name, nomImage, init_x, init_y, pt, col, sn);
|
||||
}
|
||||
|
||||
@Override public void explose() {
|
||||
//System.out.println("Tu as essayé de me tuer, mais je suis INDESTRUCTIBLE !");
|
||||
}
|
||||
}
|
31
TD1-5/Killer.java
Normal file
|
@ -0,0 +1,31 @@
|
|||
import java.awt.Color ;
|
||||
|
||||
public class Killer extends Robot {
|
||||
/** Tableau des robots déjà vu */
|
||||
private Integer[] connaissances ;
|
||||
|
||||
/** Nombre de tolérences */
|
||||
private int x ;
|
||||
|
||||
public Killer (String name, String nomImage, int init_x, int init_y, Plateau pt, Color col, int sn, int x) {
|
||||
super(name, nomImage, init_x, init_y, pt, col, sn);
|
||||
connaissances = new Integer[7];
|
||||
this.x = x ;
|
||||
|
||||
for (int i = 0; i < 7; ++i){
|
||||
connaissances[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void collision(Robot autre) {
|
||||
//System.out.println("Je ne t'ai vu que " + Integer.toString(connaissances[autre.getSN()]) + " fois toi, ça ira...");
|
||||
doLogsThings(autre);
|
||||
connaissances[autre.getSN()]++;
|
||||
|
||||
|
||||
if (connaissances[autre.getSN()] > x) {
|
||||
autre.explose();
|
||||
//System.out.println("Pauvre fou, je suis un KILLER !");
|
||||
}
|
||||
}
|
||||
}
|
21
TD1-5/Logline.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
import java.util.*;
|
||||
|
||||
public class Logline {
|
||||
protected Date d;
|
||||
private String msg;
|
||||
protected Robot r,tier;
|
||||
|
||||
public Logline(Date d, String msg, Robot self, Robot tier) {
|
||||
this.d = d;
|
||||
this.msg = msg;
|
||||
this.r = self;
|
||||
this.tier = tier;
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
if (tier != null)
|
||||
return "[" + d.toString() + "](" + r + ", " + tier + ") " + msg + "\n";
|
||||
else
|
||||
return "[" + d.toString() + "](" + r + ") " + msg + "\n";
|
||||
}
|
||||
}
|
24
TD1-5/Logs.java
Normal file
|
@ -0,0 +1,24 @@
|
|||
import java.util.*;
|
||||
|
||||
public class Logs {
|
||||
private Stack<Logline> logsStack = new Stack<Logline>();
|
||||
|
||||
public void add(String message, Robot emetteur, Robot tiers) {
|
||||
logsStack.push(new Logline(new Date(), message, emetteur, tiers));
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
String str = "";
|
||||
for (Logline ligne : logsStack)
|
||||
str += ligne;
|
||||
return str;
|
||||
}
|
||||
|
||||
public Logline trouveLigne(Robot emetteur, Robot tiers) throws PasTrouve {
|
||||
for (Logline i : logsStack)
|
||||
if (i.r == emetteur && i.tier == tiers)
|
||||
return i;
|
||||
throw (new PasTrouve("Not found."));
|
||||
//return null;
|
||||
}
|
||||
}
|
7
TD1-5/PasTrouve.java
Normal file
|
@ -0,0 +1,7 @@
|
|||
public class PasTrouve extends Exception {
|
||||
String contenu;
|
||||
|
||||
public PasTrouve(String contenu) {
|
||||
this.contenu = contenu;
|
||||
}
|
||||
}
|
83
TD1-5/Plateau.java
Normal file
|
@ -0,0 +1,83 @@
|
|||
/**
|
||||
* Ouvre une fenetre pour y afficher les sprites
|
||||
* ou y tracer des points, lignes, etc.
|
||||
*/
|
||||
|
||||
import javax.swing.* ;
|
||||
import java.awt.* ;
|
||||
import java.awt.image.* ;
|
||||
|
||||
public class Plateau {
|
||||
|
||||
// ImagePanel : contient le cadre et les sprites des robots.
|
||||
private ImagePanel panel ;
|
||||
|
||||
private JFrame frame ;
|
||||
private BufferedImage img ;
|
||||
private Graphics2D gr ;
|
||||
|
||||
private int larg ;
|
||||
private int haut ;
|
||||
|
||||
/**
|
||||
* Crée et affiche une nouvelle fenêtre
|
||||
*/
|
||||
public Plateau (int largeur, int hauteur) {
|
||||
this.frame = new JFrame ("Le Monde des Robots");
|
||||
this.frame.setResizable(false) ;
|
||||
this.larg = largeur ;
|
||||
this.haut = hauteur ;
|
||||
this.img = new BufferedImage (largeur, hauteur, BufferedImage.TYPE_3BYTE_BGR) ;
|
||||
this.gr = this.img.createGraphics() ;
|
||||
this.panel = new ImagePanel () ;
|
||||
|
||||
this.panel.setImage(this.img) ;
|
||||
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
this.frame.setSize(largeur, hauteur);
|
||||
|
||||
this.frame.setContentPane(this.panel) ;
|
||||
this.frame.setVisible(true);
|
||||
|
||||
this.setColor (new Color(180, 180, 200)) ;
|
||||
gr.fillRect(0,0,largeur,hauteur) ;
|
||||
|
||||
this.setColor (Color.black) ;
|
||||
this.repaint() ;
|
||||
}
|
||||
|
||||
/** Renvoie la largeur de la fenetre */
|
||||
public int getLarg () { return this.larg ;}
|
||||
|
||||
/** Renvoie la hauteur de la fenetre */
|
||||
public int getHaut () { return this.haut ;}
|
||||
|
||||
/**
|
||||
* Force le reaffichage de la fenetre
|
||||
*/
|
||||
public void repaint() {
|
||||
panel.repaint() ;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fixe la couleur du crayon pour les dessins futurs.
|
||||
*/
|
||||
public void setColor (Color col) {
|
||||
this.gr.setColor (col) ;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dessine un cercle
|
||||
*/
|
||||
public void drawCircle (int x, int y, int rayon) {
|
||||
this.gr.fillOval(x - rayon, y - rayon, 2*rayon, 2*rayon) ;
|
||||
panel.repaint (0, x - rayon, y - rayon, 2*rayon, 2*rayon) ;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cree un nouveau sprite a partir du fichier image indique'
|
||||
*/
|
||||
public Sprite addSprite(String imgName, int x, int y) {
|
||||
return panel.addSprite(imgName, x, y) ;
|
||||
}
|
||||
|
||||
}
|
201
TD1-5/Robot.java
Normal file
|
@ -0,0 +1,201 @@
|
|||
import java.awt.Color ;
|
||||
|
||||
public class Robot {
|
||||
|
||||
/*
|
||||
* Les attributs de chaque Robot.
|
||||
*/
|
||||
|
||||
/** Position du robot */
|
||||
protected int x, y ;
|
||||
|
||||
/** Vitesse du robot */
|
||||
protected int vx, vy ;
|
||||
|
||||
/** Etat du robot */
|
||||
private boolean vivant ;
|
||||
|
||||
/** Sprite (==image) representant le robot */
|
||||
private Sprite image ;
|
||||
|
||||
/** Plateau sur lequel le robot evolue */
|
||||
private Plateau plat ;
|
||||
|
||||
/** Ce robot est-il arrete' ? */
|
||||
private boolean arrete ;
|
||||
|
||||
/** Couleur des cercles */
|
||||
protected Color col ;
|
||||
|
||||
/** Serail number */
|
||||
private int SN;
|
||||
|
||||
/** Son nom */
|
||||
private String name;
|
||||
|
||||
/** Nombre max. d'explosion */
|
||||
public static final int maxExplosions = 20;
|
||||
|
||||
/** Compte du nombre d'explosion */
|
||||
protected static int nbExplosions;
|
||||
|
||||
/** Nombre de robots créés */
|
||||
private static int nbRobot = 0;
|
||||
|
||||
/** Logs */
|
||||
Logs l = new Logs();
|
||||
|
||||
|
||||
/*
|
||||
* Constructeur de Robot.
|
||||
*/
|
||||
|
||||
/** Cree un nouveau robot avec l'image indiquee, a la position indiquee sur le plateau
|
||||
* La vitesse de depart est aleatoire
|
||||
*/
|
||||
public Robot (String name, String nomImage, int init_x, int init_y, Plateau pt, Color col, int sn) {
|
||||
this.x = init_x ;
|
||||
this.y = init_y ;
|
||||
this.vivant = true ;
|
||||
this.vx = this.randomV() ;
|
||||
this.vy = this.randomV() ;
|
||||
this.plat = pt ;
|
||||
this.image = pt.addSprite (nomImage, this.x, this.y) ;
|
||||
this.arrete = false ;
|
||||
this.col = col ;
|
||||
this.SN = sn;
|
||||
this.name = name;
|
||||
nbRobot++;
|
||||
}
|
||||
|
||||
/*
|
||||
* Méthodes
|
||||
*/
|
||||
|
||||
|
||||
/** Renvoie une valeur de vitesse aleatoire */
|
||||
public int randomV() { return (int)(Math.random() * 6) - 3 ; }
|
||||
|
||||
/** Recupere la largeur de l'image du robot */
|
||||
public int getLarg() { return this.image.getLarg() ; }
|
||||
|
||||
/** Recupere la hauteur de l'image du robot */
|
||||
public int getHaut() { return this.image.getHaut() ; }
|
||||
|
||||
/** Retourne le SN */
|
||||
public int getSN() { return this.SN ; }
|
||||
|
||||
|
||||
/** Effectue un deplacement elementaire du robot */
|
||||
public void bouge() {
|
||||
|
||||
if (this.vivant) {
|
||||
|
||||
this.x += this.vx ;
|
||||
this.y += this.vy ;
|
||||
|
||||
// Si le robot rejoint un bord de la fenetre, il rebondit et change de direction
|
||||
int futurX = this.x + this.vx ;
|
||||
int futurY = this.y + this.vy ;
|
||||
|
||||
// Collision avec le bord gauche
|
||||
if (futurX < 0) {
|
||||
this.vx = Math.abs(this.vx) ;
|
||||
this.vy = this.randomV() ;
|
||||
}
|
||||
|
||||
// Collision avec le bord droit
|
||||
if (futurX + this.getLarg() > this.plat.getLarg()) {
|
||||
this.vx = -Math.abs(this.vx) ;
|
||||
this.vy = this.randomV() ;
|
||||
}
|
||||
|
||||
// Collision avec le bord haut
|
||||
if (futurY < 0) {
|
||||
this.vy = Math.abs(this.vy) ;
|
||||
this.vx = this.randomV() ;
|
||||
}
|
||||
|
||||
// Collision avec le bord bas
|
||||
if (futurY + this.getHaut() > this.plat.getHaut()) {
|
||||
this.vy = -Math.abs(this.vy) ;
|
||||
this.vx = this.randomV() ;
|
||||
}
|
||||
}
|
||||
|
||||
// Redessine le robot au nouvel endroit
|
||||
// Force a redessiner, meme si le robot n'a pas bouge.
|
||||
this.image.moveTo(this.x, this.y) ;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/** Teste si ce robot est en collision avec le robot 'autre' */
|
||||
public void testeCollision(Robot autre) {
|
||||
|
||||
// Pour etre en collision, il faut une intersection sur les X ET sur les Y
|
||||
boolean enCollision = ((this.x + this.getLarg() >= autre.x) && (this.x < autre.x + autre.getLarg()) &&
|
||||
(this.y + this.getHaut() >= autre.y) && (this.y < autre.y + autre.getHaut())) ;
|
||||
|
||||
// Si on est en collision avec un robot different de soi-meme, on reagit et l'autre aussi.
|
||||
if (this.vivant && autre.vivant && (autre != this) && enCollision) {
|
||||
|
||||
// Si les deux robots sont deja arretes, on ne fait rien.
|
||||
if (this.arrete && autre.arrete) {}
|
||||
else {
|
||||
this.collision(autre) ;
|
||||
autre.collision(this) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Arrêter ce robot. */
|
||||
public void arreterRobot() {
|
||||
this.vx = 0 ;
|
||||
this.vy = 0 ;
|
||||
this.arrete = true ;
|
||||
}
|
||||
|
||||
/** Fait exploser ce robot */
|
||||
public void explose() {
|
||||
if (nbExplosions >= maxExplosions) {
|
||||
return;
|
||||
}
|
||||
this.image.playSequence(Images.explosion, false) ;
|
||||
this.vivant = false ;
|
||||
nbExplosions++;
|
||||
|
||||
l.add("Explosion", this, null);
|
||||
System.out.println(l.toString());
|
||||
}
|
||||
|
||||
protected void doLogsThings(Robot autre){
|
||||
direBonjour(autre);
|
||||
l.add("Collision", this, autre);
|
||||
}
|
||||
|
||||
/** Reaction a une collision : on s'arrete */
|
||||
public void collision(Robot autre) {
|
||||
//System.out.println("[Collision entre " + this + " et " + autre +"]");
|
||||
doLogsThings(autre);
|
||||
this.arreterRobot() ;
|
||||
}
|
||||
|
||||
/** toString */
|
||||
@Override public String toString() {
|
||||
return this.name + " at " + this.x + ", " + this.y;
|
||||
//return "Robot (" + this.name + ", sn. : " + this.SN + ") at " + this.x + ", " + this.y;
|
||||
}
|
||||
|
||||
public void direBonjour(Robot autre) {
|
||||
Logline line;
|
||||
|
||||
try {
|
||||
line = l.trouveLigne(this, autre);
|
||||
System.out.println(" Rebonjour, on s'est déjà croisé le " + line.d.toString());
|
||||
}
|
||||
catch (PasTrouve e){
|
||||
System.out.println(" Bonjour, on ne se connaît pas.");
|
||||
}
|
||||
}
|
||||
}
|
95
TD1-5/Sprite.java
Normal file
|
@ -0,0 +1,95 @@
|
|||
/**
|
||||
* Un sprite est une image qui a vocation a se deplacer.
|
||||
* (bas niveau)
|
||||
*/
|
||||
|
||||
import java.awt.* ;
|
||||
import java.awt.image.* ;
|
||||
|
||||
public class Sprite {
|
||||
|
||||
// Position, dimension et image du sprite
|
||||
public int x, y ;
|
||||
private int width, height ;
|
||||
public BufferedImage image ;
|
||||
|
||||
// ImagePanel ou se trouve le sprite
|
||||
private ImagePanel ip ;
|
||||
|
||||
// Animation du robot : sequence d'images
|
||||
private BufferedImage[] seq ;
|
||||
|
||||
// Index dans la sequence. -1 signifie qu'on ne la joue pas.
|
||||
private int indexSeq ;
|
||||
|
||||
// Nombre d'expositions de chaque image de la sequence
|
||||
private final int dureeExposition = 6 ;
|
||||
private int compteurExposition ;
|
||||
|
||||
private boolean repeat ;
|
||||
|
||||
|
||||
|
||||
/** Renvoie la largeur du sprite */
|
||||
public int getLarg() { return this.width ; }
|
||||
|
||||
/** Renvoie la hauteur du sprite */
|
||||
public int getHaut() { return this.height ; }
|
||||
|
||||
public Sprite (String imgfile, int x, int y, ImagePanel ip) {
|
||||
|
||||
this.x = x ;
|
||||
this.y = y ;
|
||||
this.seq = null ;
|
||||
this.indexSeq = -1 ;
|
||||
this.ip = ip ;
|
||||
|
||||
this.image = Images.get(imgfile) ;
|
||||
this.width = this.image.getWidth() ;
|
||||
this.height = this.image.getHeight() ;
|
||||
}
|
||||
|
||||
// Indique à l'image panel de se redessiner sur la zone du sprite.
|
||||
private void repaint() {
|
||||
this.ip.repaint (0, this.x, this.y, width, height) ;
|
||||
}
|
||||
|
||||
|
||||
public void moveTo (int x, int y) {
|
||||
// Il faudra redessiner l'endroit d'où il part
|
||||
this.repaint() ;
|
||||
this.x = x ;
|
||||
this.y = y ;
|
||||
// Et l'endroit où il arrive.
|
||||
this.repaint() ;
|
||||
}
|
||||
|
||||
public void playSequence(BufferedImage[] seq, boolean repeat) {
|
||||
this.seq = seq ;
|
||||
this.indexSeq = 0 ;
|
||||
this.compteurExposition = this.dureeExposition ;
|
||||
this.repeat = repeat ;
|
||||
}
|
||||
|
||||
public void dessine(Graphics g, ImagePanel im) {
|
||||
|
||||
if (this.indexSeq >= 0 && this.indexSeq < this.seq.length) {
|
||||
this.image = this.seq[this.indexSeq] ;
|
||||
|
||||
this.width = this.image.getWidth() ;
|
||||
this.height = this.image.getHeight() ;
|
||||
|
||||
if (this.compteurExposition-- <= 0) {
|
||||
this.compteurExposition = this.dureeExposition ;
|
||||
this.indexSeq ++ ;
|
||||
}
|
||||
|
||||
if (this.indexSeq >= this.seq.length && this.repeat) {
|
||||
this.indexSeq = 0 ;
|
||||
}
|
||||
}
|
||||
|
||||
g.drawImage(this.image, this.x, this.y, im) ;
|
||||
|
||||
}
|
||||
}
|
54
TD1-5/build.sh
Executable file
|
@ -0,0 +1,54 @@
|
|||
#!/bin/bash
|
||||
|
||||
red='\e[0;31m'
|
||||
green='\e[0;32m'
|
||||
orange='\e[0;33m'
|
||||
neutral='\e[0;m'
|
||||
|
||||
printout() {
|
||||
echo -e "[${green}INFO${neutral}] $@"
|
||||
}
|
||||
|
||||
printhelp() {
|
||||
echo -e "[${orange}HELP${neutral}] $@"
|
||||
}
|
||||
|
||||
printerr() {
|
||||
echo -e "[${red}ERROR${neutral}] $@"
|
||||
}
|
||||
|
||||
if test $# -ne 1
|
||||
then
|
||||
printhelp "Veuillez spécifier le nom du programme à lancer."
|
||||
printhelp "Utilisation de Anim par défaut"
|
||||
a="Anim"
|
||||
else
|
||||
a=$1
|
||||
fi
|
||||
|
||||
printout "Compilation avec javac..."
|
||||
javac *.java
|
||||
|
||||
if test $? -eq 0
|
||||
then
|
||||
printout "Succès de la compilation !"
|
||||
else
|
||||
printerr "Echec de la compilation."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
rm build -rf 2>&1 > /dev/null
|
||||
mkdir build
|
||||
mv *.class build/
|
||||
cp -R Images build/
|
||||
|
||||
printout "Lancement du programme..."
|
||||
cd build
|
||||
|
||||
printhelp "Logs : "
|
||||
java $a
|
||||
|
||||
cd - 2>&1 > /dev/null
|
||||
rm build -rf 2>&1 > /dev/null
|
||||
|
||||
exit 0
|
35
TD1-5/notes.md
Normal file
|
@ -0,0 +1,35 @@
|
|||
## Notes de TD - POO java
|
||||
### TD 1
|
||||
* Pour passer votre programme à un collègue, que devez-vous lui envoyer : les fichiers .java, les fichiers .class, les deux ?
|
||||
|
||||
On ne lui envoie que les .class, ça suffit et il est pas question qu'il nous pique notre code.
|
||||
|
||||
* Combien d'objets sont créés explicitement dans la méthode go() ?
|
||||
|
||||
What does "Explicitement" means ? Sinon, si c'est à chaque `new`, bah 6.
|
||||
|
||||
* Comment récupère-t-on la taille d'un tableau ?
|
||||
|
||||
`.length`
|
||||
|
||||
* Localisez le constructeur de la classe Robot. A-t-il autant d'arguments que la classe a d'attributs ?
|
||||
|
||||
Nope.
|
||||
|
||||
* Où est appelé le constructeur de la classe Robot ?
|
||||
|
||||
Anim.java, l. 24 - 27 :
|
||||
```java
|
||||
robots[0] = new Robot("Images/mini1.png", 200, 100, plat, Color.RED) ;
|
||||
```
|
||||
|
||||
* Où est appelée la méthode collision() de la classe Robot ?
|
||||
|
||||
Robot.java > testeCollision > l. 121 - 122 :
|
||||
```java
|
||||
this.collision(autre) ;
|
||||
```
|
||||
|
||||
* 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.
|
||||
|
||||
Il y a l'objet en lui même et son copain.
|
57
TD6/Dossier.java
Normal file
|
@ -0,0 +1,57 @@
|
|||
import java.util.* ;
|
||||
|
||||
public class Dossier extends Node {
|
||||
// Liste des éléments contenus
|
||||
ArrayList<Node> elements = new ArrayList<Node>();
|
||||
|
||||
public Dossier (String path, int depth) {
|
||||
super(path, 0, depth);
|
||||
this.depth = depth;
|
||||
Iterator<String> son;
|
||||
String p;
|
||||
long s = 0;
|
||||
long sum = 0;
|
||||
try {
|
||||
son = FileInfo.getElements(path);
|
||||
}
|
||||
catch (java.io.IOException e) {
|
||||
son = null;
|
||||
}
|
||||
while (son.hasNext()) {
|
||||
p = son.next();
|
||||
try {
|
||||
s = FileInfo.size(p);
|
||||
sum += s;
|
||||
}
|
||||
catch (java.io.IOException e) {}
|
||||
|
||||
if (this.depth + 1 <= maxDepth && nbNode <= maxNode){
|
||||
if (FileInfo.isFile(p)) {
|
||||
elements.add(new Fichier(p, s, this.depth + 1));
|
||||
}
|
||||
else
|
||||
elements.add(new Dossier(p, this.depth + 1));
|
||||
}
|
||||
}
|
||||
Collections.sort(this.elements, comparator);
|
||||
this.size = sum;
|
||||
}
|
||||
|
||||
Comparator<Node> comparator = new Comparator<Node>() {
|
||||
@Override public int compare(Node node1, Node node2) {
|
||||
return node1.name.compareTo(node2.name);
|
||||
}
|
||||
};
|
||||
|
||||
@Override public void afficher() {
|
||||
System.out.println("+ (DIR) " + this.name + "[total = " + size + " octets]");
|
||||
for (Node i : elements) {
|
||||
if (i.depth <= maxDepth){
|
||||
for (int j = 0; j < depth; ++j){
|
||||
System.out.print(" ");
|
||||
}
|
||||
i.afficher();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
9
TD6/Fichier.java
Normal file
|
@ -0,0 +1,9 @@
|
|||
public class Fichier extends Node {
|
||||
public Fichier (String path, long size, int depth) {
|
||||
super(path, size, depth);
|
||||
}
|
||||
|
||||
@Override public void afficher() {
|
||||
System.out.println("| " + this.name + "[total = " + size + " octets]");
|
||||
}
|
||||
}
|
70
TD6/FileInfo.java
Normal file
|
@ -0,0 +1,70 @@
|
|||
import java.io.* ;
|
||||
import java.util.* ;
|
||||
|
||||
public class FileInfo {
|
||||
|
||||
// No constructor
|
||||
private FileInfo() { }
|
||||
|
||||
/**
|
||||
* @param path an absolute path to a file or directory
|
||||
* @return the name of the file or directory
|
||||
*/
|
||||
public static String getName(String path) {
|
||||
return (new File(path)).getName() ;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param path an absolute path to a file
|
||||
* @return the size of the file
|
||||
* @throws java.io.IOException if the file does not exist or if it is a directory.
|
||||
*/
|
||||
public static long size(String path) throws IOException {
|
||||
File file = new File(path) ;
|
||||
if (file.exists() && file.isFile()) return file.length() ;
|
||||
else if (!file.exists ()) throw (new IOException("File " + path + " does not exist.")) ;
|
||||
else throw (new IOException(path + " is not a regular file.")) ;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param path an absolute path to a file or a directory
|
||||
* @return true if it is a directory
|
||||
*/
|
||||
public static boolean isDirectory(String path) {
|
||||
return (new File(path)).isDirectory() ;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param path an absolute path to a file or a directory
|
||||
* @return true if it is a regular file
|
||||
*/
|
||||
public static boolean isFile(String path) {
|
||||
return (new File(path)).isFile() ;
|
||||
}
|
||||
|
||||
/**
|
||||
* @paral path an absolute path to a directory
|
||||
* @return an iterator over the paths of all elements in this directory.
|
||||
* @throws java.io.IOException if it is not a directory.
|
||||
*/
|
||||
public static Iterator<String> getElements(String path) throws IOException {
|
||||
File file = new File(path) ;
|
||||
|
||||
if (!file.exists ()) throw (new IOException("Directory " + path + " does not exist.")) ;
|
||||
if (!file.isDirectory()) throw (new IOException(path + " is not a directory.")) ;
|
||||
|
||||
String[] elements = file.list() ;
|
||||
// If the directory could not be read, elements is null here.
|
||||
if (elements == null) {
|
||||
elements = new String[0] ;
|
||||
}
|
||||
|
||||
for (int i = 0 ; i < elements.length ; i++) {
|
||||
elements[i] = path + File.separator + elements[i] ;
|
||||
}
|
||||
|
||||
// if (elements == null) throw (new IOException("Cannot read directory " + path)) ;
|
||||
|
||||
return Arrays.asList(elements).iterator() ;
|
||||
}
|
||||
}
|
33
TD6/Node.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
public abstract class Node {
|
||||
// Node max
|
||||
static protected int maxNode = 100;
|
||||
static public int nbNode = 0;
|
||||
|
||||
// Profondeur max
|
||||
static protected int maxDepth = 3;
|
||||
|
||||
// Chemin vers l'élément
|
||||
protected String path;
|
||||
|
||||
// Nom de l'élément
|
||||
protected String name;
|
||||
|
||||
// Taille de l'élément
|
||||
protected long size;
|
||||
|
||||
// Profondeur
|
||||
protected int depth;
|
||||
|
||||
|
||||
// Constructeur
|
||||
public Node(String path, long size, int depth) {
|
||||
nbNode++;
|
||||
this.path = path;
|
||||
this.size = size;
|
||||
this.depth = depth;
|
||||
this.name = FileInfo.getName(path);
|
||||
}
|
||||
|
||||
public abstract void afficher();
|
||||
|
||||
}
|
6
TD6/ReadFS.java
Normal file
|
@ -0,0 +1,6 @@
|
|||
public class ReadFS {
|
||||
public static void main (String args[]) {
|
||||
Dossier first = new Dossier(args[0], 0);
|
||||
first.afficher();
|
||||
}
|
||||
}
|