Advertisement
Guest User

labyrinthe

a guest
Mar 24th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.21 KB | None | 0 0
  1. package animation;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics2D;
  5. import java.awt.RenderingHints;
  6. import java.awt.geom.AffineTransform;
  7. import java.awt.geom.Rectangle2D;
  8.  
  9. import composants.ComposantLabyrinthe;
  10. import composants.Debut;
  11. import composants.Fin;
  12. import composants.Mur;
  13. import composants.Trou;
  14. import outils.Vecteur;
  15.  
  16. /**
  17.  * Classe qui dessine le labyrinthe et gère les matrices
  18.  * @author Zakari Rahal
  19.  *
  20.  */
  21.  
  22. public class Labyrinthe implements Dessinable {
  23.     private int[][] matLabyrinthe;
  24.     private ComposantLabyrinthe[][] labyrinthe;
  25.     private ComposantLabyrinthe[][] matCollision;
  26.     private boolean afficherQuadrillage = true;
  27.     private double largeurMonde;
  28.     //Le labyrinthe occupe 80% de l'espace
  29.     private final double CONSTANTE_TAILLE = 1/Math.sqrt(2);
  30.     private double theta = 0;
  31.    
  32.     private Vecteur pointDeDepart;
  33.    
  34.     //État de la sélection des boutons à deux états de l'applcation (mode d'édition)
  35.    
  36.     private int indicePersonnalise;
  37.     private ComposantLabyrinthe composantSouris = null;
  38.    
  39.     /**
  40.      * Constructeur d'un objet labyrinthe
  41.      * @param matLabyrinthe La matrice des entiers du labyrinthe
  42.      * @param largeurMonde La largeur du monde simulé
  43.      */
  44.     public Labyrinthe(int[][] matLabyrinthe, double largeurMonde){
  45.         this.matLabyrinthe = matLabyrinthe;
  46.         this.labyrinthe = new ComposantLabyrinthe[matLabyrinthe.length][matLabyrinthe[0].length];
  47.         this.largeurMonde = largeurMonde;
  48.         attribuerComposants();
  49.     }
  50.    
  51.     /**
  52.      * Méthode qui dessine le labyrinthe et ses composants
  53.      * @param g2d Le contexte graphique du dessin
  54.      * @param mat Le contexte matrice du dessin
  55.      */
  56.     public void dessiner(Graphics2D g2d, AffineTransform mat) {
  57.        
  58.         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
  59.         AffineTransform matLocale = new AffineTransform(mat);
  60.        
  61.         //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)));
  62.        
  63.         //rotation du labyrinthe (désactivée temporairement)
  64.         //matLocale.rotate(theta, largeurMonde/2, largeurMonde/2);
  65.        
  66.         //Dessin du plancher
  67.         g2d.setColor(new Color(211, 211, 211, 200));
  68.         g2d.fill(matLocale.createTransformedShape(new Rectangle2D.Double((largeurMonde*(1-CONSTANTE_TAILLE))/2, (largeurMonde*(1-CONSTANTE_TAILLE))/2, largeurMonde*CONSTANTE_TAILLE, largeurMonde*CONSTANTE_TAILLE)));
  69.        
  70.        
  71.         //Dessin des éléments du labyrinthe
  72.         for(int i = 0; i<matLabyrinthe.length; i++){
  73.             for(int j = 0; j<matLabyrinthe[0].length; j++){
  74.                 if(labyrinthe[i][j] != null){
  75.                     g2d.setColor(labyrinthe[i][j].getCouleurObjet());
  76.                     g2d.fill(matLocale.createTransformedShape(labyrinthe[i][j]));
  77.                 }
  78.                 //Dessin conditionnel du quadrillage
  79.                 if(afficherQuadrillage){
  80.                     g2d.setColor(Color.black);
  81.                     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)));
  82.                 }
  83.             }
  84.         }
  85.         if(composantSouris != null){
  86.             g2d.setColor(composantSouris.getCouleurObjet());
  87.             g2d.fill(matLocale.createTransformedShape(composantSouris));
  88.         }
  89.     }
  90.     /**
  91.      * Méthode qui retourne la largeur du monde
  92.      * @return largeurMonde la largeur du monde
  93.      */
  94.     public double getLargeurMonde(){
  95.         return largeurMonde;
  96.     }
  97.     /**
  98.      * Méthode qui modifie un composant du labyrinthe avec la position d'un nouveau composant
  99.      * @param i La composante i du nouveau composant du labyrinthe
  100.      * @param j La composante j du nouveau composant du labyrinthe
  101.      * @param indice L'indice du nouveau composant du labyrinthe
  102.      */
  103.     public void setComposant(int i, int j, int indice){
  104.         switch (indice){
  105.         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);
  106.         matLabyrinthe[i][j] = Mur.getIndice();
  107.         break;
  108.         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);         
  109.         matLabyrinthe[i][j] = Trou.getIndice();
  110.         break;
  111.         case 3: if(matLabyrinthe[i][j] == 0||matLabyrinthe[i][j] == indice){
  112.             for(int iTemp = 0; iTemp < matLabyrinthe.length; iTemp++){
  113.                 for(int jTemp = 0; jTemp < matLabyrinthe[0].length; jTemp++){
  114.                     if(matLabyrinthe[iTemp][jTemp] == indice){
  115.                         matLabyrinthe[iTemp][jTemp] = 0;
  116.                         labyrinthe[iTemp][jTemp] = null;
  117.                     }
  118.                 }
  119.                 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);
  120.                 pointDeDepart = new Vecteur(j,i);
  121.                 matLabyrinthe[i][j] = Debut.getIndice();
  122.             }
  123.         }
  124.         break;
  125.         case 4: if(matLabyrinthe[i][j] == 0||matLabyrinthe[i][j] == indice){
  126.             for(int iTemp = 0; iTemp < matLabyrinthe.length; iTemp++){
  127.                 for(int jTemp = 0; jTemp < matLabyrinthe[0].length; jTemp++){
  128.                     if(matLabyrinthe[iTemp][jTemp] == indice){
  129.                         matLabyrinthe[iTemp][jTemp] = 0;
  130.                         labyrinthe[iTemp][jTemp] = null;
  131.                     }
  132.                 }
  133.                 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);
  134.                 matLabyrinthe[i][j] = Fin.getIndice();
  135.             }
  136.         }
  137.         break;
  138.         default: if(!(matLabyrinthe[i][j] == 3||matLabyrinthe[i][j] == 4)){
  139.             labyrinthe[i][j] = null;
  140.             matLabyrinthe[i][j] = 0;
  141.         }
  142.         }
  143.     //  matLabyrinthe[i][j] = indice;
  144.     }
  145.    
  146.     /**
  147.      * Méthode qui attribue à la matrices des composants tous ses composants à l'aide de la matrice des entiers du labyrinthe
  148.      */
  149.     private void attribuerComposants() {
  150.         for(int i = 0; i < matLabyrinthe.length; i++){
  151.             for(int j = 0; j < matLabyrinthe[0].length; j++){
  152.                 setComposant(i,j,matLabyrinthe[i][j]);
  153.             }
  154.         }
  155.     }
  156.     /*
  157.     private int[][] ajouterMurs(int[][] mat){
  158.         int[][] matTransfo = new int[mat.length+2][mat[0].length+2];
  159.         for(int i = 0; i<matTransfo.length; i++){
  160.             for(int j = 0; j<matTransfo[0].length; j++){
  161.                 if((i == 0)||(j == 0)||(i == matTransfo.length-1)||(j == matTransfo[0].length-1)){
  162.                     matTransfo[i][j] = 1;
  163.                 }else{
  164.                     matTransfo[i][j] = mat[i-1][j-1];
  165.                 }
  166.             }
  167.         }
  168.         return matTransfo;
  169.     }
  170.     */
  171.     /**
  172.      * Méthode qui imprime dans la console la matrice des entiers actuelle
  173.      * @param mat La matrice des entiers actuelle
  174.      */
  175.     public static void printMatrix(int[][] mat){
  176.         for (int i = 0; i < mat.length; i++) {
  177.             for (int j = 0; j < mat[0].length; j++) {
  178.                 System.out.print(mat[i][j] + " ");
  179.             }
  180.             System.out.println();
  181.         }
  182.     }
  183.     /**
  184.      * Méthode qui retourne la matrice des entiers du labyrinthe
  185.      * @return La matrice des entiers du labyrinthe
  186.      */
  187.     public int[][] getLabyrinthe(){
  188.         return matLabyrinthe;
  189.     }
  190.     /**
  191.      * 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
  192.      * @return La position de départ de la bille
  193.      */
  194.     public Vecteur getPointDeDepart(){
  195.         try{
  196.             return obtenirPoint(pointDeDepart);
  197.         } catch(NullPointerException e){
  198.             return new Vecteur(0,0);
  199.         }
  200.     }
  201.     /**
  202.      * Méthode qui retourne le diamètre de la bille
  203.      * @return Le diamètre de la bille
  204.      */
  205.     public double getDiametreBille(){
  206.         return Math.min(largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length, largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length)/2;
  207.     }
  208.     /**
  209.      * Méthode qui retourne l'indice i de la matrice selon une position du monde réel
  210.      * @param y Une position du monde réel (donnée par le clic de la souris)
  211.      * @return L'indice i de la matrice
  212.      */
  213.     public int getI(double y){
  214.         double i = (y-(largeurMonde*(1-CONSTANTE_TAILLE))/2)/(largeurMonde*CONSTANTE_TAILLE/matLabyrinthe.length);
  215.         return (int)i;
  216.     }
  217.     /**
  218.      * Méthode qui retourne l'indice j de la matrice selon une position du monde réel
  219.      * @param x Une position du monde réel (donnée par le clic de la souris)
  220.      * @return L'indice j de la matrice
  221.      */
  222.     public int getJ(double x){
  223.         double j = (x-(largeurMonde*(1-CONSTANTE_TAILLE))/2)/(largeurMonde*CONSTANTE_TAILLE/matLabyrinthe[0].length);
  224.         return (int)j;
  225.     }
  226.     /**
  227.      * Méthode qui modifie l'indice personnalisé selon le bouton à deux états choisi
  228.      * @param indicePersonnalise L'indice personnalisé choisi par l'utilisateur
  229.      */
  230.     public void setIndicePersonnalise(int indicePersonnalise){
  231.         this.indicePersonnalise = indicePersonnalise;
  232.     }
  233.     /**
  234.      * Méthode qui retourne l'indice personnalisé actuel
  235.      * @return L'indice personnalisé actuel
  236.      */
  237.     public int getIndicePersonnalise(){
  238.         return indicePersonnalise;
  239.     }
  240.     /**
  241.      * Méthode qui retourne vrai si la position passée en paramètre se trouve à l'intérieur du labyrinthe
  242.      * @param x La position x du clic de la souris définie par l'utilisateur
  243.      * @param y La position y du clic de la souris définie par l'utilisateur
  244.      * @return Retourne vrai si la position est à l'intérieur du labyrinthe
  245.      */
  246.     public boolean isInside(double x, double y){
  247.         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)))){
  248.             return true;
  249.         }
  250.         return false;
  251.     }
  252.     /**
  253.      * Méthode qui définit le nouveau composantSouris selon la position du clic de la souris et l'indice personnalisé actuel
  254.      * @param x La position en x du clic de la souris en unités réelles
  255.      * @param y La position en y du clic de la souris en unités réelles
  256.      */
  257.     public void setComposantSouris(double x, double y){
  258.         if(isInside(x,y)){
  259.             switch(indicePersonnalise){
  260.             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);
  261.             break;
  262.             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);
  263.             break;
  264.             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);
  265.             break;
  266.             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);
  267.             break;
  268.             default: composantSouris = null;
  269.             }
  270.         }else{
  271.             composantSouris = null;
  272.         }
  273.     }
  274.     /**
  275.      * Méthode qui retourne le composant de la souris actuel
  276.      * @return Le composant de la souris actuel
  277.      */
  278.     public ComposantLabyrinthe getComposantSouris(){
  279.         return composantSouris;
  280.     }
  281.     /**
  282.      * Méthode qui définit un nouveau theta selon la position de la souris en unités réelles
  283.      * @param x La position en x de la souris en unités réelles
  284.      * @param y La position en y de la souris en unités réelles
  285.      */
  286.     public void setTheta(double x, double y){
  287.         theta = Math.atan(-y/x);
  288.         if(((x<0)&&(y<0))||((x<0)&&(y>0))){
  289.             theta+=Math.PI;
  290.         }else if((x<0)&&(y==0)){
  291.             theta = Math.PI;
  292.         }
  293.         theta += Math.PI/2;
  294.     }
  295.     /**
  296.      * Méthode qui retourne le theta actuel
  297.      * @return Le theta actuel
  298.      */
  299.     public double getTheta(){
  300.         return theta;
  301.     }
  302.    
  303.     public ComposantLabyrinthe[][] getMatCollision(){
  304.         ComposantLabyrinthe composant;
  305.         matCollision = new ComposantLabyrinthe[labyrinthe.length][labyrinthe[0].length];
  306.         for(int i = 0; i < labyrinthe.length; i++){
  307.             for(int j = 0; j < labyrinthe[0].length; j++){
  308.                 composant = labyrinthe[i][j];
  309.                 if(composant != null){
  310.                     if(composant.getIndiceObjet() == Mur.getIndice())
  311.                         matCollision[i][j] = (Mur) composant;
  312.                     else if(composant.getIndiceObjet() == Trou.getIndice())
  313.                         matCollision[i][j] = (Trou) composant;
  314.                     else if(composant.getIndiceObjet() == Fin.getIndice())
  315.                         matCollision[i][j] = (Fin) composant;
  316.                 }
  317.             }
  318.         }
  319.         return matCollision;
  320.     }
  321.    
  322.     public void setDimensionsLabyrinthe(int i, int j){
  323.         int[][] matLabyrintheTemp = new int[i][j];
  324.         ComposantLabyrinthe[][] labyrintheTemp = new ComposantLabyrinthe[i][j];
  325.         for(int iTemp = 0; iTemp < i; iTemp++){
  326.             for(int jTemp = 0; jTemp < j; jTemp++){
  327.                 try {
  328.                     matLabyrintheTemp[iTemp][jTemp] = matLabyrinthe[iTemp][jTemp];
  329.                 } catch (Exception e) {
  330.                     matLabyrintheTemp[iTemp][jTemp] = 0;
  331.                     labyrintheTemp[iTemp][jTemp] = null;
  332.                 }
  333.             }
  334.         }
  335.         matLabyrinthe = matLabyrintheTemp;
  336.         labyrinthe = labyrintheTemp;
  337.         attribuerComposants();
  338.     }
  339.     public Vecteur obtenirPoint(Vecteur point){
  340.         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);
  341.     }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement