Ajouter un commentaire

hervemoha
Salut

Merci pour le code posté mais j ai quelques problemes à son implémentation. J ai quelques erreurs pour la finir, ça me renvoie des erreurs. Je vous poste ça pour voir ce qu il en est


// la classe carte



import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JPanel;

public class Carte extends JPanel{
	
	private static final long serialVersionUID = -4216707929142555363L;
	
	public Carte(ToutesLesVilles maroc){
           setSize(1000,1000);
           t=maroc.charger("C:/Documents and Settings/guy mena/Bureau/eclipse/Ville.txt");
    }
 
	public void paint(Graphics g)
    {
  	super.paint(g);
  	Font f = new Font("time new roman", 2,11);
  	g.setFont(f);
      if(villeSelectionnee!=null)
          {
              for(int i=0; i < t.length; i++)
              {
                if(t[i].getNom().equals(villeSelectionnee.getNom()))
                  {
                    g.setColor(Color.red);
                    g.drawString(t[i].getNom(), t[i].getLongitude()-25, t[i].getLatitude()-2);
                    g.fillRect(t[i].getLongitude(), t[i].getLatitude(), 2, 2);
                  }
                 else
                  {
                    g.setColor(Color.black);
                    g.drawString(t[i].getNom(), t[i].getLongitude()-25, t[i].getLatitude()-2);
                    g.fillRect(t[i].getLongitude(), t[i].getLatitude(), 2, 2);
                   }
               }
          }
        else
          {
             for(int i=0; i < t.length; i++)
             {
                    g.setColor(Color.black);
                    g.drawString(t[i].getNom(), t[i].getLongitude()-25, t[i].getLatitude()-2);
                    g.fillRect(t[i].getLongitude(), t[i].getLatitude(), 2, 2);
              }
          }

          
    }             
      

    public void update (Graphics g){
            super.update(g);
            paint(g);
    }
    
    public void dessineItineraire(Graphics g) { 
        g.setColor(Color.blue); 
        
        
		if (mItineraire.getVilleDepart() != null) { 
           g.drawString(mItineraire.getVilleDepart().getNom(),mItineraire.getVilleDepart().getCoordonnees().x - 12,mItineraire.getVilleDepart().getCoordonnees().y - 2); 
           g.fillRect(mItineraire.getVilleDepart().getCoordonnees().x,mItineraire.getVilleDepart().getCoordonnees().y,2,2); 
        } 
         
        if (mItineraire.getVilleArrivee() != null) { 
           g.drawString(mItineraire.getVilleArrivee().getNom(),mItineraire.getVilleArrivee().getCoordonnees().x  - 12,mItineraire.getVilleArrivee().getCoordonnees().y  - 2); 
            g.fillRect(mItineraire.getVilleArrivee().getCoordonnees().x,mItineraire.getVilleArrivee().getCoordonnees().y,2,2); 
        } 
         
        for (int i = 0; i < mItineraire.getVillesParcourues().size() - 1; i++) { 
           g.drawLine( 
                 mItineraire.getVillesParcourues().get(i).getCoordonnees().x, 
                 mItineraire.getVillesParcourues().get(i).getCoordonnees().y, 
                 mItineraire.getVillesParcourues().get(i + 1).getCoordonnees().x, 
                 mItineraire.getVillesParcourues().get(i + 1).getCoordonnees().y); 
        } 
     } 

    
    public Ville getVilleselectionnee(){
    	return villeSelectionnee;
    }
    
    public void setVilleSelectionnee(Ville villeSelectionnee){
    	this.villeSelectionnee = villeSelectionnee;	
    }
    
    private Ville t[];
    private Ville villeSelectionnee;
    Itineraire itineraireSelectionne, mItineraire;
    

}

// la classe Itineraire



import java.awt.List;
import java.util.ArrayList;

public class Itineraire {
	Ville [] villes;
	Ville ville;
	Itineraire itineraireSelectionne ;
	private int meilleureVille;
	private int meilleureDistance;
	private int [][] matriceDistance;
	private int[] tabDistance;
	private int[] ville1;
	private int[] ville2;
	private int[] tabTotal;
	private int[][] tabVille1;
	private int[][] tabVille2;
	private int[][] tabDistancebis;
	private int[] tabTotalbis;
	Ville mVilleDepart; 
	Ville mVilleArrivee; 
	ArrayList<Ville> mVillesParcourues; 
	
	public Itineraire() { 
		    mVillesParcourues = new ArrayList<Ville>(); 
		  } 
	
	public void calculeItineraire(){
		
	}

