Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8624105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:26:34+00:00 2026-06-12T07:26:34+00:00

Here is a Connect Four program I’m making. So far, I’m able to add

  • 0

Here is a Connect Four program I’m making. So far, I’m able to add checkers to empty (eligible) slots, alternating red and black each time. My code may be quite inefficient, as this is my first swing program. However, my only major problem is that I can’t really get the GUI to update after the empty slots are clicked on. I tried validate(), but that didn’t appear to do anything. Links to the images used in the program can be found below. Thanks so much for your help!

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;

import javax.swing.*;

public class GameFrame extends JFrame {

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frame = new GameFrame();
                    frame.setContentPane(new JLabel(new ImageIcon(getClass()
                            .getResource("675 x 588 Connect Four.png"))));
                    frame.addSlots();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */

    static GameFrame frame;
    private static Icon slotForButton;
    public static JButton slotButton;
    static private JButton[] slot = new JButton[42];
    static private String[] slotColor = new String[42];
    static boolean turn = true;
    static boolean legitClick;
    static String slotClicked;
    static int slotNum;
    static Container gamePane;

    public GameFrame() {
        setBounds(100, 100, 685, 622);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        super.setTitle("Connect Four by Joel Christophel");
    }

    void addSlots() {
        ButtonHandler bh = new ButtonHandler();
        gamePane = getContentPane();
        gamePane.setLayout(new GridLayout(6, 6));
        int counter = 0;

        for (JButton e : slot) {
            slot[counter] = makeSlot("white");
            slot[counter].setBorderPainted(false);
            slot[counter].setContentAreaFilled(false);
            slot[counter].setFocusPainted(false);
            slot[counter].setActionCommand(counter + "");
            slotColor[counter] = "white";

            slot[counter].addActionListener(bh);
            add(slot[counter]);

            counter++;
        }
    }

    static JButton makeSlot(String color) {

        if (color.equals("white")) {
            slotForButton = new ImageIcon(
                    GameFrame.class.getResource("Space.png"));
        }

        else if (color.equals("red")) {
            slotForButton = new ImageIcon(
                    GameFrame.class.getResource("Red Checker.png"));
        }

        else if (color.equals("black")) {
            slotForButton = new ImageIcon(
                    GameFrame.class.getResource("Black Checker.png"));
        }

        slotButton = new JButton(slotForButton);

        return slotButton;
    }

    private class ButtonHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            slotClicked = e.getActionCommand();

            GameFrame.legitClick(slotClicked);
        }

    }

    private static void changeTurn() {
        turn = !turn; // true is red's turn; false is black's
    }

    private static void legitClick(String slotClicked1) {
        legitClick = false;

        slotClicked = slotClicked1;
        Scanner numScan = new Scanner(slotClicked);
        slotNum = numScan.nextInt();

        try {
            if (!slotColor[slotNum + 7].equals("white")&&slotColor[slotNum].equals("white")) {
                legitClick = true;
            }
        }

        catch (ArrayIndexOutOfBoundsException t) {

            if (slotColor[slotNum].equals("white")) {
                legitClick = true;
            }
        }

        if (legitClick == true) {
            if (turn == true) {
                slot[slotNum] = makeSlot("red");
                slotColor[slotNum] = "red";
                System.out.println("Put " + slotColor[slotNum] + " checker     in slot number " + slotNum + ".");
            }

            else if (turn == false) {
                slot[slotNum] = makeSlot("black");
                slotColor[slotNum] = "black";
                System.out.println("Put " + slotColor[slotNum] + " checker      in slot number " + slotNum + ".");
            }

            gamePane.validate();
            GameFrame.changeTurn();
        }
        System.out.println(turn);
    }

}

https://i.stack.imgur.com/8cNB3.png 675 x 588 Connect Four.png

https://i.stack.imgur.com/6oX7A.png Black Checker.png

https://i.stack.imgur.com/cdF7u.png Red Checker.png

