Partage
  • Partager sur Facebook
  • Partager sur Twitter

Projet en java

    26 mars 2009 à 10:42:57

    Bonjour,
    J'espere poster au bon endroit je savais pas trop ou mettre mon message.

    Je suis entrain de me faire un projet en java, il consiste a crée une interface avec avec des bouton (avancer, reculer, tourner......) avec des zone de texte (avancer de 5), et une interface graphique qui représente "une tortue" (bon en fait c juste un cercle ) qui réagit au commande qu'on lui donne.

    Tous cela marche correctement mais j'aimerais rendre cela plus "jolie", en effet transformer mon cercle (représentant ma tortue) en vrai toture ou en un truc plus ressemblant et par exemple rajouter une feuille de salade et lorsque la tortue ateint la salade un message s'affiche 'MIAM' .
    Enfin voila rendre tout sa plus jolie sans modifier mon code.

    Est ce qu'il est possible de contacter des personne qui gère bien java par msn pour leur fournir mon code et qu'on en discute?

    Merci bcp et désolé si j'ai pas posté au bonne endroit (j'ai pas lhabitude des forum c la premiere fois)
    • Partager sur Facebook
    • Partager sur Twitter
      26 mars 2009 à 10:46:42

      Salut,

      si tu veux discuter, tu es ici au bon endroit. Pas besoin de msn et en plus ca pourra servir à d'autres personnes :)
      • Partager sur Facebook
      • Partager sur Twitter
        26 mars 2009 à 10:50:31

        Tu es au bon endroit !
        Pour ton interface, je pense que tu peux dans un premier temps remplacé ton cercle par une image que tu place, déplace, tourne (c'est déjà un très bon exercice).
        Puis rajouter des images pour la salade, qui disparaissent quand la tortue la mange.
        Après pour le message par exemple tu peux avoir plusieurs solutions :
        afficher à un endroit dans ta fenetre
        mettre une pop-up
        l'afficher au niveau de la tête de ta tortue

        Après je suis pas un expert en manipulation d'images, mais j'ai déjà vu des sujet parlant de rotation d'image, etc etc

        Je laisse les autres te proposer de meilleurs solutions que ma solution basique :)

        PS : et tu peux même afficher un gif à la place d'un image fixe pour plus de réalisme !!! lol
        • Partager sur Facebook
        • Partager sur Twitter
          26 mars 2009 à 11:10:30

          Euh oui la rotation d'images me semble fort compliquée pour ce qu'il cherche à faire.
          En 2D il est beaucoup plus courant d'utiliser des sprites pour les animations graphiques.
          Si tu veux rester dans le basique, crée juste quatre images de la tortue correspondant chacune à une position (si elle regarde vers le haut, vers la gauche, etc). Comme ça au clic sur un bouton de direction, t'as juste à charger l'image correspondante.

          Tu peux poster ton code en entier ici. Le forum est bien pour discuter mais on attendra de toi (en général...) que tu te creuses un peu les méninges ;)

          Enfin bon si j'ai bien compris tout ce que tu veux c'est charger une image en mémoire et la dessiner lors d'un clic. Là tout dépend comment ton code est organisé (je parie que tu n'as pas créé de classe Tortue par exemple non ?), mais globalement t'as juste à peindre l'image sur ton composant à l'aide de paintComponent (et même pas besoin de thread dédié).
          • Partager sur Facebook
          • Partager sur Twitter
            26 mars 2009 à 11:51:50

            MERCI beaucoup de m'avoir répondu aussi vite, vous avez compris mes problème ;).
            Comment je peux faire pour vous montrez toutes mes classe? (si jai une classe Tortue :D)
            • Partager sur Facebook
            • Partager sur Twitter
              26 mars 2009 à 12:27:00

              Alors je parie que ta classe Tortue extends JPanel :p

              Plus sérieusement, ben t'as juste à faire copier coller de ton code sur ce forum. Et ce qui est super c'est que les créateurs du site ont mis un système de balises, et si tu places ton code entre ces balises, c'est super agréable à lire ;)

              Exemple balise <'code type="java">Ici tu mets ton code</code> (faut retirer le guillement simple que j'ai mis au début)
              Et ça donne :
              class Tortue {
              
                 private int nombreDeCarapaces = 2;
              
                 public void rentrerDansMaCarapace()
                 {
                    System.out.println("Bouh j'ai peur !!!");
                 }
              
              }
              
              • Partager sur Facebook
              • Partager sur Twitter
                26 mars 2009 à 18:19:04

                oui mai mon code comporte au moi 20 classes ba donc jvai vos donnez les plus importe donc ok? (une dizaine quoi lol)
                • Partager sur Facebook
                • Partager sur Twitter
                  26 mars 2009 à 18:40:42

                  Euh ok pas besoin de toutes les classes effectivement, juste celles qui s'occupent d'afficher la fenêtre, et le composant dans lequel la tortue est dessinée.
                  • Partager sur Facebook
                  • Partager sur Twitter
                    27 mars 2009 à 2:18:12

                    Citation : elmh

                    Euh oui la rotation d'images me semble fort compliquée pour ce qu'il cherche à faire.


                    Tu exagères un peu :)

                    La rotation en elle-même prend deux lignes (je me suis servi d'un Graphics2D.translate() suivi du Graphics2D.rotate()). Il faut juste un petit calcul pour les options avancer et reculer et hop' c'est plié. A part un petit pré-requis trigonométrique je ne vois pas de forte difficulté.

                    Je pense donc que c'est un objectif tout à fait accessible.
                    • Partager sur Facebook
                    • Partager sur Twitter
                      27 mars 2009 à 5:06:43

                      Oui enfin rotate ne fait que tourner l'image d'origine. Selon la vue qu'il propose ça n'aura peut-être pas l'effet désiré. Pour la vue de dessus effectivement ça marche niquel.
                      Mais il est vrai que j'ai largement exagéré en disant "fort compliquée" ^^'

                      Mais de toute manière s'il a réussi à faire son code tout seul, il n'aura aucune difficulté à charger une image selon l'action en cours ^^'
                      • Partager sur Facebook
                      • Partager sur Twitter
                        2 avril 2009 à 9:27:13

                        <'code type="java">
                        package upmc.li260.robot.logo.langage;

                        import java.util.ArrayList;
                        import java.util.logging.Level;
                        import java.util.logging.Logger;

                        public class Tortue extends Model implements IEffecteur{
                        private Position p;
                        private double angle;
                        private boolean stylo; // false=lever, true=baisser
                        private int rayon;
                        private ArrayList<Double> x, y;

                        /** Creates a new instance of Tortue */
                        public Tortue() {
                        this.p=new Position();
                        this.angle = 0;
                        this.stylo = false;
                        this.rayon = 20;
                        this.x = new ArrayList<Double>();
                        this.y = new ArrayList<Double>();
                        }

                        public int getRayon(){
                        return this.rayon;
                        }

                        public Position getPosition(){
                        return this.p;
                        }

                        public double getAngle(){
                        return this.angle;
                        }

                        public ArrayList<Double> getX(){
                        return this.x;
                        }

                        public ArrayList<Double> getY(){
                        return this.y;
                        }

                        public boolean getStylo(){
                        return this.stylo;
                        }

                        public int execute(Commande c)
                        {

                        double i, j, ancienX, nouveauX, ancienY, nouveauY;

                        if (c instanceof Avancer)
                        {
                        /*
                        if (this.stylo){
                        x.add(p.getX());
                        y.add(p.getY());
                        }
                        p.setX( p.getX() + (c.getArgs().get(0).getA()) * Math.cos(Math.toRadians(angle)) );
                        p.setY( p.getY() + (c.getArgs().get(0).getA()) * Math.sin(Math.toRadians(angle)) );
                        if (this.stylo){
                        x.add(p.getX());
                        y.add(p.getY());
                        }
                        */
                        ancienX = p.getX();
                        ancienY = p.getY();
                        nouveauX = p.getX() + (double)(c.getArgs().get(0).getA()) * Math.cos(Math.toRadians(angle));
                        nouveauY = p.getY() + (double)(c.getArgs().get(0).getA()) * Math.sin(Math.toRadians(angle));

                        if (this.stylo){
                        x.add(ancienX);
                        y.add(ancienY);
                        x.add(nouveauX);
                        y.add(nouveauY);
                        }

                        if (Math.abs((ancienX-nouveauX)/(ancienY-nouveauY)) >= 1)
                        {
                        if (ancienX - nouveauX <= 0)
                        {
                        for (i=ancienX;i<=nouveauX;i++)
                        {
                        if (this.stylo){
                        x.remove(x.size()-1);
                        y.remove(y.size()-1);
                        }
                        nouveauY = Math.tan(Math.toRadians(angle)) + ancienY;
                        p.setX(i);
                        p.setY(nouveauY);
                        ancienY = nouveauY;
                        if (this.stylo){
                        x.add(i);
                        y.add(nouveauY);
                        }
                        this.changed();
                        try {
                        Thread.sleep((long) (1000 / Math.abs(nouveauX-ancienX)));
                        } catch (InterruptedException ex) {
                        Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        }
                        } else {
                        for (i=ancienX;i>=nouveauX;i--)
                        {
                        if (this.stylo){
                        x.remove(x.size()-1);
                        y.remove(y.size()-1);
                        }
                        nouveauY = -Math.tan(Math.toRadians(angle)) + ancienY;
                        p.setX(i);
                        p.setY(nouveauY);
                        ancienY = nouveauY;
                        if (this.stylo){
                        x.add(i);
                        y.add(nouveauY);
                        }
                        this.changed();
                        try {
                        Thread.sleep((long) (1000 / Math.abs(nouveauX-ancienX)));
                        } catch (InterruptedException ex) {
                        Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        }
                        }
                        } else {
                        if (ancienY - nouveauY <= 0)
                        {
                        for (i=ancienY;i<=nouveauY;i++)
                        {
                        if (this.stylo){
                        x.remove(x.size()-1);
                        y.remove(y.size()-1);
                        }
                        nouveauX = Math.cos(Math.toRadians(angle))/Math.sin(Math.toRadians(angle)) + ancienX;
                        p.setY(i);
                        p.setX(nouveauX);
                        ancienX = nouveauX;
                        if (this.stylo){
                        y.add(i);
                        x.add(nouveauX);
                        }
                        this.changed();
                        try {
                        Thread.sleep((long) (1000 / Math.abs(nouveauY-ancienY)));
                        } catch (InterruptedException ex) {
                        Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        }
                        } else {
                        for (i=ancienY;i>=nouveauY;i--)
                        {
                        if (this.stylo){
                        x.remove(x.size()-1);
                        y.remove(y.size()-1);
                        }
                        nouveauX = -Math.cos(Math.toRadians(angle))/Math.sin(Math.toRadians(angle)) + ancienX;
                        p.setY(i);
                        p.setX(nouveauX);
                        ancienX = nouveauX;
                        if (this.stylo){
                        y.add(i);
                        x.add(nouveauX);
                        }
                        this.changed();
                        try {
                        Thread.sleep((long) (1000 / Math.abs(nouveauY-ancienY)));
                        } catch (InterruptedException ex) {
                        Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        }
                        }
                        }


                        }

                        if (c instanceof Reculer)
                        {
                        /*
                        if (this.stylo){
                        x.add(p.getX());
                        y.add(p.getY());
                        }
                        p.setX( p.getX() - (c.getArgs().get(0).getA()) * Math.cos(Math.toRadians(angle)) );
                        p.setY( p.getY() - (c.getArgs().get(0).getA()) * Math.sin(Math.toRadians(angle)) );
                        if (this.stylo){
                        x.add(p.getX());
                        y.add(p.getY());
                        }
                        */
                        ancienX = p.getX();
                        ancienY = p.getY();
                        nouveauX = p.getX() - (double)(c.getArgs().get(0).getA()) * Math.cos(Math.toRadians(angle));
                        nouveauY = p.getY() - (double)(c.getArgs().get(0).getA()) * Math.sin(Math.toRadians(angle));

                        if (this.stylo){
                        x.add(ancienX);
                        y.add(ancienY);
                        x.add(nouveauX);
                        y.add(nouveauY);
                        }

                        if (Math.abs((ancienX-nouveauX)/(ancienY-nouveauY)) >= 1)
                        {
                        if (ancienX - nouveauX <= 0)
                        {
                        for (i=ancienX;i<=nouveauX;i++)
                        {
                        if (this.stylo){
                        x.remove(x.size()-1);
                        y.remove(y.size()-1);
                        }
                        nouveauY = +Math.tan(Math.toRadians(angle)) + ancienY;
                        p.setX(i);
                        p.setY(nouveauY);
                        ancienY = nouveauY;
                        if (this.stylo){
                        x.add(i);
                        y.add(nouveauY);
                        }
                        this.changed();
                        try {
                        Thread.sleep((long) (1000 / Math.abs(nouveauX-ancienX)));
                        } catch (InterruptedException ex) {
                        Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        }
                        } else {
                        for (i=ancienX;i>=nouveauX;i--)
                        {
                        if (this.stylo){
                        x.remove(x.size()-1);
                        y.remove(y.size()-1);
                        }
                        nouveauY = -Math.tan(Math.toRadians(angle)) + ancienY;
                        p.setX(i);
                        p.setY(nouveauY);
                        ancienY = nouveauY;
                        if (this.stylo){
                        x.add(i);
                        y.add(nouveauY);
                        }
                        this.changed();
                        try {
                        Thread.sleep((long) (1000 / Math.abs(nouveauX-ancienX)));
                        } catch (InterruptedException ex) {
                        Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        }
                        }
                        } else {
                        if (ancienY - nouveauY <= 0)
                        {
                        for (i=ancienY;i<=nouveauY;i++)
                        {
                        if (this.stylo){
                        x.remove(x.size()-1);
                        y.remove(y.size()-1);
                        }
                        nouveauX = +Math.cos(Math.toRadians(angle))/Math.sin(Math.toRadians(angle)) + ancienX;
                        p.setY(i);
                        p.setX(nouveauX);
                        ancienX = nouveauX;
                        if (this.stylo){
                        y.add(i);
                        x.add(nouveauX);
                        }
                        this.changed();
                        try {
                        Thread.sleep((long) (1000 / Math.abs(nouveauY-ancienY)));
                        } catch (InterruptedException ex) {
                        Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        }
                        } else {
                        for (i=ancienY;i>=nouveauY;i--)
                        {
                        if (this.stylo){
                        x.remove(x.size()-1);
                        y.remove(y.size()-1);
                        }
                        nouveauX = -Math.cos(Math.toRadians(angle))/Math.sin(Math.toRadians(angle)) + ancienX;
                        p.setY(i);
                        p.setX(nouveauX);
                        ancienX = nouveauX;
                        if (this.stylo){
                        y.add(i);
                        x.add(nouveauX);
                        }
                        this.changed();
                        try {
                        Thread.sleep((long) (1000 / Math.abs(nouveauY-ancienY)));
                        } catch (InterruptedException ex) {
                        Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        }
                        }
                        }
                        }

                        if (c instanceof TournerGauche)
                        {
                        this.angle -= (c.getArgs().get(0).getA()) % 360;
                        }

                        if (c instanceof TournerDroite)
                        {
                        this.angle += (c.getArgs().get(0).getA()) % 360;
                        }

                        if (c instanceof Baisser)
                        {
                        this.stylo = true;
                        }

                        if (c instanceof Lever)
                        {
                        this.stylo = false;
                        }

                        this.changed();

                        return 0;
                        }

                        public void reinitialiserLigne() {
                        this.x.clear();
                        this.y.clear();
                        this.changed();
                        }

                        public String toString()
                        {
                        String s = "";
                        if (stylo == false)
                        {
                        s = "lever";
                        }
                        else
                        {
                        s = "baisser";
                        }
                        return "position : " + this.p.toString() + ", angle : " + this.angle + ", stylo : " + s;
                        }

                        }

                        </code>
                        • Partager sur Facebook
                        • Partager sur Twitter
                          2 avril 2009 à 10:43:38

                          Citation : elmh

                          Exemple balise <'code type="java">Ici tu mets ton code</code> (faut retirer le guillement simple que j'ai mis au début)

                          • Partager sur Facebook
                          • Partager sur Twitter
                            3 avril 2009 à 20:14:41

                            Je cite pour corriger. ;)

                            Citation : diabloic

                            package upmc.li260.robot.logo.langage;
                            
                            import java.util.ArrayList;
                            import java.util.logging.Level;
                            import java.util.logging.Logger;
                            
                            public class Tortue extends Model implements IEffecteur{
                                private Position p;
                                private double angle;
                                private boolean stylo; // false=lever, true=baisser 
                                private int rayon;
                                private ArrayList<Double> x, y;
                                
                                /** Creates a new instance of Tortue */
                                public Tortue() {
                                    this.p=new Position();
                                    this.angle = 0;
                                    this.stylo = false;
                                    this.rayon = 20;
                                    this.x = new ArrayList<Double>();
                                    this.y = new ArrayList<Double>();
                                }
                                
                                public int getRayon(){
                                     return this.rayon;
                                }
                            
                                public Position getPosition(){
                                     return this.p;
                                }
                                
                                public double getAngle(){
                                     return this.angle;
                                }
                                
                                public ArrayList<Double> getX(){
                                    return this.x;
                                }
                                
                                public ArrayList<Double> getY(){
                                    return this.y;
                                }
                                
                                public boolean getStylo(){
                                    return this.stylo;
                                }
                                
                                public int execute(Commande c)
                                {
                                	
                                	double i, j, ancienX, nouveauX, ancienY, nouveauY;
                                	
                                    if (c instanceof Avancer)
                                    {
                                        /*
                                       if (this.stylo){
                                           x.add(p.getX());
                                           y.add(p.getY());
                                       }
                                       p.setX( p.getX() + (c.getArgs().get(0).getA()) * Math.cos(Math.toRadians(angle)) );
                                       p.setY( p.getY() + (c.getArgs().get(0).getA()) * Math.sin(Math.toRadians(angle)) );
                                       if (this.stylo){
                                           x.add(p.getX());
                                           y.add(p.getY());
                                       }
                                       */
                                    	ancienX = p.getX();
                                    	ancienY = p.getY();
                                    	nouveauX = p.getX() +  (double)(c.getArgs().get(0).getA()) * Math.cos(Math.toRadians(angle));
                                    	nouveauY = p.getY() +  (double)(c.getArgs().get(0).getA()) * Math.sin(Math.toRadians(angle));
                            	        
                                        if (this.stylo){
                                       	   x.add(ancienX);
                                           y.add(ancienY);
                                       	   x.add(nouveauX);
                                           y.add(nouveauY);
                                        }
                                    	
                                    	if (Math.abs((ancienX-nouveauX)/(ancienY-nouveauY)) >= 1)
                                    	{
                                    		if (ancienX - nouveauX <= 0)
                            	        	{
                            		        	for (i=ancienX;i<=nouveauX;i++)
                            		        	{
                            		                if (this.stylo){
                            		            	   x.remove(x.size()-1);
                            		            	   y.remove(y.size()-1);
                            		                }
                            		        		nouveauY = Math.tan(Math.toRadians(angle)) + ancienY;
                            		        		p.setX(i);
                            		        		p.setY(nouveauY);
                            		        		ancienY = nouveauY;
                            		                if (this.stylo){
                            		            	   x.add(i);
                            		            	   y.add(nouveauY);
                            		                }
                            		        		this.changed();
                                                    try {
                                                        Thread.sleep((long) (1000 / Math.abs(nouveauX-ancienX)));
                                                    } catch (InterruptedException ex) {
                                                        Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                                                    }
                            		        	}
                            	        	} else {
                            		        	for (i=ancienX;i>=nouveauX;i--)
                            		        	{
                            		                if (this.stylo){
                            		            	   x.remove(x.size()-1);
                            		            	   y.remove(y.size()-1);
                            		                }
                            		        		nouveauY = -Math.tan(Math.toRadians(angle)) + ancienY;
                            		        		p.setX(i);
                            		        		p.setY(nouveauY);
                            		        		ancienY = nouveauY;
                            		                if (this.stylo){
                            		            	   x.add(i);
                            		            	   y.add(nouveauY);
                            		                }
                            		        		this.changed();
                                                    try {
                                                        Thread.sleep((long) (1000 / Math.abs(nouveauX-ancienX)));
                                                    } catch (InterruptedException ex) {
                                                        Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                                                    }
                            		        	}
                            	        	}
                                    	} else {
                                    		if (ancienY - nouveauY <= 0)
                            	        	{
                            		        	for (i=ancienY;i<=nouveauY;i++)
                            		        	{
                            		                if (this.stylo){
                            		            	   x.remove(x.size()-1);
                            		            	   y.remove(y.size()-1);
                            		                }
                            		        		nouveauX = Math.cos(Math.toRadians(angle))/Math.sin(Math.toRadians(angle)) + ancienX;
                            		        		p.setY(i);
                            		        		p.setX(nouveauX);
                            		        		ancienX = nouveauX;
                            		                if (this.stylo){
                            		            	   y.add(i);
                            		            	   x.add(nouveauX);
                            		                }
                            		        		this.changed();
                                                    try {
                                                        Thread.sleep((long) (1000 / Math.abs(nouveauY-ancienY)));
                                                    } catch (InterruptedException ex) {
                                                        Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                                                    }
                            		        	}
                            	        	} else {
                            		        	for (i=ancienY;i>=nouveauY;i--)
                            		        	{
                            		                if (this.stylo){
                            		            	   x.remove(x.size()-1);
                            		            	   y.remove(y.size()-1);
                            		                }
                            		        		nouveauX = -Math.cos(Math.toRadians(angle))/Math.sin(Math.toRadians(angle)) + ancienX;
                            		        		p.setY(i);
                            		        		p.setX(nouveauX);
                            		        		ancienX = nouveauX;
                            		                if (this.stylo){
                            		            	   y.add(i);
                            		            	   x.add(nouveauX);
                            		                }
                            		        		this.changed();
                                                    try {
                                                        Thread.sleep((long) (1000 / Math.abs(nouveauY-ancienY)));
                                                    } catch (InterruptedException ex) {
                                                        Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                                                    }
                            		        	}
                            	        	}
                                    	}
                                    	
                            
                                    }
                            
                                    if (c instanceof Reculer)
                                    {
                                       /*
                                       if (this.stylo){
                                           x.add(p.getX());
                                           y.add(p.getY());
                                       }
                                       p.setX( p.getX() - (c.getArgs().get(0).getA()) * Math.cos(Math.toRadians(angle)) );
                                       p.setY( p.getY() - (c.getArgs().get(0).getA()) * Math.sin(Math.toRadians(angle)) );
                                       if (this.stylo){
                                           x.add(p.getX());
                                           y.add(p.getY());
                                       }
                                       */
                                            ancienX = p.getX();
                                    	ancienY = p.getY();
                                    	nouveauX = p.getX() -  (double)(c.getArgs().get(0).getA()) * Math.cos(Math.toRadians(angle));
                                    	nouveauY = p.getY() -  (double)(c.getArgs().get(0).getA()) * Math.sin(Math.toRadians(angle));
                            	        
                                        if (this.stylo){
                                       	   x.add(ancienX);
                                           y.add(ancienY);
                                       	   x.add(nouveauX);
                                           y.add(nouveauY);
                                        }
                                    	
                                    	if (Math.abs((ancienX-nouveauX)/(ancienY-nouveauY)) >= 1)
                                    	{
                                    		if (ancienX - nouveauX <= 0)
                            	        	{
                            		        	for (i=ancienX;i<=nouveauX;i++)
                            		        	{
                            		                if (this.stylo){
                            		            	   x.remove(x.size()-1);
                            		            	   y.remove(y.size()-1);
                            		                }
                            		        		nouveauY = +Math.tan(Math.toRadians(angle)) + ancienY;
                            		        		p.setX(i);
                            		        		p.setY(nouveauY);
                            		        		ancienY = nouveauY;
                            		                if (this.stylo){
                            		            	   x.add(i);
                            		            	   y.add(nouveauY);
                            		                }
                            		        		this.changed();
                                                    try {
                                                        Thread.sleep((long) (1000 / Math.abs(nouveauX-ancienX)));
                                                    } catch (InterruptedException ex) {
                                                        Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                                                    }
                            		        	}
                            	        	} else {
                            		        	for (i=ancienX;i>=nouveauX;i--)
                            		        	{
                            		                if (this.stylo){
                            		            	   x.remove(x.size()-1);
                            		            	   y.remove(y.size()-1);
                            		                }
                            		        		nouveauY = -Math.tan(Math.toRadians(angle)) + ancienY;
                            		        		p.setX(i);
                            		        		p.setY(nouveauY);
                            		        		ancienY = nouveauY;
                            		                if (this.stylo){
                            		            	   x.add(i);
                            		            	   y.add(nouveauY);
                            		                }
                            		        		this.changed();
                                                    try {
                                                        Thread.sleep((long) (1000 / Math.abs(nouveauX-ancienX)));
                                                    } catch (InterruptedException ex) {
                                                        Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                                                    }
                            		        	}
                            	        	}
                                    	} else {
                                    		if (ancienY - nouveauY <= 0)
                            	        	{
                            		        	for (i=ancienY;i<=nouveauY;i++)
                            		        	{
                            		                if (this.stylo){
                            		            	   x.remove(x.size()-1);
                            		            	   y.remove(y.size()-1);
                            		                }
                            		        		nouveauX = +Math.cos(Math.toRadians(angle))/Math.sin(Math.toRadians(angle)) + ancienX;
                            		        		p.setY(i);
                            		        		p.setX(nouveauX);
                            		        		ancienX = nouveauX;
                            		                if (this.stylo){
                            		            	   y.add(i);
                            		            	   x.add(nouveauX);
                            		                }
                            		        		this.changed();
                                                    try {
                                                        Thread.sleep((long) (1000 / Math.abs(nouveauY-ancienY)));
                                                    } catch (InterruptedException ex) {
                                                        Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                                                    }
                            		        	}
                            	        	} else {
                            		        	for (i=ancienY;i>=nouveauY;i--)
                            		        	{
                            		                if (this.stylo){
                            		            	   x.remove(x.size()-1);
                            		            	   y.remove(y.size()-1);
                            		                }
                            		        		nouveauX = -Math.cos(Math.toRadians(angle))/Math.sin(Math.toRadians(angle)) + ancienX;
                            		        		p.setY(i);
                            		        		p.setX(nouveauX);
                            		        		ancienX = nouveauX;
                            		                if (this.stylo){
                            		            	   y.add(i);
                            		            	   x.add(nouveauX);
                            		                }
                            		        		this.changed();
                                                    try {
                                                        Thread.sleep((long) (1000 / Math.abs(nouveauY-ancienY)));
                                                    } catch (InterruptedException ex) {
                                                        Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                                                    }
                            		        	}
                            	        	}
                                    	}
                                    }
                                    
                                    if (c instanceof TournerGauche)
                                    {
                                       this.angle -= (c.getArgs().get(0).getA()) % 360;
                                    }
                                    
                                    if (c instanceof TournerDroite)
                                    {
                                       this.angle += (c.getArgs().get(0).getA()) % 360;
                                    }
                                    
                                    if (c instanceof Baisser)
                                    {
                                       this.stylo = true;
                                    }
                                    
                                    if (c instanceof Lever)
                                    {
                                       this.stylo = false;
                                    }
                                    
                                    this.changed();
                                    
                                    return 0;
                                }
                                
                                public void reinitialiserLigne() {
                                    this.x.clear();
                                    this.y.clear();
                                    this.changed();
                                }
                                
                                public String toString() 
                                {
                                    String s = "";
                                    if (stylo == false)
                                    {
                                        s = "lever";
                                    }
                                    else
                                    {
                                        s = "baisser";
                                    }
                                    return "position : " + this.p.toString() + ", angle : " + this.angle + ", stylo : " + s;
                                }
                                  
                            }
                            
                            • Partager sur Facebook
                            • Partager sur Twitter
                            Anonyme
                              3 avril 2009 à 22:00:46

                              Je pense que tu devrais jeter ton code, prendre un crayon et une feuille et repenser ton architecture en premier lieu.

                              ce code rassemble un tas de mauvaises pratiques (complexité cyclomatique, polymorphisme foireux, redondance, illisibilité...).

                              Ca te prendra un peu plus de temps au début mais ça te simplifiera la tâche par après.


                              • Partager sur Facebook
                              • Partager sur Twitter
                                4 avril 2009 à 0:33:02

                                Je n'arrive pas à lire son code, donc je le reecris ici dans les balises.
                                package upmc.li260.robot.logo.langage;
                                
                                import java.util.ArrayList;
                                import java.util.logging.Level;
                                import java.util.logging.Logger;
                                
                                public class Tortue extends Model implements IEffecteur{
                                private Position p;
                                private double angle;
                                private boolean stylo; // false=lever, true=baisser
                                private int rayon;
                                private ArrayList<Double> x, y;
                                
                                /** Creates a new instance of Tortue */
                                public Tortue() {
                                this.p=new Position();
                                this.angle = 0;
                                this.stylo = false;
                                this.rayon = 20;
                                this.x = new ArrayList<Double>();
                                this.y = new ArrayList<Double>();
                                }
                                
                                public int getRayon(){
                                return this.rayon;
                                }
                                
                                public Position getPosition(){
                                return this.p;
                                }
                                
                                public double getAngle(){
                                return this.angle;
                                }
                                
                                public ArrayList<Double> getX(){
                                return this.x;
                                }
                                
                                public ArrayList<Double> getY(){
                                return this.y;
                                }
                                
                                public boolean getStylo(){
                                return this.stylo;
                                }
                                
                                public int execute(Commande c)
                                {
                                
                                double i, j, ancienX, nouveauX, ancienY, nouveauY;
                                
                                if (c instanceof Avancer)
                                {
                                /*
                                if (this.stylo){
                                x.add(p.getX());
                                y.add(p.getY());
                                }
                                p.setX( p.getX() + (c.getArgs().get(0).getA()) * Math.cos(Math.toRadians(angle)) );
                                p.setY( p.getY() + (c.getArgs().get(0).getA()) * Math.sin(Math.toRadians(angle)) );
                                if (this.stylo){
                                x.add(p.getX());
                                y.add(p.getY());
                                }
                                */
                                ancienX = p.getX();
                                ancienY = p.getY();
                                nouveauX = p.getX() + (double)(c.getArgs().get(0).getA()) * Math.cos(Math.toRadians(angle));
                                nouveauY = p.getY() + (double)(c.getArgs().get(0).getA()) * Math.sin(Math.toRadians(angle));
                                
                                if (this.stylo){
                                x.add(ancienX);
                                y.add(ancienY);
                                x.add(nouveauX);
                                y.add(nouveauY);
                                }
                                
                                if (Math.abs((ancienX-nouveauX)/(ancienY-nouveauY)) >= 1)
                                {
                                if (ancienX - nouveauX <= 0)
                                {
                                for (i=ancienX;i<=nouveauX;i++)
                                {
                                if (this.stylo){
                                x.remove(x.size()-1);
                                y.remove(y.size()-1);
                                }
                                nouveauY = Math.tan(Math.toRadians(angle)) + ancienY;
                                p.setX(i);
                                p.setY(nouveauY);
                                ancienY = nouveauY;
                                if (this.stylo){
                                x.add(i);
                                y.add(nouveauY);
                                }
                                this.changed();
                                try {
                                Thread.sleep((long) (1000 / Math.abs(nouveauX-ancienX)));
                                } catch (InterruptedException ex) {
                                Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                                }
                                }
                                } else {
                                for (i=ancienX;i>=nouveauX;i--)
                                {
                                if (this.stylo){
                                x.remove(x.size()-1);
                                y.remove(y.size()-1);
                                }
                                nouveauY = -Math.tan(Math.toRadians(angle)) + ancienY;
                                p.setX(i);
                                p.setY(nouveauY);
                                ancienY = nouveauY;
                                if (this.stylo){
                                x.add(i);
                                y.add(nouveauY);
                                }
                                this.changed();
                                try {
                                Thread.sleep((long) (1000 / Math.abs(nouveauX-ancienX)));
                                } catch (InterruptedException ex) {
                                Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                                }
                                }
                                }
                                } else {
                                if (ancienY - nouveauY <= 0)
                                {
                                for (i=ancienY;i<=nouveauY;i++)
                                {
                                if (this.stylo){
                                x.remove(x.size()-1);
                                y.remove(y.size()-1);
                                }
                                nouveauX = Math.cos(Math.toRadians(angle))/Math.sin(Math.toRadians(angle)) + ancienX;
                                p.setY(i);
                                p.setX(nouveauX);
                                ancienX = nouveauX;
                                if (this.stylo){
                                y.add(i);
                                x.add(nouveauX);
                                }
                                this.changed();
                                try {
                                Thread.sleep((long) (1000 / Math.abs(nouveauY-ancienY)));
                                } catch (InterruptedException ex) {
                                Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                                }
                                }
                                } else {
                                for (i=ancienY;i>=nouveauY;i--)
                                {
                                if (this.stylo){
                                x.remove(x.size()-1);
                                y.remove(y.size()-1);
                                }
                                nouveauX = -Math.cos(Math.toRadians(angle))/Math.sin(Math.toRadians(angle)) + ancienX;
                                p.setY(i);
                                p.setX(nouveauX);
                                ancienX = nouveauX;
                                if (this.stylo){
                                y.add(i);
                                x.add(nouveauX);
                                }
                                this.changed();
                                try {
                                Thread.sleep((long) (1000 / Math.abs(nouveauY-ancienY)));
                                } catch (InterruptedException ex) {
                                Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                                }
                                }
                                }
                                }
                                
                                
                                }
                                
                                if (c instanceof Reculer)
                                {
                                /*
                                if (this.stylo){
                                x.add(p.getX());
                                y.add(p.getY());
                                }
                                p.setX( p.getX() - (c.getArgs().get(0).getA()) * Math.cos(Math.toRadians(angle)) );
                                p.setY( p.getY() - (c.getArgs().get(0).getA()) * Math.sin(Math.toRadians(angle)) );
                                if (this.stylo){
                                x.add(p.getX());
                                y.add(p.getY());
                                }
                                */
                                ancienX = p.getX();
                                ancienY = p.getY();
                                nouveauX = p.getX() - (double)(c.getArgs().get(0).getA()) * Math.cos(Math.toRadians(angle));
                                nouveauY = p.getY() - (double)(c.getArgs().get(0).getA()) * Math.sin(Math.toRadians(angle));
                                
                                if (this.stylo){
                                x.add(ancienX);
                                y.add(ancienY);
                                x.add(nouveauX);
                                y.add(nouveauY);
                                }
                                
                                if (Math.abs((ancienX-nouveauX)/(ancienY-nouveauY)) >= 1)
                                {
                                if (ancienX - nouveauX <= 0)
                                {
                                for (i=ancienX;i<=nouveauX;i++)
                                {
                                if (this.stylo){
                                x.remove(x.size()-1);
                                y.remove(y.size()-1);
                                }
                                nouveauY = +Math.tan(Math.toRadians(angle)) + ancienY;
                                p.setX(i);
                                p.setY(nouveauY);
                                ancienY = nouveauY;
                                if (this.stylo){
                                x.add(i);
                                y.add(nouveauY);
                                }
                                this.changed();
                                try {
                                Thread.sleep((long) (1000 / Math.abs(nouveauX-ancienX)));
                                } catch (InterruptedException ex) {
                                Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                                }
                                }
                                } else {
                                for (i=ancienX;i>=nouveauX;i--)
                                {
                                if (this.stylo){
                                x.remove(x.size()-1);
                                y.remove(y.size()-1);
                                }
                                nouveauY = -Math.tan(Math.toRadians(angle)) + ancienY;
                                p.setX(i);
                                p.setY(nouveauY);
                                ancienY = nouveauY;
                                if (this.stylo){
                                x.add(i);
                                y.add(nouveauY);
                                }
                                this.changed();
                                try {
                                Thread.sleep((long) (1000 / Math.abs(nouveauX-ancienX)));
                                } catch (InterruptedException ex) {
                                Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                                }
                                }
                                }
                                } else {
                                if (ancienY - nouveauY <= 0)
                                {
                                for (i=ancienY;i<=nouveauY;i++)
                                {
                                if (this.stylo){
                                x.remove(x.size()-1);
                                y.remove(y.size()-1);
                                }
                                nouveauX = +Math.cos(Math.toRadians(angle))/Math.sin(Math.toRadians(angle)) + ancienX;
                                p.setY(i);
                                p.setX(nouveauX);
                                ancienX = nouveauX;
                                if (this.stylo){
                                y.add(i);
                                x.add(nouveauX);
                                }
                                this.changed();
                                try {
                                Thread.sleep((long) (1000 / Math.abs(nouveauY-ancienY)));
                                } catch (InterruptedException ex) {
                                Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                                }
                                }
                                } else {
                                for (i=ancienY;i>=nouveauY;i--)
                                {
                                if (this.stylo){
                                x.remove(x.size()-1);
                                y.remove(y.size()-1);
                                }
                                nouveauX = -Math.cos(Math.toRadians(angle))/Math.sin(Math.toRadians(angle)) + ancienX;
                                p.setY(i);
                                p.setX(nouveauX);
                                ancienX = nouveauX;
                                if (this.stylo){
                                y.add(i);
                                x.add(nouveauX);
                                }
                                this.changed();
                                try {
                                Thread.sleep((long) (1000 / Math.abs(nouveauY-ancienY)));
                                } catch (InterruptedException ex) {
                                Logger.getLogger(Tortue.class.getName()).log(Level.SEVERE, null, ex);
                                }
                                }
                                }
                                }
                                }
                                
                                if (c instanceof TournerGauche)
                                {
                                this.angle -= (c.getArgs().get(0).getA()) % 360;
                                }
                                
                                if (c instanceof TournerDroite)
                                {
                                this.angle += (c.getArgs().get(0).getA()) % 360;
                                }
                                
                                if (c instanceof Baisser)
                                {
                                this.stylo = true;
                                }
                                
                                if (c instanceof Lever)
                                {
                                this.stylo = false;
                                }
                                
                                this.changed();
                                
                                return 0;
                                }
                                
                                public void reinitialiserLigne() {
                                this.x.clear();
                                this.y.clear();
                                this.changed();
                                }
                                
                                public String toString()
                                {
                                String s = "";
                                if (stylo == false)
                                {
                                s = "lever";
                                }
                                else
                                {
                                s = "baisser";
                                }
                                return "position : " + this.p.toString() + ", angle : " + this.angle + ", stylo : " + s;
                                }
                                
                                }
                                
                                • Partager sur Facebook
                                • Partager sur Twitter
                                J'ai tous les badges d'OpenClassrooms.

                                Projet en java

                                × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                                × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                                • Editeur
                                • Markdown