public TestBattle() { super("Battle"); setSize(450, 500); Container pane = getContentPane(); pane.setBackground(Color.GREEN); pane.setLayout(new FlowLayout()); createDefenders(); createInvaders(); textD = new JTextArea("monospaced", Font.PLAIN, 14); textD.setText(ta.toString() + "\n" + in.toString()); pane.add(textD); textI = new JTextArea("monospaced", Font.PLAIN, 14); textI.setText(bo.toString() + "\n" + fs.toString()); pane.add(textI); tankShootTheBomber = new JButton("Tank Shoots Bomber"); tankShootTheBomber.addActionListener(this); pane.add(tankShootTheBomber); infantryShootTheBomber = new JButton("Infantry Shoots Bomber"); infantryShootTheBomber.addActionListener(this); pane.add(infantryShootTheBomber); tankShootTheFootsoldier = new JButton("Tank Shoots Footsoldier"); tankShootTheFootsoldier.addActionListener(this); pane.add(tankShootTheFootsoldier); infantryShootTheFootsoldier = new JButton("Infantry Shoots Footsoldier"); infantryShootTheFootsoldier.addActionListener(this); pane.add(infantryShootTheFootsoldier); bomberShootTheTank = new JButton("Bomber Shoots Tank"); bomberShootTheTank.addActionListener(this); pane.add(bomberShootTheTank); bomberShootTheInfantry = new JButton("Bomber Shoots Infantry"); bomberShootTheInfantry.addActionListener(this); pane.add(bomberShootTheInfantry); footsoldierShootTheTank = new JButton("Footsoldier Shoots Tank"); footsoldierShootTheTank.addActionListener(this); pane.add(footsoldierShootTheTank); footsoldierShootTheInfantry = new JButton("Footsoldier Shoots Infantry"); footsoldierShootTheInfantry.addActionListener(this); pane.add(footsoldierShootTheInfantry); reloadTank = new JButton("Reload Tank"); reloadTank.addActionListener(this); pane.add(reloadTank); reloadInfantry = new JButton("Reload Infantry"); reloadInfantry.addActionListener(this); pane.add(reloadInfantry); reloadBomber = new JButton("Reload Bomber"); reloadBomber.addActionListener(this); pane.add(reloadBomber); reloadFootsoldier = new JButton("Reload Footsoldier"); reloadFootsoldier.addActionListener(this); pane.add(reloadFootsoldier); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); }
public void actionPerformed(ActionEvent e) { if (e.getSource().equals(tankShootTheBomber)) { if (ta.getAmmo() > 0 && bo.isAlive()) { ta.shoot(bo); ta.setAmmo(ta.getAmmo() - 1); textI.setText(bo.toString() + "\n" + fs.toString()); textD.setText(ta.toString() + "\n" + in.toString()); if (bo.isAlive() == false && fs.isAlive() == false) { JOptionPane.showMessageDialog(null, "Invaders are dead game over"); System.exit(0); } } else JOptionPane.showMessageDialog(null, "Tank out of ammo or bomber is dead"); } else if (e.getSource().equals(infantryShootTheBomber)) { if (in.getAmmo() > 0 && bo.isAlive()) { in.shoot(bo); in.setAmmo(in.getAmmo() - 1); textI.setText(bo.toString() + "\n" + fs.toString()); textD.setText(ta.toString() + "\n" + in.toString()); if (bo.isAlive() == false && fs.isAlive() == false) { JOptionPane.showMessageDialog(null, "Invaders are dead game over"); System.exit(0); } } else JOptionPane.showMessageDialog(null, "Infantry out of ammo or bomber is dead"); } else if (e.getSource().equals(tankShootTheFootsoldier)) { if (ta.getAmmo() > 0 && fs.isAlive()) { ta.shoot(fs); ta.setAmmo(ta.getAmmo() - 1); textI.setText(bo.toString() + "\n" + fs.toString()); textD.setText(ta.toString() + "\n" + in.toString()); if (bo.isAlive() == false && fs.isAlive() == false) { JOptionPane.showMessageDialog(null, "Invaders are dead game over"); System.exit(0); } } else JOptionPane.showMessageDialog(null, "Tank out of ammo or Footsoldier is dead"); } else if (e.getSource().equals(infantryShootTheFootsoldier)) // { if (in.getAmmo() > 0 && fs.isAlive()) { in.shoot(fs); in.setAmmo(in.getAmmo() - 1); textI.setText(bo.toString() + "\n" + fs.toString()); textD.setText(ta.toString() + "\n" + in.toString()); if (bo.isAlive() == false && fs.isAlive() == false) { JOptionPane.showMessageDialog(null, "Invaders are dead game over"); System.exit(0); } } else JOptionPane.showMessageDialog(null, "Infantry out of ammo or Footsoldier is dead"); } /* else if(e.getSource().equals(reloadGood)) { good.setAmmo(10); text.setText(good.toString()+ "\n" + bad.toString() ); } else { bad.setAmmo(10); text.setText(good.toString()+ "\n" + bad.toString() ); }*/ }