package application; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.beans.*; import graphe.*; /** * Définit la fenêtre principale qui contient tous les objets graphiques : menu, champs de saisie, boutons, dessin du graphe ainsi que les commentaires. */ public final class CadrePrincipal extends JFrame implements MouseListener { //Fichier contenant la matrice à charger private String fichier; //Conteneurs private JPanel contentPane; private BorderLayout borderLayout1 = new BorderLayout(); //Menu private JMenuBar jMenuBar1 = new JMenuBar(); private JMenu jMenuFile = new JMenu(); private JMenuItem jMenuFileExit = new JMenuItem(); private JMenuItem jMenuFileCharger = new JMenuItem(); private JMenu jMenuHelp = new JMenu(); private JMenuItem jMenuHelpAbout = new JMenuItem(); //Zones texte private JLabel jLabel5 = new JLabel(); private JLabel jLabel4 = new JLabel(); private JLabel jLabel3 = new JLabel(); private JLabel jLabel2 = new JLabel(); private JLabel jLabel1 = new JLabel(); private JLabel jLabel6 = new JLabel(); private JLabel jLabel7 = new JLabel(); private JLabel jLabel8 = new JLabel(); //Zones de saisie private JTextField jTextField5 = new JTextField(); private JTextField jTextField4 = new JTextField(); private JTextField jTextField3 = new JTextField(); private JTextField jTextField2 = new JTextField(); private JTextField jTextField1 = new JTextField(); private JTextField jTextField6 = new JTextField(); private JTextField jTextField7 = new JTextField(); //Combos private Object[] dataObject = { "Circulation Jeton", "Distribué" }; private JComboBox jComboBox1 = new JComboBox(dataObject); //Cadres private JTextArea jTextArea2 = new JTextArea(); private JTextArea jTextArea1 = new JTextArea(); private JTextArea jTextArea3 = new JTextArea(); private JTextArea jTextArea4 = new JTextArea(); private JScrollPane jScrollPane1 = new JScrollPane(); //Boutons private JButton jButton2 = new JButton(); private JButton jButton1 = new JButton(); private JButton jButton3 = new JButton(); //Zone de dessin private CadreDessin cadreDessin = new CadreDessin(jTextArea4); //Gestion des messages d'erreur private JOptionPane jOptionPane1 = new JOptionPane(); private int typeCalcul; /** * Constructeur du cadre */ public CadrePrincipal() { addMouseListener(this); enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } /** * Initialisation de la fenêtre */ private final void jbInit() throws Exception { //Fichier contenant la matrice à charger fichier = ""; //Fenêtre this.setSize(new Dimension(700, 590)); this.setTitle("K-Coloration Distribuée"); //conteneur contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(null); //Menu jMenuFile.setText("Fichier"); jMenuFileExit.setText("Quitter"); jMenuFileExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jMenuFileExit_actionPerformed(e); } }); jMenuFileCharger.setText("Charger"); jMenuFileCharger.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jMenuHelpCharger_actionPerformed(e); } }); jMenuHelp.setText("Aide"); jMenuHelpAbout.setText("A propos"); jMenuHelpAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jMenuHelpAbout_actionPerformed(e); } }); //Cadres jTextArea1.setBackground(SystemColor.inactiveCaptionBorder); jTextArea1.setBorder(BorderFactory.createEtchedBorder()); jTextArea1.setText("Caractéristiques du Graphe"); jTextArea1.setBounds(new Rectangle(5, 5, 275, 100)); jTextArea1.setEditable(false); jTextArea2.setBackground(SystemColor.inactiveCaptionBorder); jTextArea2.setBorder(BorderFactory.createEtchedBorder()); jTextArea2.setText("Coloration"); jTextArea2.setBounds(new Rectangle(5, 110, 275, 100)); jTextArea2.setEditable(false); jTextArea3.setBackground(SystemColor.inactiveCaptionBorder); jTextArea3.setBorder(BorderFactory.createEtchedBorder()); jTextArea3.setText("Statistiques"); jTextArea3.setBounds(new Rectangle(5, 215, 275, 160)); jTextArea3.setEditable(false); jTextArea4.setBackground(SystemColor.inactiveCaptionBorder); jTextArea4.setText(""); jTextArea4.setEditable(false); jScrollPane1.setViewportView(jTextArea4); jScrollPane1.setBounds(new Rectangle(5, 380, 680, 150)); //Zones de saisie jTextField1.setBounds(new Rectangle(150, 25, 120, 20)); jTextField2.setBounds(new Rectangle(150, 50, 120, 20)); jTextField3.setBounds(new Rectangle(150, 130, 120, 20)); jTextField4.setBounds(new Rectangle(150, 235, 120, 20)); jTextField4.setEditable(false); jTextField5.setBounds(new Rectangle(150, 260, 120, 20)); jTextField5.setEditable(false); jTextField6.setBounds(new Rectangle(150, 285, 120, 20)); jTextField6.setEditable(false); jTextField7.setBounds(new Rectangle(150, 310, 120, 20)); jTextField7.setEditable(false); //Combo jComboBox1.setBounds(new Rectangle(150, 155, 120, 20)); jComboBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jComboBox1_actionPerformed(e); } }); //Libellés jLabel1.setText("Nombre de sommets"); jLabel1.setBounds(new Rectangle(10, 25, 150, 17)); jLabel2.setText("Probabilité de connexité"); jLabel2.setBounds(new Rectangle(10, 50, 150, 17)); jLabel3.setText("Nombre de Couleurs"); jLabel3.setBounds(new Rectangle(10, 130, 150, 17)); jLabel4.setText("Type de traitement"); jLabel4.setBounds(new Rectangle(10, 155, 150, 17)); jLabel5.setText("Nombre de messages"); jLabel5.setBounds(new Rectangle(10, 235, 150, 17)); jLabel6.setText("Nb Sommets simultanés"); jLabel6.setBounds(new Rectangle(10, 260, 150, 17)); jLabel7.setText("Couleurs utilisées"); jLabel7.setBounds(new Rectangle(10, 285, 150, 17)); jLabel8.setText("Messages Coloration"); jLabel8.setBounds(new Rectangle(10, 310, 150, 17)); //Boutons jButton1.setText("Générer"); jButton1.setBounds(new Rectangle(150, 75, 120, 20)); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jButton2.setText("Démarrer"); jButton2.setBounds(new Rectangle(150, 180, 120, 20)); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton2_actionPerformed(e); } }); jButton3.setText("Afficher"); jButton3.setBounds(new Rectangle(150, 335, 120, 20)); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton3_actionPerformed(e); } }); //Zone de dessin cadreDessin.setBorder(BorderFactory.createEtchedBorder()); cadreDessin.setBounds(new Rectangle(285, 5, 400, 370)); //Ajout des éléments jMenuFile.add(jMenuFileCharger); jMenuFile.add(jMenuFileExit); jMenuHelp.add(jMenuHelpAbout); jMenuBar1.add(jMenuFile); jMenuBar1.add(jMenuHelp); contentPane.add(jTextField7, null); contentPane.add(jTextField6, null); contentPane.add(jTextField5, null); contentPane.add(jTextField4, null); contentPane.add(jTextField3, null); contentPane.add(jTextField2, null); contentPane.add(jTextField1, null); contentPane.add(jComboBox1, null); contentPane.add(jButton3, null); contentPane.add(jButton2, null); contentPane.add(jButton1, null); contentPane.add(cadreDessin, null); contentPane.add(jLabel8, null); contentPane.add(jLabel7, null); contentPane.add(jLabel6, null); contentPane.add(jLabel5, null); contentPane.add(jLabel4, null); contentPane.add(jLabel3, null); contentPane.add(jLabel2, null); contentPane.add(jLabel1, null); contentPane.add(jScrollPane1, null); contentPane.add(jTextArea2, null); contentPane.add(jTextArea1, null); contentPane.add(jTextArea3, null); this.setJMenuBar(jMenuBar1); } /** * Opération Fichier | Quitter effectuée */ private final void jMenuFileExit_actionPerformed(ActionEvent e) { System.exit(0); } /** * Opération Aide | A propos effectuée */ private final void jMenuHelpAbout_actionPerformed(ActionEvent e) { CadreAPropos dlg = new CadreAPropos(this); Dimension dlgSize = dlg.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); dlg.setLocation( (frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setModal(true); dlg.show(); } /** * Opération Fichier | Charger graphe */ private final void jMenuHelpCharger_actionPerformed(ActionEvent e) { fichier=""; SelecteurFichiers dlg = new SelecteurFichiers(this); Dimension dlgSize = dlg.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); dlg.setLocation( (frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setModal(true); if(!fichier.equals("")) chargerFichier(); } /** * Remplacé, ainsi nous pouvons sortir quand la fenêtre est fermée */ protected final void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { jMenuFileExit_actionPerformed(null); } } private final void jComboBox1_actionPerformed(ActionEvent e) { } private final void jComboBox1_propertyChange(PropertyChangeEvent e) { } private final void jButton2_actionPerformed(ActionEvent e) { int nbCoul = -1; boolean ok = true; try { jTextArea4.setText(""); nbCoul = (new Integer(jTextField3.getText().trim())).parseInt( jTextField3.getText().trim()); if (jComboBox1.getSelectedItem() == "Circulation Jeton") { typeCalcul=1; } else if (jComboBox1.getSelectedItem() == "Distribué") { typeCalcul=2; } if (nbCoul < 0) ok = false; if (!ok) { JOptionPane.showMessageDialog( jOptionPane1, "Le nombre de couleurs est invalide.", "Erreur.", JOptionPane.ERROR_MESSAGE); } else { cadreDessin.start(typeCalcul, nbCoul); } repaint(); } catch (Exception theException) { JOptionPane.showMessageDialog( jOptionPane1, "Le nombre de couleurs est invalide.", "Erreur.", JOptionPane.ERROR_MESSAGE); } } private final void jButton3_actionPerformed(ActionEvent e) { jTextField4.setText(new Integer(cadreDessin.getEcouteur().getNbMess()).toString()); String stat = "1"; if (cadreDessin.getEcouteur().type != 1) stat = (new Long(cadreDessin.getEcouteur().getDuree())).toString(); jTextField5.setText(stat); jTextField6.setText(new Integer(cadreDessin.getEcouteur().getNbCouleurs()).toString()); jTextField7.setText(new Integer(cadreDessin.getEcouteur().getNbMessCouleur()).toString()); repaint(); } private final void chargerFichier(){ jTextArea4.setText(""); cadreDessin.initSommets(fichier); repaint(); } private final void jButton1_actionPerformed(ActionEvent e) { int nbSom = -1; int lambda = -1; boolean ok = true; try { jTextArea4.setText(""); nbSom = (new Integer(jTextField1.getText().trim())).parseInt( jTextField1.getText().trim()); lambda = (new Integer(jTextField2.getText().trim())).parseInt( jTextField2.getText().trim()); if (nbSom < 0 || nbSom > 500) ok = false; if (lambda < 0 || lambda > 100) ok = false; if (!ok) { JOptionPane.showMessageDialog( jOptionPane1, "Le nombre de sommets ou la probabilité de connexité est invalide.", "Erreur.", JOptionPane.ERROR_MESSAGE); } else { cadreDessin.initSommets(nbSom, lambda); } repaint(); } catch (Exception theException) { JOptionPane.showMessageDialog( jOptionPane1, "Le nombre de sommets ou la probabilité de connexité est invalide.", "Erreur.", JOptionPane.ERROR_MESSAGE); } } final void setFile(String file){ fichier = file; } //Souris public final void mouseEntered(MouseEvent e) { } public final void mouseClicked(MouseEvent e) { } public final void mouseExited(MouseEvent e) { } public final void mouseDragged(MouseEvent e) { } public final void mouseMoved(MouseEvent e) { } public final void mousePressed(MouseEvent e) { cadreDessin.mousePressed(e); } public final void mouseReleased(MouseEvent e) { cadreDessin.mouseReleased(e); repaint(); } }