import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.geom.Rectangle2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class GUIMain extends JPanel { private World world; private int victory = -1; private int currentPlayer = 0; private int numDead = 0; private JPanel inputPanel; public GUIMain(World world) { this.world = world; initGame(); repaint(); } public void reset() { numDead = 0; currentPlayer = 0; victory = -1; repaint(); } public void initGame() { inputPanel = new JPanel(); inputPanel.setBackground(Color.blue); inputPanel.setLayout(new GridLayout(4, 2)); final JLabel playerLabel = new JLabel("Player 1:"); playerLabel.setForeground(Color.white); inputPanel.add(playerLabel); inputPanel.add(new JLabel("")); JLabel angleLabel = new JLabel("Angle: "); angleLabel.setForeground(Color.white); inputPanel.add(angleLabel); final JTextField angleText = new JTextField(); inputPanel.add(angleText); JLabel velocityLabel = new JLabel("Velocity: "); velocityLabel.setForeground(Color.white); inputPanel.add(velocityLabel); final JTextField velocityText = new JTextField(); inputPanel.add(velocityText); inputPanel.add(new JLabel("")); JButton fireButton = new JButton("Fire!"); fireButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int angle = Integer.parseInt(angleText.getText()); int velocity = Integer.parseInt(velocityText.getText()); int result = world.doShot(currentPlayer, velocity, angle); if (result >= 0) { if (!world.isDead(result)) { world.setDead(result); numDead++; } } if (numDead == world.getNumPlayers() - 1) { int alive = 0; for (int i = 1; i < world.getNumPlayers(); i++) { if (!world.isDead(i)) { alive = i; break; } } victory = alive + 1; angleText.setText(""); velocityText.setText(""); angleText.grabFocus(); playerLabel.setText("Player 1: "); } else { currentPlayer = (currentPlayer + 1) % world.getNumPlayers(); while (world.isDead(currentPlayer)) { currentPlayer = (currentPlayer + 1) % world.getNumPlayers(); } playerLabel.setText("Player " + (currentPlayer + 1) + ":"); angleText.setText(""); velocityText.setText(""); angleText.grabFocus(); } repaint(); } }); inputPanel.add(fireButton); this.add(inputPanel); } public void paint(Graphics g) { Graphics2D g2d = (Graphics2D)g; int width = world.getWidth(), height = world.getHeight(), playerSize = world.getPlayerSize(); g2d.setBackground(Color.blue); g2d.clearRect(0, 0, width, height); int[][] land = world.getLandPolygon(); g2d.setColor(Color.green); g2d.fillPolygon(land[0], land[1], land[1].length); g2d.setColor(Color.red); int[][] lastShot = world.getLastShot(); if (lastShot != null) { g2d.drawPolyline(lastShot[0], lastShot[1], lastShot[0].length); } for (int k = 0; k < world.getNumPlayers(); k++) { if (world.isDead(k)) { g2d.setColor(Color.black); } else { g2d.setColor(Color.orange); } g2d.fillRect(world.getPlayerLocation(k) - playerSize, height - (world.getAltitude(world.getPlayerLocation(k)) + 2 * playerSize), playerSize * 2, playerSize * 2); } if (victory != -1) { g2d.setColor(Color.red); g2d.setFont(g2d.getFont().deriveFont(24.0f)); String message = "Congratulations Player: " + victory + " You Win!!"; Rectangle2D rect = g2d.getFontMetrics().getStringBounds(message, g2d); g2d.drawString(message, (int)(width / 2 - rect.getWidth() / 2), (int)(height / 2 - rect.getHeight() / 2)); } inputPanel.repaint(); } }