	  public void getVilleDepart(){
		  
	  }

	  public void getVilleArrivee(){
		  
	  }

	  public void getVillesParcourues(){
		  
	  }

	  public void setVilleDepart(Ville villeDepart){
		  
	  }
	    
	  public void setVilleArrivee(Ville villeArrivee){
		  
	  }
	    
	  public void setVillesParcourues(ArrayList<Ville> villesParcourues){
		  
	  }


	
	public Itineraire(Ville pVilleDepart,Ville pVilleArrivee){
		mVilleDepart = pVilleDepart; 
	    mVilleArrivee = pVilleArrivee; 
	    mVillesParcourues = new ArrayList<Ville>(); 

		
	}

	
	public Ville getVilleParNom(String nomVille){
    	
    	return ville;
    }
	
	public boolean contains(Ville v){
		 for(int i=0;i<villes.length;i++)
	        {
	            if  (villes[i]==v)  return  true;   //oui
	        }
	        return  false;  //non
	}

	public void setItineraireSelectionne(Itineraire i){
		
		this.itineraireSelectionne = i;
	}

	public void construireItineraire(){ 
		
		int ligne, colonne,cptVille,testVilleExistante,total,touteVille; 
		touteVille=0; 
		this.meilleureVille=-1; 
		this.meilleureDistance=1000000; 
		while (touteVille<this.matriceDistance.length){ 
			
			ligne=0; 
			colonne=0; 
			cptVille=0; 
			testVilleExistante=0; 
			total=0; 
			for(int j=0;j<this.matriceDistance.length;j++){
				
				this.tabDistance[j] = 1000000; 
			} 

			this.ville1[cptVille] = touteVille; 
			if (this.matriceDistance[ville1[cptVille]][colonne] == -1){
				
				colonne++; 
			} 
			this.ville2[cptVille]=colonne; 

			this.tabDistance[cptVille]=this.matriceDistance[ville1[cptVille]][ville2[cptVille]]; 
			colonne++; 
			while(ligne < ((this.matriceDistance.length-1))){
				
				while(colonne < ((this.matriceDistance.length-1))){
					
					while (testVilleExistante<=cptVille)
					{
						
						if (colonne == this.ville1[testVilleExistante]) 
						{ 
							colonne++; 
							testVilleExistante=-1; 
						} 
						testVilleExistante++; 
					} 
						testVilleExistante=0; 

	if ((colonne < this.matriceDistance.length) && (this.matriceDistance[ville1[cptVille]][colonne] != -1)) 
	{ 
		if (this.tabDistance[cptVille] > this.matriceDistance[ville1[cptVille]][colonne]) 
			{ 
				this.tabDistance[cptVille] = this.matriceDistance[ville1[cptVille]][colonne]; 
				this.ville2[cptVille] = colonne; 
			} 
	} 
		colonne++; 
	} 
				colonne = 0; 
				ville1[cptVille+1] = ville2[cptVille]; 
				cptVille++; 
				ligne++; 
	} 
	this.ville2[cptVille]=touteVille; 
	this.tabDistance[cptVille]=this.matriceDistance[this.ville1[cptVille]][this.ville2[cptVille]]; 
	for(int j=0;j<this.matriceDistance.length;j++) 
	{ 
	total=total+this.tabDistance[j]; 
	} 
	this.tabTotal[touteVille]=total; 

	for(int k=0;k<this.matriceDistance.length;k++) 
	{ 
	this.tabVille1[touteVille][k]=ville1[k]; 
	this.tabVille2[touteVille][k]=ville2[k]; 
	this.tabDistancebis[touteVille][k]=this.tabDistance[k]; 
	this.tabTotalbis[touteVille]=total; 
	} 
	touteVille++; 
	} 
	for(int j=0;j<this.matriceDistance.length;j++) 
	{ 
	if (this.meilleureDistance > this.tabTotal[j]) 
	{ 
	this.meilleureDistance=this.tabTotal[j]; 
	this.meilleureVille=j; 
	} 
	} 
	System.out.println(); 
	} 
	
}

// la classe Mafenetre




import java.io.*; 
import java.util.*; 

import java.awt.*; 
import java.awt.event.*; 


import javax.swing.*; 
import javax.swing.event.*; 

public class Mafenetre extends JFrame implements ActionListener, ItemListener{ 
   //la barre de menus 
   JMenuBar br = new JMenuBar(); 
      
    
   // Déclaration des menus 
   JMenu F = new JMenu("Fichier"); 
   JMenu E = new JMenu("Help"); 
   JMenu A = new JMenu("Affichage"); 
    
