Skip to content
Snippets Groups Projects
Commit a34bbabf authored by Simon Peter Marneros's avatar Simon Peter Marneros
Browse files

client.java

parent 6d0d2765
No related branches found
No related tags found
No related merge requests found
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Client extends JFrame {
private static final int WIDTH = 800, HEIGHT = 600;
private static final Color BACKGROUND_COLOR = new Color(135, 206, 235); // Sky blue
private Socket socket;
private PrintWriter out;
private BufferedReader in;
private GamePanel gamePanel;
private String playerId;
public Client(String host, int port) {
setTitle("Bunny Crossy");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
showStartPage(host, port);
}
private void showStartPage(String host, int port) {
JPanel startPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(BACKGROUND_COLOR);
g.fillRect(0, 0, getWidth(), getHeight());
}
};
startPanel.setLayout(new BoxLayout(startPanel, BoxLayout.Y_AXIS));
JLabel titleLabel = new JLabel("Bunny Crossy");
titleLabel.setFont(new Font("Arial", Font.BOLD, 48));
titleLabel.setForeground(Color.WHITE);
titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JButton singlePlayerButton = createStyledButton("Single Player");
JButton multiplayerButton = createStyledButton("Multiplayer");
singlePlayerButton.addActionListener(e -> connectToServer(host, port, "SINGLEPLAYER"));
multiplayerButton.addActionListener(e -> connectToServer(host, port, "MULTIPLAYER"));
startPanel.add(Box.createVerticalGlue());
startPanel.add(titleLabel);
startPanel.add(Box.createRigidArea(new Dimension(0, 50)));
startPanel.add(singlePlayerButton);
startPanel.add(Box.createRigidArea(new Dimension(0, 20)));
startPanel.add(multiplayerButton);
startPanel.add(Box.createVerticalGlue());
add(startPanel);
setVisible(true);
}
private JButton createStyledButton(String text) {
JButton button = new JButton(text);
button.setFont(new Font("Arial", Font.BOLD, 24));
button.setAlignmentX(Component.CENTER_ALIGNMENT);
button.setMaximumSize(new Dimension(200, 50));
button.setBackground(new Color(255, 165, 0));
button.setForeground(Color.WHITE);
button.setFocusPainted(false);
button.setBorderPainted(false);
return button;
}
private void connectToServer(String host, int port, String mode) {
try {
socket = new Socket(host, port);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println(mode);
new Thread(this::receiveGameState).start();
showGamePanel();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Failed to connect to the server.", "Connection Error", JOptionPane.ERROR_MESSAGE);
}
}
private void showGamePanel() {
getContentPane().removeAll();
gamePanel = new GamePanel();
add(gamePanel);
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
sendCommand(e.getKeyCode());
}
});
revalidate();
repaint();
requestFocus();
}
private void sendCommand(int keyCode) {
String command = switch (keyCode) {
case KeyEvent.VK_UP -> "MOVE:UP";
case KeyEvent.VK_DOWN -> "MOVE:DOWN";
case KeyEvent.VK_LEFT -> "MOVE:LEFT";
case KeyEvent.VK_RIGHT -> "MOVE:RIGHT";
case KeyEvent.VK_R -> "RESTART";
default -> null;
};
if (command != null && out != null) {
out.println(playerId + ":" + command);
}
}
private void receiveGameState() {
try {
String line;
while ((line = in.readLine()) != null) {
final String finalLine = line;
if (finalLine.startsWith("ID:")) {
playerId = finalLine.substring(3);
} else {
SwingUtilities.invokeLater(() -> gamePanel.updateGameState(finalLine));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private class GamePanel extends JPanel {
private GameState gameState = new GameState();
public GamePanel() {
setBackground(Color.BLACK);
}
public void updateGameState(String state) {
gameState.fromString(state);
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
gameState.render(g);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Client("localhost", 12345));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment