public void getCellsInfos() { nbPredateurs = 0; nbProies = 0; for (int i = 0; i < GUI.getRows(); i++) for (int j = 0; j < GUI.getCols(); j++) { if (GUI.getTheGame().getEnv().getCellAt(i, j) instanceof Predateur) nbPredateurs++; else nbProies++; } }
@Override public void run() { int i = 0; Calendar start; Calendar end; demarre = 1; if (nbCycles == 0) { while (demarre != 0) { start = new GregorianCalendar(); faireUnCycle(); end = new GregorianCalendar(); GUI.AfficherGrille(); getCellsInfos(); inf = new InfoTable( i++, nbPredateurs, nbProies, end.getTimeInMillis() - start.getTimeInMillis()); ((GenerationsTable) theUI.getTableGenerations().getModel()).addRow(inf); theUI.getChart().actualiser(nbPredateurs, nbProies); start = end = null; theUI.getGridPanel().repaint(); try { synchronized (this) { wait(100); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } else { while (nbCycles != 0 && demarre != 0) { start = new GregorianCalendar(); faireUnCycle(); end = new GregorianCalendar(); nbCycles--; GUI.AfficherGrille(); theUI.getGridPanel().repaint(); getCellsInfos(); inf = new InfoTable( i++, nbPredateurs, nbProies, end.getTimeInMillis() - start.getTimeInMillis()); theUI.getChart().actualiser((double) nbPredateurs, (double) nbProies); start = end = null; ((GenerationsTable) theUI.getTableGenerations().getModel()).addRow(inf); try { synchronized (this) { wait(100); } } catch (InterruptedException e) { e.printStackTrace(); } } } demarre = 0; }
@Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Start")) { if (Reconstruct.demarre == 0) { if (!GUI.getTxtCycles().getText().equals("")) GUI.iterer(Integer.parseInt(GUI.getTxtCycles().getText())); else GUI.iterer(0); } } else if (e.getActionCommand().equals("Stop")) { if (Reconstruct.demarre == 1) { Reconstruct.demarre = 0; } } else if (e.getActionCommand().equals("Générer")) { for (int i = 0; i < GUI.getRows(); i++) for (int j = 0; j < GUI.getCols(); j++) { if (MyRand()) { GUI.getTheGame().getEnv().setCellAt(i, j, new Predateur(i, j)); GUI.getBtnList().get(i * GUI.getCols() + j).setBackground(Color.WHITE); } } } else { JButton temp = (JButton) e.getSource(); int position = GUI.getBtnList().indexOf(temp); if (temp.getBackground() == Color.WHITE) { GUI.getTheGame() .getEnv() .setCellAt( position / GUI.getCols(), position % GUI.getRows(), new Proie(position / GUI.getCols(), position % GUI.getRows())); // GUI.getTheGame().getEnv().setCellAt(position/GUI.getCols(), position%GUI.getRows(), // false); temp.setBackground(Color.BLACK); } else { GUI.getTheGame() .getEnv() .setCellAt( position / GUI.getCols(), position % GUI.getRows(), new Predateur(position / GUI.getCols(), position % GUI.getRows())); // GUI.getTheGame().getEnv().setCellAt(position/GUI.getCols(), position%GUI.getRows(), // true); temp.setBackground(Color.WHITE); } } }