-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectFourGUI.java
More file actions
144 lines (122 loc) · 5.61 KB
/
ConnectFourGUI.java
File metadata and controls
144 lines (122 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ConnectFourGUI extends JFrame implements ActionListener {
private final int rows = 6;
private final int cols = 7;
private final JButton[][] buttons = new JButton[rows][cols];
private ConnectFour game = new ConnectFour();
private String player1Name;
private String player2Name;
private int currentPlayer = 1;
public ConnectFourGUI() {
player1Name = getPlayerName("Enter name for Player 1:");
player2Name = getPlayerName("Enter name for Player 2:");
if (player1Name == null || player1Name.trim().isEmpty()) {
player1Name = "Player 1";
}
if (player2Name == null || player2Name.trim().isEmpty()) {
player2Name = "Player 2";
}
setTitle("Connect Four - Gaming Theme");
setSize(700, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(rows, cols));
setResizable(false);
getContentPane().setBackground(Color.BLACK);
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
buttons[row][col] = new JButton("");
buttons[row][col].setFont(new Font("Orbitron", Font.BOLD, 24));
buttons[row][col].setFocusPainted(false);
buttons[row][col].setForeground(Color.WHITE);
buttons[row][col].setBackground(Color.DARK_GRAY);
buttons[row][col].setOpaque(true);
buttons[row][col].setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
buttons[row][col].addActionListener(this);
add(buttons[row][col]);
}
}
setVisible(true);
}
private String getPlayerName(String message) {
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(new Color(221, 160, 221));
JLabel label = new JLabel(message);
label.setForeground(Color.GREEN);
label.setFont(new Font("Orbitron", Font.BOLD, 16)); // Set font
JTextField textField = new JTextField(10);
textField.setBackground(Color.DARK_GRAY);
textField.setForeground(Color.GREEN);
textField.setCaretColor(Color.GREEN); // Set cursor color to green
textField.setFont(new Font("Orbitron", Font.BOLD, 16));
panel.add(label, BorderLayout.NORTH);
panel.add(textField, BorderLayout.CENTER);
JButton okButton = new JButton("OK");
okButton.setBorder(BorderFactory.createLineBorder(Color.ORANGE, 2));
okButton.setBackground(Color.DARK_GRAY);
okButton.setForeground(Color.GREEN);
JButton cancelButton = new JButton("Cancel");
cancelButton.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
cancelButton.setBackground(Color.DARK_GRAY);
cancelButton.setForeground(Color.GREEN);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(new Color(221, 160, 221));
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
panel.add(buttonPanel, BorderLayout.SOUTH);
JDialog dialog = new JDialog(this, "Enter Name", true);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.getContentPane().add(panel);
dialog.pack();
dialog.setLocationRelativeTo(this);
okButton.addActionListener(e -> dialog.setVisible(false));
cancelButton.addActionListener(e -> {
textField.setText("");
dialog.setVisible(false);
});
dialog.setVisible(true);
return textField.getText();
}
@Override
public void actionPerformed(ActionEvent e) {
JButton buttonClicked = (JButton) e.getSource();
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (buttonClicked == buttons[row][col]) {
if (game.dropPiece(col, currentPlayer)) {
buttons[row][col].setText(currentPlayer == 1 ? player1Name : player2Name);
buttons[row][col].setForeground(Color.ORANGE); // Set the player's name color to orange
buttons[row][col].setEnabled(false);
int winner = game.checkWinner();
if (winner != 0) {
JOptionPane.showMessageDialog(this, (winner == 1 ? player1Name : player2Name) + " wins!", "Game Over", JOptionPane.INFORMATION_MESSAGE);
resetBoard();
} else if (winner == -1) {
JOptionPane.showMessageDialog(this, "It's a draw!", "Game Over", JOptionPane.INFORMATION_MESSAGE);
resetBoard();
} else {
currentPlayer = 3 - currentPlayer;
}
}
break;
}
}
}
}
private void resetBoard() {
game = new ConnectFour();
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
buttons[row][col].setText("");
buttons[row][col].setForeground(Color.WHITE);
buttons[row][col].setEnabled(true);
}
}
currentPlayer = 1;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(ConnectFourGUI::new);
}
}