   //Declarations des items de menus 
    
   JMenuItem A_C = new JMenuItem("Afficher les informations d une ville"); 
   JMenuItem Q   = new JMenuItem("Quitter"); 
   JMenuItem C_A = new JMenuItem("Centre d aide"); 
   JMenuItem A_P = new JMenuItem("A propos du logiciel"); 
   JMenuItem C_C = new JMenuItem("Couleur de fond"); 
    
   //déclaration de panels 
   private Carte pcentre; 
   JPanel pest    = new JPanel(); 
   JPanel pestn   = new JPanel(); 
   JPanel pestc   = new JPanel(); 
   JPanel psud    = new JPanel(); 
   JPanel pnord   = new JPanel(); 
   JPanel pnord1  = new JPanel(); 
              
    
   JComboBox cb = new JComboBox(); 
   JComboBox cc = new JComboBox(); 
   public static final String[] nomcombo = {"dont le nom est","dont le code postale est"}; 
   JComboBox crech = new JComboBox(nomcombo); 
    
    
   JLabel lvdep = new JLabel("Ville de départ"); 
   JLabel lvar  = new JLabel("Ville d'arrivée"); 
   JLabel lval  = new JLabel("Valider"); 
   JLabel ldist = new JLabel("Distance    :"); 
   JLabel ltemp = new JLabel("Temps       :"); 
   JLabel lvp   = new JLabel("Ville Proche:"); 
   JLabel lrech = new JLabel("Recherche d'une ville"); 
   JLabel nomv  = new JLabel("Nom ville"); 
   JLabel cdp   = new JLabel("Code postal"); 
    
    
   JTextField trech = new JTextField(20); 
   JTextField ldistr= new JTextField(10); 
   JTextField ltempr= new JTextField(10); 
   JTextField nomvr = new JTextField(10); 
   JTextField cdpr  = new JTextField(10);    
    
    
   //JButton rech = new JButton("rechercher");    
   JButton bok   = new JButton("OK"); 
   JButton brech = new JButton("Rechercher"); 
    
   private Ville[] t; 
   private ToutesLesVilles maroc; 
    