https://i.stack.imgur.com/JNT61.png Space.png

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:26:35+00:00Added an answer on June 12, 2026 at 7:26 am

    Two thingies, it appears to me that you doing wrong in your code.

    • First do check the condition that you specifying inside your
      legitClick() method, no matter when you click, it always returns false (legitClick). Hence no change will appear to the view.
    • It appears that you creating a new JButton inside a makeSlot(), which is not exactly what you want, you simply have to change the Icon for the respective JButton instead of creating a new one.

    I had modified the code a bit, inside the makeSlot() method and legitClick() method, though you have to change the logic yourself to actually make it work as you wanted, since I don’t know how checkers work 🙁

    import java.awt.Container;
    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Scanner;
    
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    
    import javax.swing.*;
    
    public class GameFrame extends JFrame {
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        frame = new GameFrame();
                        frame.setContentPane(new JLabel(new ImageIcon(getClass()
                                .getResource("/image/675 x 588 Connect Four.png"))));
                        frame.addSlots();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
    
        static GameFrame frame;
        private static Icon slotForButton;
        public static JButton slotButton;
        static private JButton[] slot = new JButton[42];
        static private String[] slotColor = new String[42];
        static boolean turn = true;
        static boolean legitClick;
        static String slotClicked;
        static int slotNum;
        static Container gamePane;
    
        public GameFrame() {
            setBounds(100, 100, 685, 622);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setResizable(false);
            super.setTitle("Connect Four by Joel Christophel");
        }
    
        void addSlots() {
            ButtonHandler bh = new ButtonHandler();
            gamePane = getContentPane();
            gamePane.setLayout(new GridLayout(6, 6));
    
            for (int counter = 0; counter < slot.length; counter++) {
                // Added by me.
                slot[counter] = new JButton();
                slot[counter].setIcon(makeSlot("white", counter));
                slot[counter].setBorderPainted(false);
                slot[counter].setContentAreaFilled(false);
                slot[counter].setFocusPainted(false);
                slot[counter].setActionCommand(counter + "");
                slotColor[counter] = "white";
    
                slot[counter].addActionListener(bh);
                add(slot[counter]);
    
                //counter++;
            }
        }
    
        static Icon makeSlot(String color, int index) {
    
            if (color.equals("white")) {
                slotForButton = new ImageIcon(
                        GameFrame.class.getResource("/image/Space.png"));
            }
    
            else if (color.equals("red")) {
                slotForButton = new ImageIcon(
                        GameFrame.class.getResource("/image/Red Checker.png"));
            }
    
            else if (color.equals("black")) {
                slotForButton = new ImageIcon(
                        GameFrame.class.getResource("/image/Black Checker.png"));
            }
    
            //slot[index].setIcon(slotForButton);
    
            return slotForButton;
        }
    
        private class ButtonHandler implements ActionListener {
    
            public void actionPerformed(ActionEvent e) {
                slotClicked = e.getActionCommand();
    
                GameFrame.legitClick(slotClicked);
            }
    
        }
    
        private static void changeTurn() {
            turn = !turn; // true is red's turn; false is black's
        }
    
        private static void legitClick(String slotClicked1) {
            legitClick = false;
    
            slotClicked = slotClicked1;
            Scanner numScan = new Scanner(slotClicked);
            slotNum = numScan.nextInt();
    
            try {
                if (slotColor[slotNum + 7].equals("white")&&slotColor[slotNum].equals("white")) {
                    legitClick = true;
                }
            }
    
            catch (ArrayIndexOutOfBoundsException t) {
    
                if (slotColor[slotNum].equals("white")) {
                    legitClick = true;
                }
            }
    
            /*
             * This variable is used to tell
             * makeSlot that the JButton at
             * this index is pressed. So
             * simply change the Icon for this
             * JButton, instead of adding a new 
             * JButton.
             */
            int index = Integer.parseInt(slotClicked1);
    
            if (legitClick == true) {
                if (turn == true) {
                    slot[index].setIcon(makeSlot("red", index));
                    slotColor[slotNum] = "red";
                    System.out.println("Put " + slotColor[slotNum] + " checker     in slot number " + slotNum + ".");
                }
    
                else if (turn == false) {
                    slot[index].setIcon(makeSlot("black", index));
                    slotColor[slotNum] = "black";
                    System.out.println("Put " + slotColor[slotNum] + " checker      in slot number " + slotNum + ".");
                }
    
                //gamePane.validate();
                GameFrame.changeTurn();
            }
            System.out.println(turn);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've coded an entire program to play Connect Four, but the algorithm for checking
I am currently working on making a Python version of the game Connect Four.
Here is my method to connect to server and get the response in MyUtilitiesActivity
Here is the code for the relevant function: def populateSubscribers(csvfile, db): conn = sqlite3.connect(db)
Here is my app: var express = require('express'), app = express.createServer(), RedisStore = require('connect-redis')(express);
I am trying to connect to SQL Server 2008 server from Java here is
I am using twython to connect to Twitter API and retrieve a query. Here
I'm following the tutorial here http://applicake.com/posts/54-integrating-facebook-connect-with-rails-applications to set up facebook connect. It almost worked,
How do I connect to MySQL database through two SSH hosts ? Here is
I'm having trouble adding Facebook Connect to my iPhone Project. Here's the directory structure:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.