Les threads java

Résolu
rayhana1 Messages postés 11 Date d'inscription lundi 19 mai 2008 Statut Membre Dernière intervention 24 février 2010 - 8 juin 2008 à 22:12
rayhana1 Messages postés 11 Date d'inscription lundi 19 mai 2008 Statut Membre Dernière intervention 24 février 2010 - 10 juin 2008 à 21:32
Bonsoir,



<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" /??>
 





         J’ai un problème de thread dans mon projet de fin d’étude, voici ce code :

import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;


public class Main extends JFrame{
    static Main fenetre = new Main();
    public Main() {               //constructeur       
        super( "Ad-Hoc" );
    }
   
   public static void main( String args[] ) {     
      fenetre.resize(700,500);
      fenetre.setJMenuBar(new barreMenu( ));
      fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
      fenetre.setLocationRelativeTo(null);      //On centre la fenêtre sur l'écran
      fenetre.setVisible( true );
   }
}


class barreMenu extends JMenuBar {
      
    private String[] fixeItems = new String[] {"Scénario1"};
   
    public barreMenu( ) {


        JMenu connexion = new JMenu("Connexion");
        //les sous menus de "connexion"
        JMenu protocole1 = new JMenu("Protocole ZHLS");
        JMenu fixe = new JMenu("Fixe");
      
      
        ActionListener P1scénario1= new ActionListener( ) {
                public void actionPerformed(ActionEvent e) {
                    Main.fenetre.getContentPane().removeAll();
                    Main.fenetre.add(new pan1());
                    Main.fenetre.show(); 
                




                   try {








                        Thread.sleep(3000);








                    } catch (InterruptedException ex) {








                         Logger.getLogger(barreMenu.class.getName()).log(Level.SEVERE, null, ex);








                   

}





                    Main.fenetre.add(new pan2());
                    Main.fenetre.show();
                   
                }
        };
       
        JMenuItem itemC_P1 = new JMenuItem(fixeItems[0]);
        itemC_P1.addActionListener(P1scénario1);       
        fixe.add(itemC_P1);
       
        protocole1.add(fixe);
        connexion.add(protocole1);    
        add(connexion);       
    }
}


class pan1 extends JComponent {
    private static Color blueT = new Color(0,0,255,25);
    private static Color redT = new Color(255,0,100,50);
    private static Color blackT = new Color(0,0,0,100);