   public Mafenetre() 
      { 
         //Parametre de la fenetre, titre, taille, dimension 
          
         this.setTitle("Réalisation d'un Maroc Routier"); 
         this.setSize(1024,768); 
         this.setLayout(new BorderLayout(15,10)); 
          
         ldistr.setEditable(false); 
         ltempr.setEditable(false); 
         //nomvr.setEditable(false); 
         //cdpr.setEditable(false); 
          
         maroc=new  ToutesLesVilles(); 
         t=maroc.charger("C:/Documents and Settings/guy mena/Bureau/eclipse/Ville.txt"); 
         for(int i=0; i < t.length; i++) 
            { 
            try{ 
            cb.addItem(t[i].getNom()); 
            cc.addItem(t[i].getNom()); 
               } 
            catch(Exception a) 
            { } 
            } 
         pcentre = new Carte(maroc); 
          
            
          
         //Ecouteur sur  le combobox 
         cb.addItemListener(this); 
         cc.addItemListener(this);
          
         //Ecouteur pour sortir de l application 
         Q.addActionListener(this); 
          
         //Ecouteur dans le menu affichage 
         C_C.addActionListener(this); 
         C_C.setToolTipText("Changer la couleur de fond"); 
          
         //Ecouteur sur le menu afficher les infos d une ville 
         A_C.addActionListener(this); 
          
         //ecouteur sur A_P 
         A_P.addActionListener(this);          
          
          
         //Ajout des items sur le menu fichier 
          
         F.add(A_C); 
         F.add(Q); 
          
         //Ajout des items sur le menu aide 
         E.add(C_A); 
         E.add(A_P); 
          
         //Ajout d un item sur le menu affichage 
         A.add(C_C); 
          
          
          
         //Ajout des menus : fichier et help sur la barre de menus 
         br.add(F); 
         br.add(A); 
         br.add(E); 
          
          
         //mettre la barre de menus sur la frame 
         this.setJMenuBar(br); 
          
          
          
         //Proprieté de la frame une qui va contenir la carte 
         //pcentre.setBackground(Color.red); 
          
        
                          
                    
         //Proprietés de la frame trois qui va contenir nos 2 labels avec les noms des réalisateurs et de l encadrant 
         psud.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Choix")); 
         psud.setLayout(new BoxLayout(psud,BoxLayout.X_AXIS)); 
         psud.add(Box.createHorizontalStrut(20)); 
         psud.add(lvdep); 
         psud.add(Box.createHorizontalStrut(10)); 
         psud.add(cb); 
         psud.add(Box.createHorizontalStrut(20)); 
         psud.add(lvar); 
         psud.add(Box.createHorizontalStrut(10)); 
         psud.add(cc); 
         psud.add(Box.createHorizontalStrut(20)); 
         psud.add(bok); 
          
         //panel nord 
         pnord.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Rechercher une ville")); 
         pnord.setLayout(new BorderLayout()); 
          
         pnord.add(pnord1, BorderLayout.CENTER); 
         pnord.setToolTipText("Rechercher une ville sur la carte"); 
          
          
         //panel nord 1 qui contient la zone de recherche 
          
         //pnord1.setLayout(new BoxLayout(pnord1,BoxLayout.X_AXIS)); 
         pnord1.add(lrech); 
         pnord1.add(Box.createHorizontalStrut(1)); 
         pnord1.add(crech); 
         pnord1.add(trech); 
         pnord1.add(brech); 
          
        
         //panel est 
         pest.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Resultat")); 
         pest.setLayout(new BorderLayout()); 
          
         //panel est nord 
         pestn.setLayout(new GridBagLayout()); 
         GridBagConstraints gbc = new GridBagConstraints(); 
         gbc.gridx = gbc.gridy = 0; 
         gbc.gridwidth = 1; 
         gbc.gridheight = 1; 
         gbc.weightx = 0; 
         gbc.weighty = 0; 
         gbc.insets = new Insets(10, 15, 0, 0); 
         pestn.add(ldist, gbc); 
          
         gbc.gridx =1; 
         gbc.gridy =0; 
         gbc.gridwidth = 1; 
         gbc.gridheight = 1; 
         gbc.weightx = 0; 
         gbc.weighty = 0; 
         gbc.insets = new Insets(10, 15, 0, 0); 
         pestn.add(ldistr, gbc); 
          
          
         gbc.gridx = 0; 
         gbc.gridy = 1; 
         gbc.gridwidth = 1; 
         gbc.gridheight = 1; 
         gbc.weightx = 0; 
         gbc.weighty = 0; 
         gbc.insets = new Insets(10, 15, 0, 0); 
         pestn.add(ltemp, gbc); 
          
          
         gbc.gridx = 1; 
         gbc.gridy = 1; 
         gbc.gridwidth = 1; 
         gbc.gridheight = 1; 
         gbc.weightx = 0; 
         gbc.weighty = 0; 
         gbc.insets = new Insets(10, 15, 0, 0); 
         pestn.add(ltempr, gbc); 
          
         //panel est centre 
         pestc.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Infos sur ville")); 
         pestc.setLayout(new GridBagLayout()); 
         GridBagConstraints gbcd = new GridBagConstraints(); 
         gbcd.gridx = gbc.gridy = 0; 
         gbcd.gridwidth = 1; 
         gbcd.gridheight = 1; 
         gbcd.weightx = 0; 
         gbcd.weighty = 0; 
         gbcd.insets = new Insets(10, 15, 0, 0); 
         pestc.add(nomv, gbcd); 
          
         gbcd.gridx =1; 
         gbcd.gridy =0; 
         gbcd.gridwidth = 1; 
         gbcd.gridheight = 1; 
         gbcd.weightx = 0; 
         gbcd.weighty = 0; 
         gbcd.insets = new Insets(10, 15, 0, 0); 
         pestc.add(nomvr, gbcd); 
          
          
         gbcd.gridx = 0; 
         gbcd.gridy = 1; 
         gbcd.gridwidth = 1; 
         gbcd.gridheight = 1; 
         gbcd.weightx = 0; 
         gbcd.weighty = 0; 
         gbcd.insets = new Insets(10, 15, 0, 0); 
         pestc.add(cdp, gbcd); 
          
          
         gbcd.gridx = 1; 
         gbcd.gridy = 1; 
         gbcd.gridwidth = 1; 
         gbcd.gridheight = 1; 
         gbcd.weightx = 0; 
         gbcd.weighty = 0; 
         gbcd.insets = new Insets(10, 15, 0, 0); 
         pestc.add(cdpr, gbcd); 
          
          
        
         pest.add(pestn, BorderLayout.NORTH); 
         pest.add(pestc, BorderLayout.CENTER); 
          
          
         //Ajouter nos panels sur la frame 
         this.add(pcentre,BorderLayout.CENTER); 
         this.add(pest,BorderLayout.EAST); 
         this.add(psud,BorderLayout.SOUTH); 
         this.add(pnord,BorderLayout.NORTH); 
        

} 

