diff --git a/Java-Missle/src/main/Program.java b/Java-Missle/src/main/Program.java index b829b3d..dbc4826 100644 --- a/Java-Missle/src/main/Program.java +++ b/Java-Missle/src/main/Program.java @@ -30,15 +30,16 @@ public class Program { public static void main(String[] args) { try { -// War warModel = xml.readXML(); - // new WarDBConnection(); -// WarDBConnection.clearWarDataBase(); War warModel = new War(); AbstractWarView viewGui = new WarGui(); - // new Server(); WarController controller = new WarController(warModel,viewGui); XMLparser xml = new XMLparser(controller, warModel); +// new WarDBConnection(); +// WarDBConnection.clearWarDataBase(); +// new Server(); + + } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { @@ -49,89 +50,7 @@ public static void main(String[] args) { } - /** - * Print menu - */ - - private static void checkEndOfWar(War war) { - int counter = 0; - int size = war.getMissileLaunchers().size(); - //test if all launchers has been destroyed - if (size > 0) { - for (Launcher l : war.getMissileLaunchers() ) { - if (l.isRunning() == false) { - counter++; - //loop to check if there is a missile in air - int size_missiles = l.getMissiles().size(); - for (int j = 0; j < size_missiles; j++) { - Missile m = l.getMissiles().get(j); - //once we find a missile in air we dont stop the war - if (m.getStatus() == Missile.Status.Launched) { - return; - } - } - } - } - //ok thats better - //you should really consider modifying the source. its fucking you up bigtime. - if (counter == size) { - /*String stats = displayStatistics(war); - logger.log(Level.INFO, "All launchers destroyed - end of war"); - logger.log(Level.INFO, "end war\n" + stats); - System.out.println(stats); - System.exit(0);*/ - } - } - } - /** - * The method displays the status of war - * @param war - * @return string of statistic - */ - private static void displayStatistics(War war) { - int total_launched_missiles = 0; - int total_destroyed_missiles = 0; - int total_missiles_hit = 0; - int total_destroyed_launchers = 0; - int total_damage = 0; - int size_launcher = war.getMissileLaunchers().size(); - int size_missiles; + - Launcher l; - Missile m; - String statistic = ""; - - for (int i = 0; i < size_launcher; i++) { - l = war.getMissileLaunchers().get(i); - if (l.isRunning() == false) { - total_destroyed_launchers++; - } - size_missiles = l.getMissiles().size(); - for (int j = 0; j < size_missiles; j++) { - m = l.getMissiles().get(j); - if (m.getStatus() != Missile.Status.Waiting) { - total_launched_missiles++; - if(m.getStatus() == Missile.Status.Hit) { - total_missiles_hit++; - total_damage += m.getDamage(); - } else if (m.getStatus() == Missile.Status.Destroyed){ - total_destroyed_missiles++; - } - } - } - } -/* statistic = "The statistics of war is: \n" - + "The number of missiles launched:\t" - + total_launched_missiles + "\n" - + "The number of missiles destroyed:\t" - + total_destroyed_missiles + "\n" - + "The number of missiles that hit:\t" - + total_missiles_hit - + "\n" + "The number launchers were destroyed:\t" - + total_destroyed_launchers + "\n" - + "The total value of damage caused:\t" - + total_damage + "\n"; - return statistic;*/ - } } \ No newline at end of file diff --git a/Java-Missle/src/view/ControlPanel.java b/Java-Missle/src/view/ControlPanel.java index 30f15df..e886f63 100644 --- a/Java-Missle/src/view/ControlPanel.java +++ b/Java-Missle/src/view/ControlPanel.java @@ -192,6 +192,16 @@ public void actionPerformed(ActionEvent e) { selectLauncherToDestroy(); } }); + + // STATISTICS BUTTON ACTION LISTENER + statisticsButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ShowStatistics(); + + } + }); + openClient.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -227,6 +237,19 @@ public void addMessageToGui(String string) { JOptionPane.showMessageDialog(null, string); } + + public void ShowStatistics() { + int[] statistics = new int[5]; + + + for (WarUIEventsListener l : allListeners) { + statistics = l.getStatistics(); + } + + + new StatisticsPopUpFrame(statistics[0], statistics[1], statistics[2], statistics[3], statistics[4]); + + } diff --git a/Java-Missle/src/view/IronDomesPanel.java b/Java-Missle/src/view/IronDomesPanel.java index a25dbf8..2f3920a 100644 --- a/Java-Missle/src/view/IronDomesPanel.java +++ b/Java-Missle/src/view/IronDomesPanel.java @@ -55,8 +55,11 @@ public void actionPerformed(ActionEvent e) { for (WarUIEventsListener l : allListeners) { l.addInterceptionToUI(missileId, id); } + fireMissileButtonPressed = false; } + setBorder(new LineBorder(new Color(0, 0, 0))); + validate(); } }); diff --git a/Java-Missle/src/view/StatisticsPopUpFrame.java b/Java-Missle/src/view/StatisticsPopUpFrame.java new file mode 100644 index 0000000..099114d --- /dev/null +++ b/Java-Missle/src/view/StatisticsPopUpFrame.java @@ -0,0 +1,98 @@ +package view; + +import javax.swing.JFrame; +import javax.swing.SpringLayout; +import javax.swing.JLabel; +import javax.swing.JTextField; + +public class StatisticsPopUpFrame extends JFrame { + + private JTextField launchedMissiles; + private JTextField destroyedMissiles; + private JTextField hitMissiles; + private JTextField destroyedLaunchers; + private JTextField totalDamage; + + public StatisticsPopUpFrame(int launched_m, int destroyed_m, int hit_m, int destroyed_l, int total_d) { + + + SpringLayout springLayout = new SpringLayout(); + getContentPane().setLayout(springLayout); + + JLabel launched_missiles = new JLabel("Total Launched Missiles"); + springLayout.putConstraint(SpringLayout.NORTH, launched_missiles, 7, SpringLayout.NORTH, getContentPane()); + getContentPane().add(launched_missiles); + + launchedMissiles = new JTextField(); + springLayout.putConstraint(SpringLayout.NORTH, launchedMissiles, -3, SpringLayout.NORTH, launched_missiles); + springLayout.putConstraint(SpringLayout.EAST, launchedMissiles, -74, SpringLayout.EAST, getContentPane()); + getContentPane().add(launchedMissiles); + launchedMissiles.setColumns(10); + + JLabel destroyed_missiles = new JLabel("Total Destroyed Missiles"); + springLayout.putConstraint(SpringLayout.NORTH, destroyed_missiles, 31, SpringLayout.SOUTH, launched_missiles); + springLayout.putConstraint(SpringLayout.WEST, launched_missiles, 0, SpringLayout.WEST, destroyed_missiles); + getContentPane().add(destroyed_missiles); + + destroyedMissiles = new JTextField(); + springLayout.putConstraint(SpringLayout.EAST, destroyed_missiles, -78, SpringLayout.WEST, destroyedMissiles); + springLayout.putConstraint(SpringLayout.NORTH, destroyedMissiles, -3, SpringLayout.NORTH, destroyed_missiles); + springLayout.putConstraint(SpringLayout.EAST, destroyedMissiles, 0, SpringLayout.EAST, launchedMissiles); + getContentPane().add(destroyedMissiles); + destroyedMissiles.setColumns(10); + + JLabel missiles_hit = new JLabel("Total Missiles Hit"); + springLayout.putConstraint(SpringLayout.WEST, missiles_hit, 0, SpringLayout.WEST, launched_missiles); + getContentPane().add(missiles_hit); + + hitMissiles = new JTextField(); + springLayout.putConstraint(SpringLayout.NORTH, hitMissiles, -3, SpringLayout.NORTH, missiles_hit); + springLayout.putConstraint(SpringLayout.EAST, hitMissiles, 0, SpringLayout.EAST, launchedMissiles); + getContentPane().add(hitMissiles); + hitMissiles.setColumns(10); + + JLabel destroyed_launchers = new JLabel("Total Destroyed Launchers"); + springLayout.putConstraint(SpringLayout.NORTH, destroyed_launchers, 162, SpringLayout.NORTH, getContentPane()); + springLayout.putConstraint(SpringLayout.SOUTH, missiles_hit, -40, SpringLayout.NORTH, destroyed_launchers); + springLayout.putConstraint(SpringLayout.WEST, destroyed_launchers, 0, SpringLayout.WEST, launched_missiles); + getContentPane().add(destroyed_launchers); + + destroyedLaunchers = new JTextField(); + springLayout.putConstraint(SpringLayout.NORTH, destroyedLaunchers, -3, SpringLayout.NORTH, destroyed_launchers); + springLayout.putConstraint(SpringLayout.EAST, destroyedLaunchers, 0, SpringLayout.EAST, launchedMissiles); + getContentPane().add(destroyedLaunchers); + destroyedLaunchers.setColumns(10); + + JLabel total_damage = new JLabel("Total Damage"); + springLayout.putConstraint(SpringLayout.NORTH, total_damage, 42, SpringLayout.SOUTH, destroyed_launchers); + springLayout.putConstraint(SpringLayout.WEST, total_damage, 0, SpringLayout.WEST, launched_missiles); + getContentPane().add(total_damage); + + totalDamage = new JTextField(); + springLayout.putConstraint(SpringLayout.NORTH, totalDamage, -3, SpringLayout.NORTH, total_damage); + springLayout.putConstraint(SpringLayout.EAST, totalDamage, 0, SpringLayout.EAST, launchedMissiles); + getContentPane().add(totalDamage); + totalDamage.setColumns(10); + + launchedMissiles.setHorizontalAlignment(JTextField.CENTER); + destroyedMissiles.setHorizontalAlignment(JTextField.CENTER); + hitMissiles.setHorizontalAlignment(JTextField.CENTER); + destroyedLaunchers.setHorizontalAlignment(JTextField.CENTER); + totalDamage.setHorizontalAlignment(JTextField.CENTER); + + launchedMissiles.setText(Integer.toString(launched_m)); + destroyedMissiles.setText(Integer.toString(destroyed_m)); + hitMissiles.setText(Integer.toString(hit_m)); + destroyedLaunchers.setText(Integer.toString(destroyed_l)); + totalDamage.setText(Integer.toString(total_d)); + + setResizable(false); + setSize(450, 300); + setLocationRelativeTo(null); + setVisible(true); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + + + + } +} diff --git a/Java-Missle/src/war/War.java b/Java-Missle/src/war/War.java index 0df12de..9958afa 100644 --- a/Java-Missle/src/war/War.java +++ b/Java-Missle/src/war/War.java @@ -244,8 +244,7 @@ public void destroyLauncher(String destructor_id, String target_id) { } public void startMissileInterception(String missileId , String ironDome) { - Destructor selected_destructor = WarUtility.getDestructorById( - ironDome, this, MISSILE); + Destructor selected_destructor = WarUtility.getDestructorById(ironDome, this, MISSILE); int destruct_time = (int) (TAKES_TIME_MIN + (Math.random() * (TAKES_TIME_MAX - TAKES_TIME_MIN + 1))); Missile target = WarUtility.getMissileById(missileId, this); @@ -257,6 +256,47 @@ public void startMissileInterception(String missileId , String ironDome) { } } + + public int[] displayStatistics() { + int statistics[] = new int[5]; + int total_launched_missiles = 0; + int total_destroyed_missiles = 0; + int total_missiles_hit = 0; + int total_destroyed_launchers = 0; + int total_damage = 0; + int size_launcher = getMissileLaunchers().size(); + int size_missiles; + + Launcher l; + Missile m; + + for (int i = 0; i < size_launcher; i++) { + l = getMissileLaunchers().get(i); + if (l.isRunning() == false) { + total_destroyed_launchers++; + } + size_missiles = l.getMissiles().size(); + for (int j = 0; j < size_missiles; j++) { + m = l.getMissiles().get(j); + if (m.getStatus() != Missile.Status.Waiting) { + total_launched_missiles++; + if(m.getStatus() == Missile.Status.Hit) { + total_missiles_hit++; + total_damage += m.getDamage(); + } else if (m.getStatus() == Missile.Status.Destroyed){ + total_destroyed_missiles++; + } + } + } + } + statistics[0] = total_launched_missiles; + statistics[1] = total_destroyed_missiles; + statistics[2] = total_missiles_hit; + statistics[3] = total_destroyed_launchers; + statistics[4] = total_damage; + + return statistics; + } public void addLauncherToClient(String id) { diff --git a/Java-Missle/src/war/controller/WarController.java b/Java-Missle/src/war/controller/WarController.java index dc60f5b..dd48bb1 100644 --- a/Java-Missle/src/war/controller/WarController.java +++ b/Java-Missle/src/war/controller/WarController.java @@ -192,4 +192,9 @@ public void connectToServer() { } + @Override + public int[] getStatistics() { + return warModel.displayStatistics(); + } + } diff --git a/Java-Missle/src/war/controller/WarUIEventsListener.java b/Java-Missle/src/war/controller/WarUIEventsListener.java index 4e0daea..eb3c6fb 100644 --- a/Java-Missle/src/war/controller/WarUIEventsListener.java +++ b/Java-Missle/src/war/controller/WarUIEventsListener.java @@ -26,4 +26,6 @@ void addMissileToProgress(String name, String dest, String damage, void addMissileThroughClient(String id, String dest, String damageT, String flyTime, String launcherId); + int[] getStatistics(); + }