    public pan1() {
       setDoubleBuffered(true);
       setOpaque(true);
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
       
        int i,j;
        int noeud=10;
      
        g.setColor(Color.white);
        g.fillRect(0,0,getWidth(),getHeight());
       
        g.setColor(blackT);    
        g.drawOval(20,100,260,300);
        g.drawOval(220,0,260,300);
        g.drawOval(400,125,260,300);
       
        //dessiner tous les noeuds        
        for(i=0;i<20;i++){
            if((i==5)||(i==12)||(i==13)){ 
                int x= noeudInfo.Tab[i][0];         
                int y= noeudInfo.Tab[i][1];
                int p= noeudInfo.Tab[i][2];
                g.setColor(redT); 
                g.fillOval(x-p+noeud/2,y-p+noeud/2,p*2,p*2);
                g.setColor(Color.black); 
                g.fillOval(x,y,noeud,noeud);
            }
            else{
                int x= noeudInfo.Tab[i][0];         
                int y= noeudInfo.Tab[i][1];
                int p= noeudInfo.Tab[i][2];
                g.setColor(blueT); 
                g.fillOval(x-p+noeud/2,y-p+noeud/2,p*2,p*2);
                g.setColor(Color.black); 
                g.fillOval(x,y,noeud,noeud);
            }   
        }
        //optimiser le nonbre de liens
        //premier groupement
        for(i=0;i<5;i++){
            int x1= noeudInfo.Tab[5][0]+5;        
            int y1= noeudInfo.Tab[5][1]+5;
            int p1= noeudInfo.Tab[5][2]+5;
           
            int x2= noeudInfo.Tab[i][0]+5;
            int y2= noeudInfo.Tab[i][1]+5;
            int p2= noeudInfo.Tab[i][2]+5;
                if((noeudInfo.distance(5,i)


 class pan2 extends JComponent {
    private static Color blueT = new Color(0,0,255,25);
    private static Color redT = new Color(255,0,100,50);
    private static Color blackT = new Color(0,0,0,100);


    public pan2() {
       setDoubleBuffered(true);
       setOpaque(true);
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
       
        int i,j;      
         //liens entre le premier et le deuxième groupe
        for( i=0;i<6;i++){
            for(j=6;j<13;j++){
                int x1= noeudInfo.Tab[i][0]+5;         
                int y1= noeudInfo.Tab[i][1]+5;
                int p1= noeudInfo.Tab[i][2]+5;
               
                int x2= noeudInfo.Tab[j][0]+5;         
                int y2= noeudInfo.Tab[j][1]+5;
                int p2= noeudInfo.Tab[j][2]+5;
                if((noeudInfo.distance(i,j)
class noeudInfo extends JFrame implements ActionListener{
  
    static Object a;
    public static int Tab[][]={
        //    X         y       la portee
        {    200 ,     200 ,       75    },
        {    150 ,     185 ,       60    },
        {    85  ,     250 ,       60    },
        {    100 ,     300 ,       60    },
        {    200 ,     300 ,       60    },
        {    150 ,     250 ,       75    },
        // deuxieme groupemment
        {    250 ,     150 ,       75    },
        {    300 ,     125 ,       60    },
        {    325 ,     75  ,       60    },
        {    375 ,     60  ,       60    },
        {    400 ,     100 ,       60    },
        {    400 ,     200 ,       60    },
        {    350 ,     150 ,       75    },
        // troisieme groupemment
        {    500 ,     225 ,       60    },
        {    550 ,     250 ,       60    },
        {    450 ,     250 ,       75    },
        {    525 ,     200 ,       60    },
        {    600 ,     250 ,       60    },
        {    525 ,     300 ,       60    },
        // hors connexion
        {    355 ,     360 ,       60    }};
   
    private JButton b;


    private static class CustomTableModel extends DefaultTableModel {
        public CustomTableModel() {}
       
        public CustomTableModel(Object[][] data, Object[] columnNames) {
            super(data, columnNames);
        }
    }
   
    public noeudInfo() {
       
        setTitle("table des noeuds");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(400, 400);
        Container c = this.getContentPane();   
        Object[] columnNames = {"Noeud","X", "Y", "Portee"};       
      
       
        final Object[][] data = {
            {"    A",new Integer(Tab[0][0]),new Integer(Tab[0][1]),new Integer(Tab[0][2])},
            {"    B",new Integer(Tab[1][0]),new Integer(Tab[1][1]),new Integer(Tab[1][2])},
            {"    C",new Integer(Tab[2][0]),new Integer(Tab[2][1]),new Integer(Tab[2][2])},
            {"    D",new Integer(Tab[3][0]),new Integer(Tab[3][1]),new Integer(Tab[3][2])},
            {"    E",new Integer(Tab[4][0]),new Integer(Tab[4][1]),new Integer(Tab[4][2])},
            {"    F",new Integer(Tab[5][0]),new Integer(Tab[5][1]),new Integer(Tab[5][2])},
            {"    G",new Integer(Tab[6][0]),new Integer(Tab[6][1]),new Integer(Tab[6][2])},
            {"    H",new Integer(Tab[7][0]),new Integer(Tab[7][1]),new Integer(Tab[7][2])},
            {"    I",new Integer(Tab[8][0]),new Integer(Tab[8][1]),new Integer(Tab[8][2])},
            {"    J",new Integer(Tab[9][0]),new Integer(Tab[9][1]),new Integer(Tab[9][2])},
            {"    K",new Integer(Tab[10][0]),new Integer(Tab[10][1]),new Integer(Tab[10][2])},
            {"    L",new Integer(Tab[11][0]),new Integer(Tab[11][1]),new Integer(Tab[11][2])},
            {"    M",new Integer(Tab[12][0]),new Integer(Tab[12][1]),new Integer(Tab[12][2])},
            {"    N",new Integer(Tab[13][0]),new Integer(Tab[13][1]),new Integer(Tab[13][2])},
            {"    O",new Integer(Tab[14][0]),new Integer(Tab[14][1]),new Integer(Tab[14][2])},
            {"    P",new Integer(Tab[15][0]),new Integer(Tab[15][1]),new Integer(Tab[15][2])},
            {"    Q",new Integer(Tab[16][0]),new Integer(Tab[16][1]),new Integer(Tab[16][2])},
            {"    S",new Integer(Tab[17][0]),new Integer(Tab[17][1]),new Integer(Tab[17][2])},
            {"    T",new Integer(Tab[18][0]),new Integer(Tab[18][1]),new Integer(Tab[18][2])},
            {"    U",new Integer(Tab[19][0]),new Integer(Tab[19][1]),new Integer(Tab[19][2])},
           
       };
    
        JTable table = new JTable(new CustomTableModel(data, columnNames));       
        c.add("Center", new JScrollPane(table));
       
        int i=0;
       
       
       
        JPanel p2 = new JPanel();
        b = new JButton("OK");
        p2.add(b);
        c.add("South",p2);
        //ajouter l'ecouteur d'evenement au boutton
        b.addActionListener(this);
      
    }
   
    public static float distance(int n1,int n2){
        int x1=Tab[n1][0],
            x2=Tab[n2][0],
            y1=Tab[n1][1],
            y2=Tab[n2][1];
        float dis=(float) Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2));
        return dis;
    }
   
    public static float distance(int x1,int y1,int i){
        int x2=Tab[i][0],
            y2=Tab[i][1];
        float dis=(float) Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2));
        return dis;
    }
   
  
   
   
    public void actionPerformed(ActionEvent e) {
         dispose();        
    }
   
}
           




Premièrement je veux afficher ‘pan1’ puis bloquer l’exécution puis afficher ‘pan2’ mais le problème est que elle est bloquée avant l’affichage du premier panneau puis il n’affiche que les contenants de ‘pan1’.


J’ai aussi un autre problème : comment accéder à un JTable (déjà remplie) pour utiliser le contenant d’un de ses cases ?



 




Merci pour m’aider.

2 réponses

Utilisateur anonyme
9 juin 2008 à 08:16
Bonjour,

Je repond a la question "Titre du message"
http://rom.developpez.com/java-synchronisation/...\ Dan /...
3
rayhana1 Messages postés 11 Date d'inscription lundi 19 mai 2008 Statut Membre Dernière intervention 24 février 2010
10 juin 2008 à 21:32
Merci danimo, je va le voire
0
Rejoignez-nous