  /* public void itemStateChanged (ItemEvent e){ 
      if(e.getStateChange() == ItemEvent.SELECTED){ 
         String texte = (String)cb.getSelectedItem();
         
      try{    
         pcentre.setVilleSelectionnee(maroc.getVilleParNom(texte)); 
         pcentre.updateUI(); 
      } 
      catch(Exception e1){ 
         e1.printStackTrace(); 
      } 
      }
     
   } */
  /* public void itemStateChanged(ItemEvent e){
	   if(e.getSource()== cb){
		   if(e.getStateChange() == ItemEvent.SELECTED){ 
		         String texte = (String)cb.getSelectedItem();
		         
		      try{    
		         pcentre.setVilleSelectionnee(maroc.getVilleParNom(texte)); 
		         pcentre.updateUI(); 
		      } 
		      catch(Exception e1){ 
		         e1.printStackTrace(); 
		      } 
		      }
	   }
	   
	   if(e.getSource() == cc){
		   if(e.getStateChange() == ItemEvent.SELECTED){ 
		         String texte = (String)cc.getSelectedItem();
		         
		      try{    
		         pcentre.setVilleSelectionnee(maroc.getVilleParNom(texte)); 
		         pcentre.updateUI(); 
		      } 
		      catch(Exception e1){ 
		         e1.printStackTrace(); 
		      } 
		      }
	   }
   }*/
   
   public void itemStateChanged(ItemEvent e) { 
	      if (e.getSource().equals(cb)) { 
	         pcentre.getItineraire().setVilleDepart(#LaVilleSelectionnee#); 
	      } else if (e.getSource().equals(cc)) { 
	         pcentre.getItineraire().setVilleArrivee(#LaVilleSelectionnee#); 
	      } 
	             
	      pcentre.updateUI(); 
	   } 

    
   public void actionPerformed(ActionEvent argo) { 
      //Object source = argo.getSource(); 
      //int source = JFrame.EXIT_ON_CLOSE; 
                    
      if(argo.getSource() == Q) 
         { 
            int result = JOptionPane.showConfirmDialog(pcentre,"Voulez vous vraiment quittez l application","Quitter application",JOptionPane.YES_NO_OPTION); 
            if(result == JOptionPane.YES_OPTION) System.exit(0); 
         } 
    
      if(argo.getSource() == C_C) 
      { 
         Color color = JColorChooser.showDialog(this,"Choix d'une couleur",Color.RED); 
         pnord1.setBackground(color); 
         pnord.setBackground(color); 
         psud.setBackground(color); 
         pest.setBackground(color); 
         pestn.setBackground(color); 
         pestc.setBackground(color); 
          
          
      } 
    
      if(argo.getSource() == A_C) 
         { 
            String string = JOptionPane.showInputDialog(null, "Entrez le nom de la ville") ; 
            int rep = 0; 
            if( string != null) 
               { 
                  //System.out.println ("vous avez tapez:"+ string); 
                  for(int i=0; i<t.length; i++){ 
                     if(t[i].getNom().equalsIgnoreCase(string)){ 
                        System.out.println ("vous avez tapez:"+ string); 
                        System.out.println(+ t[i].getCode()); 
                        nomvr.setText(t[i].getNom()); 
                        try{    
                            pcentre.setVilleSelectionnee(maroc.getVilleParNom(string)); 
                            pcentre.updateUI(); 
                         } 
                         catch(Exception e1){ 
                            e1.printStackTrace(); 
                         } 
                        //cdpr.setText(String(t[i].getCode())); 
                     } 
               /*      if (t[i].getNom() != string){ 
                        JOptionPane.showMessageDialog(this, "Entrer une ville"); 
                        string = JOptionPane.showInputDialog(null, "Entrez le nom de la ville") ; 
                        }*/ 
                  } 
                    
                    
               } 
            } 
          
      if(argo.getSource() == brech) 
         { 
            for(int i = 0; i<t.length;i++){ 
            if(trech.getText().equals(t[i].getNom())) 
            System.out.println("vous avez choisi"+ trech.getText());} 
         } 
    
    

          
        
   } 

    
    
    
    
} 

Je compte sur vous
Merci bcp
a plus

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
 EEEE  N   N  W     W  N   N  W     W 
E NN N W W NN N W W
EEE N N N W W W N N N W W W
E N NN W W W N NN W W W
EEEE N N W W N N W W