Hi,

I am making the Connect Four game for my school computer science class. I have made the layout and most of the GUI, but I need help with a big part of the game.

I dont know how to keep switching the colors of the dots. I am keeping track of whos turn it is, but I cannot set the color of a "game piece" and keep it as that. When I repaint when a button is pressed, it changes number that shows which player is moving, and that changes all prevous "game piece" colors.

Ex.
if(column1pieces == 1)
{

if (colorofpiece % 2 == 1)
{
Color column1row1 = Color.black;
g.setColor(column1row1);
}
else
{
Color column1row1 = Color.red;
g.setColor(column1row1);
}

g.fillOval(216,602,66,64);

}

If it is player 1's starting the game, 'colorofpiece' will be 1 which means that the color of the oval will be black. When I have player 2 click the second column, it changes 'colorofpiece' which also changes the color of the first game piece! I was just wondering if there was a way where the color of that certain piece is only changeable once and then cannot be changed again when repainting.

Thanks,
Ricky

Ps. Here is my full code so far.














import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Type;

import javax.swing.*;

public class ConnectFourInit extends JFrame implements ActionListener
{
public JButton column1, column2, column3, column4, column5, column6, column7;

int column1pieces = 0;
int column2pieces = 0;
int column3pieces = 0;
int column4pieces = 0;
int column5pieces = 0;
int column6pieces = 0;
int column7pieces = 0;
int colorofpiece = 0;
int gamepiecesleftONE = 21;
int gamepiecesleftTWO = 21;
Color column1row1;
Color column2row1;

private ImageIcon grid;

public ConnectFourInit ()
{
setLayout(null);
setSize(1000, 700);
setTitle("Connect Four");
setBackground(Color.BLUE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

column1 = new JButton("Place");
column1.addActionListener(this);
column1.setBounds(209, 112, 70, 50);
add(column1);

column2 = new JButton("Place");
column2.addActionListener(this);
column2.setBounds(293, 112, 70, 50);
add(column2);

column3 = new JButton("Place");
column3.addActionListener(this);
column3.setBounds(377, 112, 70, 50);
add(column3);

column4 = new JButton("Place");
column4.addActionListener(this);
column4.setBounds(461, 112, 70, 50);
add(column4);

column5 = new JButton("Place");
column5.addActionListener(this);
column5.setBounds(545, 112, 70, 50);
add(column5);

column6 = new JButton("Place");
column6.addActionListener(this);
column6.setBounds(629, 112, 70, 50);
add(column6);

column7 = new JButton("Place");
column7.addActionListener(this);
column7.setBounds(713, 112, 70, 50);
add(column7);
}


public void paint(Graphics g)
{
Font One = new Font("Arial", Font.BOLD, 100);
Font Two = new Font("Arial", Font.BOLD, 37);
Font Three = new Font("Arial", Font.BOLD, 25);
Font Four = new Font("Arial", Font.BOLD, 18);
Font Five = new Font("Arial", Font.BOLD, 35);
Font Six = new Font("Arial", Font.BOLD, 30);

g.setFont(One);
g.setColor(Color.blue);
g.drawString("Connect 4", 260, 120);

g.setColor(Color.black);
g.fillRect(20, 202, 163, 483);
g.fillRect(820, 202, 163, 483);

g.setColor(Color.white);
g.fillRect(22, 204, 159, 479);
g.fillRect(822, 204, 159, 479);

g.setFont(Two);
g.setColor(Color.blue);
g.drawString("Player 1", 29, 245);
g.drawString("Player 2", 828, 245);

g.setFont(Three);

g.setColor(Color.black);
g.fillRect(20, 263, 161, 2);
g.fillRect(822, 263, 161, 2);

g.fillRect(20, 380, 161, 2);
g.fillRect(822, 380, 161, 2);

g.fillRect(20, 547, 161, 2);
g.fillRect(822, 547, 161, 2);

g.fillRect(20, 600, 161, 2);
g.fillRect(822, 600, 161, 2);


g.drawString("You ", 45, 640);
g.drawString("are", 51, 660);

g.drawString("You", 846, 640);
g.drawString("are", 852, 660);

g.fillOval(108,610,66,66);
g.setColor(Color.red);
g.fillOval(909,610,66,66);

g.setFont(Four);
g.setColor(Color.black);
g.drawString("You have " + gamepiecesleftONE, 47, 570);
g.drawString("game pieces left", 29, 590);

g.drawString("You have " + gamepiecesleftTWO, 847, 570);
g.drawString("game pieces left", 829, 590);


g.setFont(Five);
g.setColor(Color.red);

if (colorofpiece % 2 == 1)
{
g.drawString("IT IS", 867, 302);
g.drawString("YOUR", 852, 335);
g.drawString("TURN", 853, 368);
}
else
{
g.drawString("IT IS", 67, 302);
g.drawString("YOUR", 52, 335);
g.drawString("TURN", 53, 368);
}

g.setFont(Six);

grid = new ImageIcon("Connect4Grid5.jpg");
grid.paintIcon(this, g, 200, 200);

g.setColor(Color.white);








if(column1pieces == 1)
{

if (colorofpiece % 2 == 1)
{
Color column1row1 = Color.black;
g.setColor(column1row1);
}
else
{
Color column1row1 = Color.red;
g.setColor(column1row1);
}

g.fillOval(216,602,66,64);

}



if(column2pieces == 1)
{
if (colorofpiece % 2 == 1)
{
Color column2row1 = Color.black;
g.setColor(column2row1);


}
else
{
Color column2row1 = Color.red;
g.setColor(column2row1);
}

g.fillOval(300,602,66,64);
}















if(column1pieces == 7 || column2pieces == 7|| column3pieces == 7|| column4pieces == 7|| column5pieces == 7|| column6pieces == 7|| column7pieces == 7)
{
if (colorofpiece % 2 == 1)
{
g.drawString("Please try", 830, 450);
g.drawString("another", 844, 480);
g.drawString("column", 849, 510);
}
else
{
g.drawString("Please try", 30, 450);
g.drawString("another", 48, 480);
g.drawString("column", 49, 510);
}
}

}








public void actionPerformed(ActionEvent e)
{
if (e.getSource() == column1)
{
column1pieces +=1;
colorofpiece +=1;
if(column1pieces > 6 )
{
column1pieces = 7;
colorofpiece -=1;
}
}

if (e.getSource() == column2)
{
column2pieces +=1;
colorofpiece +=1;
if(column2pieces > 6 )
{
column2pieces = 7;
colorofpiece -=1;
}
}

if (e.getSource() == column3)
{
column3pieces +=1;
colorofpiece +=1;
if(column3pieces > 6 )
{
column3pieces = 7;
colorofpiece -=1;
}
}

if (e.getSource() == column4)
{
column4pieces +=1;
colorofpiece +=1;
if(column4pieces > 6 )
{
column4pieces = 7;
colorofpiece -=1;
}
}

if (e.getSource() == column5)
{
column5pieces +=1;
colorofpiece +=1;
if(column5pieces > 6 )
{
column5pieces = 7;
colorofpiece -=1;
}
}

if (e.getSource() == column6)
{
column6pieces +=1;
colorofpiece +=1;
if(column6pieces > 6 )
{
column6pieces = 7;
colorofpiece -=1;
}
}

if (e.getSource() == column7)
{
column7pieces +=1;
colorofpiece +=1;
if(column7pieces > 6 )
{
column7pieces = 7;
colorofpiece -=1;
}
}
repaint();
}

}