This program is meant to eventually make the famous connect four game.
Right now i have made the logic behind the game in a separate file that does work. The problem is with the actual GUI interface.
The program successfully compiles and runs to make the starting game board. The problem occurs when I attempt to place a piece. While debugging the program all the correct information is gathered, including which panel to redraw and what color to use, but then it just does not redraw anything. Thanks for any pointers or help. I am relatively new to GUI, so the problem may be something basic I have overlooked. Thanks so much in advance.
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.*;
public class Connect4GUI extends JFrame {
private SlotPanel[][] slots;
private int currentPlayer;
private JFrame frame;
public Connect4GUI (){
frame = new JFrame();
frame.setTitle("Connect4");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1 = new JPanel();
p1.setBackground(Color.yellow);
//creates gridlayout to add panels
p1.setLayout(new GridLayout(6,7,5,5));
slots = new SlotPanel[7][6];
for (int row = 0; row < 6; row++){
for (int column = 0; column<7; column++){
//creating blank panels
slots[column][row] = new SlotPanel();
slots[column][row].setBorder(new LineBorder(Color.DARK_GRAY));
p1.add(slots[column][row]);
}
}
frame.add(p1);
frame.setSize(700, 600);
frame.setVisible(true);
currentPlayer = 1;
}
//adds listeners too the panels
public void addListener(Connect4Listener listener) {
for (int row = 0; row < 6; row++) {
for (int column = 0; column < 7; column++) {
slots[column][row].addMouseListener(listener);
}
}
}
public void set (int color, int column, int row) {
//suppose to make new drawing on panel but doesnt
//finds color by looking at array created in seperate program
//to determine who placed the last tile and where.
slots[column][row] = new SlotPanel(color);
//suppoed to repaint only this panel
slots[column][row].repaint();
//change players turn
currentPlayer = (currentPlayer%2)+1;
}
static class Connect4Listener implements MouseListener {
public Connect4GUI gui;
public Connect4 game;
public Connect4Listener (Connect4 game, Connect4GUI gui){
this.game = game;
this.gui = gui;
gui.addListener(this);
}
public void mouseClicked (MouseEvent event) {
SlotPanel label = (SlotPanel) event.getComponent();
//find column
int column = gui.getColumn(label);
//find next row cosest to bottom of picked column
int row = game.drop(column);
//determine color from seperate file
int color = game.grid[column][row];
//activate the set and thus repaint
if (row!= -1) {
gui.set(color, column, row); }
}
public void mousePressed(MouseEvent event){}
public void mouseReleased(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){}
}
public static void main(String[] args){
//seperate file that i know works
Connect4 game = new Connect4();
//these two are the problem ones
Connect4GUI gui = new Connect4GUI();
Connect4Listener listener = new Connect4Listener(game , gui);
}
}
class SlotPanel extends JPanel {
int currentPlayerColor;
int xCord;
int yCord;
public SlotPanel(){
currentPlayerColor = 0;
//create all blank tiles because currentPlayerColor = 0
xCord = 2;
yCord = 2;
}
public SlotPanel(int currentPlayer){
currentPlayerColor = currentPlayer;
xCord = 2;
yCord = 2;
//suppose to repaint new panel for players turn
repaint();
}
protected void paintComponent (Graphics g) {
super.paintComponent(g);
int height = ((int)(getHeight()))-5;
int width = ((int)(getWidth()))-5;
if (currentPlayerColor == 1) {
//player one's turns
g2.setColor(Color.RED);
g2.fillOval((int)(xCord),(int)(yCord),
(int)(width),(int)(height));
}
else if (currentPlayerColor == 2) {
//player two's turns
g2.setColor(Color.BLUE);
g2.fillOval((int)(xCord),(int)(yCord),
(int)(width),(int)(height));
}
else {
g2.setColor(Color.BLACK);
g2.drawOval((int)(xCord),(int)(yCord),
(int)(width),(int)(height));
}
}
}
Edited by: 901381 on Dec 10, 2011 10:25 AM