Reputation: 11
I'm making a game of Connect 4 in Java using Swing painted Graphics. The problem I'm having is that if I click a button under a designated column to add another tile to that column, the one underneath it disappears. Also, the color changes from red to yellow, but then stays yellow instead of changing back to red (supposed to be red on each odd click until 41 and yellow on every even click to 42).
Here is a clip of the code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
public class Connect_4 extends JFrame {
private JPanel contentPane;
public int countClicks = 0;
public boolean check = false;
public boolean check2 = false;
public boolean flag1;
public boolean flag2;
public boolean flag3;
public boolean flag4;
public boolean flag5;
public boolean flag6;
public boolean flag7;
public int btn1Count = 0;
public int btn2Count = 0;
public int btn3Count = 0;
public int btn4Count = 0;
public int btn5Count = 0;
public int btn6Count = 0;
public int btn7Count = 0;
public boolean flag;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Connect_4 frame = new Connect_4();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
Font font = new Font("Serif", Font.PLAIN, 20);
g.setFont(font);
g.drawString("C O N N E C T 4", 34, 47);
g.setColor(Color.blue);
g.fillRect(34, 50, 140, 120);
g.setColor(Color.black);
// ~~~~row 1~~~~
g.drawRect(34, 50, 20, 20);
g.drawRect(54, 50, 20, 20);
g.drawRect(74, 50, 20, 20);
g.drawRect(94, 50, 20, 20);
g.drawRect(114, 50, 20, 20);
g.drawRect(134, 50, 20, 20);
g.drawRect(154, 50, 20, 20);
g.setColor(Color.white);
g.fillOval(36, 52, 16, 16);
g.fillOval(56, 52, 16, 16);
g.fillOval(76, 52, 16, 16);
g.fillOval(96, 52, 16, 16);
g.fillOval(116, 52, 16, 16);
g.fillOval(136, 52, 16, 16);
g.fillOval(156, 52, 16, 16);
g.setColor(Color.black);
// ~~~~row 2~~~~
g.drawRect(34, 70, 20, 20);
g.drawRect(54, 70, 20, 20);
g.drawRect(74, 70, 20, 20);
g.drawRect(94, 70, 20, 20);
g.drawRect(114, 70, 20, 20);
g.drawRect(134, 70, 20, 20);
g.drawRect(154, 70, 20, 20);
g.setColor(Color.white);
g.fillOval(36, 72, 16, 16);
g.fillOval(56, 72, 16, 16);
g.fillOval(76, 72, 16, 16);
g.fillOval(96, 72, 16, 16);
g.fillOval(116, 72, 16, 16);
g.fillOval(136, 72, 16, 16);
g.fillOval(156, 72, 16, 16);
g.setColor(Color.black);
// ~~~~row 3~~~~
g.drawRect(34, 90, 20, 20);
g.drawRect(54, 90, 20, 20);
g.drawRect(74, 90, 20, 20);
g.drawRect(94, 90, 20, 20);
g.drawRect(114, 90, 20, 20);
g.drawRect(134, 90, 20, 20);
g.drawRect(154, 90, 20, 20);
g.setColor(Color.white);
g.fillOval(36, 92, 16, 16);
g.fillOval(56, 92, 16, 16);
g.fillOval(76, 92, 16, 16);
g.fillOval(96, 92, 16, 16);
g.fillOval(116, 92, 16, 16);
g.fillOval(136, 92, 16, 16);
g.fillOval(156, 92, 16, 16);
g.setColor(Color.black);
// ~~~~row 4~~~~
g.drawRect(34, 110, 20, 20);
g.drawRect(54, 110, 20, 20);
g.drawRect(74, 110, 20, 20);
g.drawRect(94, 110, 20, 20);
g.drawRect(114, 110, 20, 20);
g.drawRect(134, 110, 20, 20);
g.drawRect(154, 110, 20, 20);
g.setColor(Color.white);
g.fillOval(36, 112, 16, 16);
g.fillOval(56, 112, 16, 16);
g.fillOval(76, 112, 16, 16);
g.fillOval(96, 112, 16, 16);
g.fillOval(116, 112, 16, 16);
g.fillOval(136, 112, 16, 16);
g.fillOval(156, 112, 16, 16);
g.setColor(Color.black);
// ~~~~row 5~~~~
g.drawRect(34, 130, 20, 20);
g.drawRect(54, 130, 20, 20);
g.drawRect(74, 130, 20, 20);
g.drawRect(94, 130, 20, 20);
g.drawRect(114, 130, 20, 20);
g.drawRect(134, 130, 20, 20);
g.drawRect(154, 130, 20, 20);
g.setColor(Color.white);
g.fillOval(36, 132, 16, 16);
g.fillOval(56, 132, 16, 16);
g.fillOval(76, 132, 16, 16);
g.fillOval(96, 132, 16, 16);
g.fillOval(116, 132, 16, 16);
g.fillOval(136, 132, 16, 16);
g.fillOval(156, 132, 16, 16);
g.setColor(Color.black);
// ~~~~row 6~~~~
g.drawRect(34, 150, 20, 20);
g.drawRect(54, 150, 20, 20);
g.drawRect(74, 150, 20, 20);
g.drawRect(94, 150, 20, 20);
g.drawRect(114, 150, 20, 20);
g.drawRect(134, 150, 20, 20);
g.drawRect(154, 150, 20, 20);
g.setColor(Color.white);
g.fillOval(36, 152, 16, 16);
g.fillOval(56, 152, 16, 16);
g.fillOval(76, 152, 16, 16);
g.fillOval(96, 152, 16, 16);
g.fillOval(116, 152, 16, 16);
g.fillOval(136, 152, 16, 16);
g.fillOval(156, 152, 16, 16);
g.setColor(Color.black);
g.drawLine(174, 170, 174, 187);
g.drawLine(34, 170, 34, 187);
g.drawLine(164, 187, 184, 187);
g.drawLine(24, 187, 44, 187);
if (flag1 == true && check == true && btn1Count == 1)
{
g.setColor(Color.red);
g.fillOval(36, 152, 16, 16);
}
if (flag1 == true && check2 == true && btn1Count == 1)
{
g.setColor(Color.yellow);
g.fillOval(36, 152, 16, 16);
}
if (flag1 == true && check == true && btn1Count == 2)
{
g.setColor(Color.red);
g.fillOval(36, 132, 16, 16);
}
if (flag1 == true && check2 == true && btn1Count == 2)
{
g.setColor(Color.yellow);
g.fillOval(36, 132, 16, 16);
}
if (flag1 == true && check == true && btn1Count == 3)
{
g.setColor(Color.red);
g.fillOval(36, 112, 16, 16);
}
if (flag1 == true && check2 == true && btn1Count == 3)
{
g.setColor(Color.yellow);
g.fillOval(36, 112, 16, 16);
}
if (flag1 == true && check == true && btn1Count == 4)
{
g.setColor(Color.red);
g.fillOval(36, 92, 16, 16);
}
if (flag1 == true && check2 == true && btn1Count == 4)
{
g.setColor(Color.yellow);
g.fillOval(36, 92, 16, 16);
}
if (flag1 == true && check == true && btn1Count == 5)
{
g.setColor(Color.red);
g.fillOval(36, 72, 16, 16);
}
if (flag1 == true && check2 == true && btn1Count == 5)
{
g.setColor(Color.yellow);
g.fillOval(36, 72, 16, 16);
}
if (flag1 == true && check == true && btn1Count == 6)
{
g.setColor(Color.red);
g.fillOval(36, 52, 16, 16);
}
if (flag1 == true && check2 == true && btn1Count == 6)
{
g.setColor(Color.yellow);
g.fillOval(36, 52, 16, 16);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (flag2 == true && check == true && btn2Count == 1)
{
g.setColor(Color.red);
g.fillOval(56, 152, 16, 16);
}
if (flag2 == true && check2 == true && btn2Count == 1)
{
g.setColor(Color.yellow);
g.fillOval(56, 152, 16, 16);
}
if (flag2 == true && check == true && btn2Count == 2)
{
g.setColor(Color.red);
g.fillOval(56, 132, 16, 16);
}
if (flag2 == true && check2 == true && btn2Count == 2)
{
g.setColor(Color.yellow);
g.fillOval(56, 132, 16, 16);
}
if (flag2 == true && check == true && btn2Count == 3)
{
g.setColor(Color.red);
g.fillOval(56, 112, 16, 16);
}
if (flag2 == true && check2 == true && btn2Count == 3)
{
g.setColor(Color.yellow);
g.fillOval(56, 112, 16, 16);
}
if (flag2 == true && check == true && btn2Count == 4)
{
g.setColor(Color.red);
g.fillOval(56, 92, 16, 16);
}
if (flag2 == true && check2 == true && btn2Count == 4)
{
g.setColor(Color.yellow);
g.fillOval(56, 92, 16, 16);
}
if (flag2 == true && check == true && btn2Count == 5)
{
g.setColor(Color.red);
g.fillOval(56, 72, 16, 16);
}
if (flag2 == true && check2 == true && btn2Count == 5)
{
g.setColor(Color.yellow);
g.fillOval(56, 72, 16, 16);
}
if (flag2 == true && check == true && btn2Count == 6)
{
g.setColor(Color.red);
g.fillOval(56, 52, 16, 16);
}
if (flag2 == true && check2 == true && btn2Count == 6)
{
g.setColor(Color.yellow);
g.fillOval(56, 52, 16, 16);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (flag3 == true && check == true && btn3Count == 1)
{
g.setColor(Color.red);
g.fillOval(76, 152, 16, 16);
}
if (flag3 == true && check2 == true && btn3Count == 1)
{
g.setColor(Color.yellow);
g.fillOval(76, 152, 16, 16);
}
if (flag3 == true && check == true && btn3Count == 2)
{
g.setColor(Color.red);
g.fillOval(76, 132, 16, 16);
}
if (flag3 == true && check2 == true && btn3Count == 2)
{
g.setColor(Color.yellow);
g.fillOval(76, 132, 16, 16);
}
if (flag3 == true && check == true && btn3Count == 3)
{
g.setColor(Color.red);
g.fillOval(76, 112, 16, 16);
}
if (flag3 == true && check2 == true && btn3Count == 3)
{
g.setColor(Color.yellow);
g.fillOval(76, 112, 16, 16);
}
if (flag3 == true && check == true && btn3Count == 4)
{
g.setColor(Color.red);
g.fillOval(76, 92, 16, 16);
}
if (flag3 == true && check2 == true && btn3Count == 4)
{
g.setColor(Color.yellow);
g.fillOval(76, 92, 16, 16);
}
if (flag3 == true && check == true && btn3Count == 5)
{
g.setColor(Color.red);
g.fillOval(76, 72, 16, 16);
}
if (flag3 == true && check2 == true && btn3Count == 5)
{
g.setColor(Color.yellow);
g.fillOval(76, 72, 16, 16);
}
if (flag3 == true && check == true && btn3Count == 6)
{
g.setColor(Color.red);
g.fillOval(76, 52, 16, 16);
}
if (flag3 == true && check2 == true && btn3Count == 6)
{
g.setColor(Color.yellow);
g.fillOval(76, 52, 16, 16);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (flag4 == true && check == true && btn4Count == 1)
{
g.setColor(Color.red);
g.fillOval(96, 152, 16, 16);
}
if (flag4 == true && check2 == true && btn4Count == 1)
{
g.setColor(Color.yellow);
g.fillOval(96, 152, 16, 16);
}
if (flag4 == true && check == true && btn4Count == 2)
{
g.setColor(Color.red);
g.fillOval(96, 132, 16, 16);
}
if (flag4 == true && check2 == true && btn4Count == 2)
{
g.setColor(Color.yellow);
g.fillOval(96, 132, 16, 16);
}
if (flag4 == true && check == true && btn4Count == 3)
{
g.setColor(Color.red);
g.fillOval(96, 112, 16, 16);
}
if (flag4 == true && check2 == true && btn4Count == 3)
{
g.setColor(Color.yellow);
g.fillOval(96, 112, 16, 16);
}
if (flag4 == true && check == true && btn4Count == 4)
{
g.setColor(Color.red);
g.fillOval(96, 92, 16, 16);
}
if (flag4 == true && check2 == true && btn4Count == 4)
{
g.setColor(Color.yellow);
g.fillOval(96, 92, 16, 16);
}
if (flag4 == true && check == true && btn4Count == 5)
{
g.setColor(Color.red);
g.fillOval(96, 72, 16, 16);
}
if (flag4 == true && check2 == true && btn4Count == 5)
{
g.setColor(Color.yellow);
g.fillOval(96, 72, 16, 16);
}
if (flag4 == true && check == true && btn4Count == 6)
{
g.setColor(Color.red);
g.fillOval(96, 52, 16, 16);
}
if (flag4 == true && check2 == true && btn4Count == 6)
{
g.setColor(Color.yellow);
g.fillOval(96, 52, 16, 16);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (flag5 == true && check == true && btn5Count == 1)
{
g.setColor(Color.red);
g.fillOval(116, 152, 16, 16);
}
if (flag5 == true && check2 == true && btn5Count == 1)
{
g.setColor(Color.yellow);
g.fillOval(116, 152, 16, 16);
}
if (flag5 == true && check == true && btn5Count == 2)
{
g.setColor(Color.red);
g.fillOval(116, 132, 16, 16);
}
if (flag5 == true && check2 == true && btn5Count == 2)
{
g.setColor(Color.yellow);
g.fillOval(116, 132, 16, 16);
}
if (flag5 == true && check == true && btn5Count == 3)
{
g.setColor(Color.red);
g.fillOval(116, 112, 16, 16);
}
if (flag5 == true && check2 == true && btn5Count == 3)
{
g.setColor(Color.yellow);
g.fillOval(116, 112, 16, 16);
}
if (flag5 == true && check == true && btn5Count == 4)
{
g.setColor(Color.red);
g.fillOval(116, 92, 16, 16);
}
if (flag5 == true && check2 == true && btn5Count == 4)
{
g.setColor(Color.yellow);
g.fillOval(116, 92, 16, 16);
}
if (flag5 == true && check == true && btn5Count == 5)
{
g.setColor(Color.red);
g.fillOval(116, 72, 16, 16);
}
if (flag5 == true && check2 == true && btn5Count == 5)
{
g.setColor(Color.yellow);
g.fillOval(116, 72, 16, 16);
}
if (flag5 == true && check == true && btn5Count == 6)
{
g.setColor(Color.red);
g.fillOval(116, 52, 16, 16);
}
if (flag5 == true && check2 == true && btn5Count == 6)
{
g.setColor(Color.yellow);
g.fillOval(116, 52, 16, 16);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (flag6 == true && check == true && btn6Count == 1)
{
g.setColor(Color.red);
g.fillOval(136, 152, 16, 16);
}
if (flag6 == true && check2 == true && btn6Count == 1)
{
g.setColor(Color.yellow);
g.fillOval(136, 152, 16, 16);
}
if (flag6 == true && check == true && btn6Count == 2)
{
g.setColor(Color.red);
g.fillOval(136, 132, 16, 16);
}
if (flag6 == true && check2 == true && btn6Count == 2)
{
g.setColor(Color.yellow);
g.fillOval(136, 132, 16, 16);
}
if (flag6 == true && check == true && btn6Count == 3)
{
g.setColor(Color.red);
g.fillOval(136, 112, 16, 16);
}
if (flag6 == true && check2 == true && btn6Count == 3)
{
g.setColor(Color.yellow);
g.fillOval(136, 112, 16, 16);
}
if (flag6 == true && check == true && btn6Count == 4)
{
g.setColor(Color.red);
g.fillOval(136, 92, 16, 16);
}
if (flag6 == true && check2 == true && btn6Count == 4)
{
g.setColor(Color.yellow);
g.fillOval(136, 92, 16, 16);
}
if (flag6 == true && check == true && btn6Count == 5)
{
g.setColor(Color.red);
g.fillOval(136, 72, 16, 16);
}
if (flag6 == true && check2 == true && btn6Count == 5)
{
g.setColor(Color.yellow);
g.fillOval(136, 72, 16, 16);
}
if (flag6 == true && check == true && btn6Count == 6)
{
g.setColor(Color.red);
g.fillOval(136, 52, 16, 16);
}
if (flag6 == true && check2 == true && btn6Count == 6)
{
g.setColor(Color.yellow);
g.fillOval(136, 52, 16, 16);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (flag7 == true && check == true && btn7Count == 1)
{
g.setColor(Color.red);
g.fillOval(156, 152, 16, 16);
}
if (flag7 == true && check2 == true && btn7Count == 1)
{
g.setColor(Color.yellow);
g.fillOval(156, 152, 16, 16);
}
if (flag7 == true && check == true && btn7Count == 2)
{
g.setColor(Color.red);
g.fillOval(156, 132, 16, 16);
}
if (flag7 == true && check2 == true && btn7Count == 2)
{
g.setColor(Color.yellow);
g.fillOval(156, 132, 16, 16);
}
if (flag7 == true && check == true && btn7Count == 3)
{
g.setColor(Color.red);
g.fillOval(156, 112, 16, 16);
}
if (flag7 == true && check2 == true && btn7Count == 3)
{
g.setColor(Color.yellow);
g.fillOval(156, 112, 16, 16);
}
if (flag7 == true && check == true && btn7Count == 4)
{
g.setColor(Color.red);
g.fillOval(156, 92, 16, 16);
}
if (flag7 == true && check2 == true && btn7Count == 4)
{
g.setColor(Color.yellow);
g.fillOval(156, 92, 16, 16);
}
if (flag7 == true && check == true && btn7Count == 5)
{
g.setColor(Color.red);
g.fillOval(156, 72, 16, 16);
}
if (flag7 == true && check2 == true && btn7Count == 5)
{
g.setColor(Color.yellow);
g.fillOval(156, 72, 16, 16);
}
if (flag7 == true && check == true && btn7Count == 6)
{
g.setColor(Color.red);
g.fillOval(156, 52, 16, 16);
}
if (flag7 == true && check2 == true && btn7Count == 6)
{
g.setColor(Color.yellow);
g.fillOval(156, 52, 16, 16);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
public Connect_4() {
setTitle("Connect 4");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 210, 220);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
// getContentPane().setBackground(Color.RED);
setResizable(false);
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 137));
JPanel panel = new JPanel();
// panel.setBackground(Color.BLUE);
contentPane.add(panel, BorderLayout.CENTER);
JButton btn1 = new JButton();
btn1.setPreferredSize(new Dimension(15, 10));
panel.add(btn1);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
countClicks++;
btn1Count++;
flag = true;
flag1 = true;
switch (countClicks) {
case 1:
case 3:
case 5:
case 7:
case 9:
case 11:
case 13:
case 15:
case 17:
case 19:
case 21:
case 23:
case 25:
case 27:
case 29:
case 31:
case 33:
case 35:
case 37:
case 39:
case 41:
check = true;
repaint();
break;
case 2:
case 4:
case 6:
case 8:
case 10:
case 12:
case 14:
case 16:
case 18:
case 20:
case 22:
case 24:
case 26:
case 28:
case 30:
case 32:
case 34:
case 36:
case 38:
case 40:
case 42:
check2 = true;
repaint();
break;
}
}
});
I do not know why the previous tile in a column disappears nor how to make it stop. Is the problem repaint()? Is it the if statements in the paint method? Please help.
Upvotes: 0
Views: 248
Reputation: 1209
Paint()
- this method holds instructions to paint this component. Actually, in Swing, you should change paintComponent()
instead of paint()
, as paint calls paintBorder()
, paintComponent()
and paintChildren()
. You shouldn't call this method directly, you should call repaint() instead.
repaint()
- this method can't be overridden. It controls the update() -> paint()
cycle. You should call this method to get a component to repaint itself. If you have done anything to change the look of the component, but not it's size ( like changing color, animating, etc. ) then call this method.
validate()
- This tells the component to lay itself out again and repaint itself. If you have done anything to change the size of the component or any of it's children(adding, removing, resizing children), you should call this method... I think that calling revalidate()
is preferred to calling validate()
in Swing, though...
update()
- This method is in charge of clearing the component and calling paint()
. Again, you should call repaint() instead of calling this method directly... If you need to do fast updates in animation you should override this method to just call the paint() method...
updateUI()
- Call this method if you have changed the pluggable look & feel for a component after it has been made visible.
Note: The way you have used switch case in your program is not a good implementation, use a variable(counter) and increment as user clicks and then use if/while condition for further implementation.
Upvotes: 1