Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package animation;
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.geom.AffineTransform;
- import java.awt.geom.Rectangle2D;
- import composants.ComposantLabyrinthe;
- import composants.Debut;
- import composants.Fin;
- import composants.Mur;
- import composants.Trou;
- import outils.Vecteur;
- /**
- * Classe qui dessine le labyrinthe et gère les matrices
- * @author Zakari Rahal
- *
- */
- public class Labyrinthe implements Dessinable {
- private int[][] matLabyrinthe;
- private ComposantLabyrinthe[][] labyrinthe;
- private ComposantLabyrinthe[][] matCollision;
- private boolean afficherQuadrillage = true;
- private double largeurMonde;
- //Le labyrinthe occupe 80% de l'espace
- private final double CONSTANTE_TAILLE = 1/Math.sqrt(2);
- private double theta = 0;
- private Vecteur pointDeDepart;
- //État de la sélection des boutons à deux états de l'applcation (mode d'édition)
- private int indicePersonnalise;
- private ComposantLabyrinthe composantSouris = null;
- /**
- * Constructeur d'un objet labyrinthe
- * @param matLabyrinthe La matrice des entiers du labyrinthe
- * @param largeurMonde La largeur du monde simulé
- */
- public Labyrinthe(int[][] matLabyrinthe, double largeurMonde){
- this.matLabyrinthe = matLabyrinthe;
- this.labyrinthe = new ComposantLabyrinthe[matLabyrinthe.length][matLabyrinthe[0].length];
- this.largeurMonde = largeurMonde;
- attribuerComposants();
- }
- /**
- * Méthode qui dessine le labyrinthe et ses composants
- * @param g2d Le contexte graphique du dessin
- * @param mat Le contexte matrice du dessin
- */
- public void dessiner(Graphics2D g2d, AffineTransform mat) {
- g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
- AffineTransform matLocale = new AffineTransform(mat);
- //g2d.draw(matLocale.createTransformedShape(new Rectangle2D.Double((largeurMonde*(1-CONSTANTE_TAILLE))/2+(i*largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length), (largeurMonde*(1-CONSTANTE_TAILLE))/2+(j*largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length), largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length, largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length)));
- //rotation du labyrinthe (désactivée temporairement)
- //matLocale.rotate(theta, largeurMonde/2, largeurMonde/2);
- //Dessin du plancher
- g2d.setColor(new Color(211, 211, 211, 200));
- g2d.fill(matLocale.createTransformedShape(new Rectangle2D.Double((largeurMonde*(1-CONSTANTE_TAILLE))/2, (largeurMonde*(1-CONSTANTE_TAILLE))/2, largeurMonde*CONSTANTE_TAILLE, largeurMonde*CONSTANTE_TAILLE)));
- //Dessin des éléments du labyrinthe
- for(int i = 0; i<matLabyrinthe.length; i++){
- for(int j = 0; j<matLabyrinthe[0].length; j++){
- if(labyrinthe[i][j] != null){
- g2d.setColor(labyrinthe[i][j].getCouleurObjet());
- g2d.fill(matLocale.createTransformedShape(labyrinthe[i][j]));
- }
- //Dessin conditionnel du quadrillage
- if(afficherQuadrillage){
- g2d.setColor(Color.black);
- g2d.draw(matLocale.createTransformedShape(new Rectangle2D.Double((largeurMonde*(1-CONSTANTE_TAILLE))/2+(j*largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length), (largeurMonde*(1-CONSTANTE_TAILLE))/2+(i*largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length), largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length, largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length)));
- }
- }
- }
- if(composantSouris != null){
- g2d.setColor(composantSouris.getCouleurObjet());
- g2d.fill(matLocale.createTransformedShape(composantSouris));
- }
- }
- /**
- * Méthode qui retourne la largeur du monde
- * @return largeurMonde la largeur du monde
- */
- public double getLargeurMonde(){
- return largeurMonde;
- }
- /**
- * Méthode qui modifie un composant du labyrinthe avec la position d'un nouveau composant
- * @param i La composante i du nouveau composant du labyrinthe
- * @param j La composante j du nouveau composant du labyrinthe
- * @param indice L'indice du nouveau composant du labyrinthe
- */
- public void setComposant(int i, int j, int indice){
- switch (indice){
- case 1: labyrinthe[i][j] = new Mur((largeurMonde*(1-CONSTANTE_TAILLE))/2+(j*largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length), (largeurMonde*(1-CONSTANTE_TAILLE))/2+(i*largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length), largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length, largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length);
- matLabyrinthe[i][j] = Mur.getIndice();
- break;
- case 2: labyrinthe[i][j] = new Trou((largeurMonde*(1-CONSTANTE_TAILLE))/2+(j*largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length), (largeurMonde*(1-CONSTANTE_TAILLE))/2+(i*largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length), largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length, largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length);
- matLabyrinthe[i][j] = Trou.getIndice();
- break;
- case 3: if(matLabyrinthe[i][j] == 0||matLabyrinthe[i][j] == indice){
- for(int iTemp = 0; iTemp < matLabyrinthe.length; iTemp++){
- for(int jTemp = 0; jTemp < matLabyrinthe[0].length; jTemp++){
- if(matLabyrinthe[iTemp][jTemp] == indice){
- matLabyrinthe[iTemp][jTemp] = 0;
- labyrinthe[iTemp][jTemp] = null;
- }
- }
- labyrinthe[i][j] = new Debut((largeurMonde*(1-CONSTANTE_TAILLE))/2+(j*largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length), (largeurMonde*(1-CONSTANTE_TAILLE))/2+(i*largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length), largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length, largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length);
- pointDeDepart = new Vecteur(j,i);
- matLabyrinthe[i][j] = Debut.getIndice();
- }
- }
- break;
- case 4: if(matLabyrinthe[i][j] == 0||matLabyrinthe[i][j] == indice){
- for(int iTemp = 0; iTemp < matLabyrinthe.length; iTemp++){
- for(int jTemp = 0; jTemp < matLabyrinthe[0].length; jTemp++){
- if(matLabyrinthe[iTemp][jTemp] == indice){
- matLabyrinthe[iTemp][jTemp] = 0;
- labyrinthe[iTemp][jTemp] = null;
- }
- }
- labyrinthe[i][j] = new Fin((largeurMonde*(1-CONSTANTE_TAILLE))/2+(j*largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length), (largeurMonde*(1-CONSTANTE_TAILLE))/2+(i*largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length), largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length, largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length);
- matLabyrinthe[i][j] = Fin.getIndice();
- }
- }
- break;
- default: if(!(matLabyrinthe[i][j] == 3||matLabyrinthe[i][j] == 4)){
- labyrinthe[i][j] = null;
- matLabyrinthe[i][j] = 0;
- }
- }
- // matLabyrinthe[i][j] = indice;
- }
- /**
- * Méthode qui attribue à la matrices des composants tous ses composants à l'aide de la matrice des entiers du labyrinthe
- */
- private void attribuerComposants() {
- for(int i = 0; i < matLabyrinthe.length; i++){
- for(int j = 0; j < matLabyrinthe[0].length; j++){
- setComposant(i,j,matLabyrinthe[i][j]);
- }
- }
- }
- /*
- private int[][] ajouterMurs(int[][] mat){
- int[][] matTransfo = new int[mat.length+2][mat[0].length+2];
- for(int i = 0; i<matTransfo.length; i++){
- for(int j = 0; j<matTransfo[0].length; j++){
- if((i == 0)||(j == 0)||(i == matTransfo.length-1)||(j == matTransfo[0].length-1)){
- matTransfo[i][j] = 1;
- }else{
- matTransfo[i][j] = mat[i-1][j-1];
- }
- }
- }
- return matTransfo;
- }
- */
- /**
- * Méthode qui imprime dans la console la matrice des entiers actuelle
- * @param mat La matrice des entiers actuelle
- */
- public static void printMatrix(int[][] mat){
- for (int i = 0; i < mat.length; i++) {
- for (int j = 0; j < mat[0].length; j++) {
- System.out.print(mat[i][j] + " ");
- }
- System.out.println();
- }
- }
- /**
- * Méthode qui retourne la matrice des entiers du labyrinthe
- * @return La matrice des entiers du labyrinthe
- */
- public int[][] getLabyrinthe(){
- return matLabyrinthe;
- }
- /**
- * Méthode qui retourne la position de départ de la bille selon la position du point de départ et de la taille de la cellule
- * @return La position de départ de la bille
- */
- public Vecteur getPointDeDepart(){
- try{
- return obtenirPoint(pointDeDepart);
- } catch(NullPointerException e){
- return new Vecteur(0,0);
- }
- }
- /**
- * Méthode qui retourne le diamètre de la bille
- * @return Le diamètre de la bille
- */
- public double getDiametreBille(){
- return Math.min(largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length, largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length)/2;
- }
- /**
- * Méthode qui retourne l'indice i de la matrice selon une position du monde réel
- * @param y Une position du monde réel (donnée par le clic de la souris)
- * @return L'indice i de la matrice
- */
- public int getI(double y){
- double i = (y-(largeurMonde*(1-CONSTANTE_TAILLE))/2)/(largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length);
- return (int)i;
- }
- /**
- * Méthode qui retourne l'indice j de la matrice selon une position du monde réel
- * @param x Une position du monde réel (donnée par le clic de la souris)
- * @return L'indice j de la matrice
- */
- public int getJ(double x){
- double j = (x-(largeurMonde*(1-CONSTANTE_TAILLE))/2)/(largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length);
- return (int)j;
- }
- /**
- * Méthode qui modifie l'indice personnalisé selon le bouton à deux états choisi
- * @param indicePersonnalise L'indice personnalisé choisi par l'utilisateur
- */
- public void setIndicePersonnalise(int indicePersonnalise){
- this.indicePersonnalise = indicePersonnalise;
- }
- /**
- * Méthode qui retourne l'indice personnalisé actuel
- * @return L'indice personnalisé actuel
- */
- public int getIndicePersonnalise(){
- return indicePersonnalise;
- }
- /**
- * Méthode qui retourne vrai si la position passée en paramètre se trouve à l'intérieur du labyrinthe
- * @param x La position x du clic de la souris définie par l'utilisateur
- * @param y La position y du clic de la souris définie par l'utilisateur
- * @return Retourne vrai si la position est à l'intérieur du labyrinthe
- */
- public boolean isInside(double x, double y){
- if((x>=(largeurMonde*(1-CONSTANTE_TAILLE))/2)&&(x<=(largeurMonde*(1-CONSTANTE_TAILLE))/2+((largeurMonde*CONSTANTE_TAILLE)))&&(y>=(largeurMonde*(1-CONSTANTE_TAILLE))/2)&&(y<=(largeurMonde*(1-CONSTANTE_TAILLE))/2+((largeurMonde*CONSTANTE_TAILLE)))){
- return true;
- }
- return false;
- }
- /**
- * Méthode qui définit le nouveau composantSouris selon la position du clic de la souris et l'indice personnalisé actuel
- * @param x La position en x du clic de la souris en unités réelles
- * @param y La position en y du clic de la souris en unités réelles
- */
- public void setComposantSouris(double x, double y){
- if(isInside(x,y)){
- switch(indicePersonnalise){
- case 1: composantSouris = new Mur(x-(largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length)/2,y-(largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length)/2,largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length, largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length);
- break;
- case 2: composantSouris = new Trou(x-(largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length)/2,y-(largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length)/2,largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length, largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length);
- break;
- case 3: composantSouris = new Debut(x-(largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length)/2,y-(largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length)/2,largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length, largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length);
- break;
- case 4: composantSouris = new Fin(x-(largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length)/2,y-(largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length)/2,largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length, largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length);
- break;
- default: composantSouris = null;
- }
- }else{
- composantSouris = null;
- }
- }
- /**
- * Méthode qui retourne le composant de la souris actuel
- * @return Le composant de la souris actuel
- */
- public ComposantLabyrinthe getComposantSouris(){
- return composantSouris;
- }
- /**
- * Méthode qui définit un nouveau theta selon la position de la souris en unités réelles
- * @param x La position en x de la souris en unités réelles
- * @param y La position en y de la souris en unités réelles
- */
- public void setTheta(double x, double y){
- theta = Math.atan(-y/x);
- if(((x<0)&&(y<0))||((x<0)&&(y>0))){
- theta+=Math.PI;
- }else if((x<0)&&(y==0)){
- theta = Math.PI;
- }
- theta += Math.PI/2;
- }
- /**
- * Méthode qui retourne le theta actuel
- * @return Le theta actuel
- */
- public double getTheta(){
- return theta;
- }
- public ComposantLabyrinthe[][] getMatCollision(){
- ComposantLabyrinthe composant;
- matCollision = new ComposantLabyrinthe[labyrinthe.length][labyrinthe[0].length];
- for(int i = 0; i < labyrinthe.length; i++){
- for(int j = 0; j < labyrinthe[0].length; j++){
- composant = labyrinthe[i][j];
- if(composant != null){
- if(composant.getIndiceObjet() == Mur.getIndice())
- matCollision[i][j] = (Mur) composant;
- else if(composant.getIndiceObjet() == Trou.getIndice())
- matCollision[i][j] = (Trou) composant;
- else if(composant.getIndiceObjet() == Fin.getIndice())
- matCollision[i][j] = (Fin) composant;
- }
- }
- }
- return matCollision;
- }
- public void setDimensionsLabyrinthe(int i, int j){
- int[][] matLabyrintheTemp = new int[i][j];
- ComposantLabyrinthe[][] labyrintheTemp = new ComposantLabyrinthe[i][j];
- for(int iTemp = 0; iTemp < i; iTemp++){
- for(int jTemp = 0; jTemp < j; jTemp++){
- try {
- matLabyrintheTemp[iTemp][jTemp] = matLabyrinthe[iTemp][jTemp];
- } catch (Exception e) {
- matLabyrintheTemp[iTemp][jTemp] = 0;
- labyrintheTemp[iTemp][jTemp] = null;
- }
- }
- }
- matLabyrinthe = matLabyrintheTemp;
- labyrinthe = labyrintheTemp;
- attribuerComposants();
- }
- public Vecteur obtenirPoint(Vecteur point){
- return new Vecteur((largeurMonde*(1-CONSTANTE_TAILLE))/2+(point.getX()*largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length)+(largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length)/4, (largeurMonde*(1-CONSTANTE_TAILLE))/2+(point.getY()*largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length)+(largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length)/4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement