Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Connect Four GUI Help needed

  1. #1
    Junior Member Hobbs's Avatar
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Connect Four GUI Help needed

    Hi, my name is Hobbs and I'm obviously new to these forums. Im a beginner Java student who right now is in a bit of trouble. For my final project I am suppose to make a connect four game complete with GUI. Unfortunately we were not taught GUI in the class and the only chapter in the book tells us how to make a window with buttons. I think the rest of my code is good but I have no idea how to make the game board to show pieces and have it work with my array and such. Any help is appreciated.

    Link to Code
    import java.awt.*; import java.awt.event.*; import javax.swing.*; import java - Pastebin.com


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Connect Four GUI Help needed

    You'll want to post your pertinent code here in the forum for all to see as many will not click on links. If you do post this, please be sure to use [code] [/code] tags per the forum FAQ.

  3. #3
    Junior Member Hobbs's Avatar
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connect Four GUI Help needed

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.MouseInputListener;
     
    public class Project {
     
      public static void main(String args[]){
       boolean continuePlay = true;
       int player1Score = 0;
       int player2Score = 0;
     
     
       JFrame game = new Connect4(); //call the gameboard
      }
     }
     
    class Connect4 extends JFrame
    {
     final static int NUMROWS = 6;
     final static int NUMCOLUMNS = 7;
     final int EMPTY = 0;
     static int currentPlayer = 0;
     int playerNumber = 1;
     static int[][] gameboard = new int[NUMCOLUMNS][NUMROWS];
     int x;
     
     //create the game board GUI
     private JFrame board;
     
     //create a place in the game board for the pieces
     private JLabel[][] grid;
     
     //Constructor (Make the Game Board)
     public Connect4()
     {
     
      //set the game board to blank
      for ( int y = 0 ; y < NUMROWS ; y++ ) 
      {
       for ( int x = 0 ; x < NUMCOLUMNS ; x++)
       {
        gameboard[x][y] = EMPTY ;
       }
      }
     
      //create a GUI
      board = new JFrame("Connect 4");
      JPanel panel = (JPanel) board.getContentPane();
      board.setContentPane(panel);
     
      // 10 x 10 for each disk and then 10 x 10 for the buttons
      board.setSize(750 , 750);
      board.setVisible(true);
      //panel.setLayout(new GridLayout( (NUMROWS + 1),NUMCOLUMNS));
      grid = new JLabel[NUMCOLUMNS][NUMROWS];
     
      //create the individual Jlabels
      for (int y = 0 ; y < NUMROWS ; y++)
      {
       for ( int x = 0 ; x < NUMCOLUMNS ; x++)
       {
        grid[x][y] = new JLabel();
       // panel.add(grid[x][y]);
       }
      }
     
      //create buttons above the columns
      JButton jbtcol0 = new JButton("Column 1");
      JButton jbtcol1 = new JButton("Column 2");
      JButton jbtcol2 = new JButton("Column 3");
      JButton jbtcol3 = new JButton("Column 4");
      JButton jbtcol4 = new JButton("Column 5");
      JButton jbtcol5 = new JButton("Column 6");
      JButton jbtcol6 = new JButton("Column 7");
     
      //add buttons to the board
      JPanel buttonPanel = new JPanel();
      buttonPanel.add(jbtcol0);
      buttonPanel.add(jbtcol1);
      buttonPanel.add(jbtcol2);
      buttonPanel.add(jbtcol3);
      buttonPanel.add(jbtcol4);
      buttonPanel.add(jbtcol5);
      buttonPanel.add(jbtcol6);
     
      //add panel to the game board
      board.add(buttonPanel);
     
      //Register Listeners for the buttons
      Column_1ListenerClass listener0 = new Column_1ListenerClass();
      Column_2ListenerClass listener1 = new Column_2ListenerClass();
      Column_3ListenerClass listener2 = new Column_3ListenerClass();
      Column_4ListenerClass listener3 = new Column_4ListenerClass();
      Column_5ListenerClass listener4 = new Column_5ListenerClass();
      Column_6ListenerClass listener5 = new Column_6ListenerClass();
      Column_7ListenerClass listener6 = new Column_7ListenerClass();
      jbtcol0.addActionListener(listener0);
      jbtcol1.addActionListener(listener1);
      jbtcol2.addActionListener(listener2);
      jbtcol3.addActionListener(listener3);
      jbtcol4.addActionListener(listener4);
      jbtcol5.addActionListener(listener5);
      jbtcol6.addActionListener(listener6);
     
     }
     
     //reset the game board
     public void reset(){
      for( int y = 0 ; y < NUMROWS ; y++)
      {
       for ( int x = 0 ; x < NUMCOLUMNS ; x++)
       {
        //Game logic to blank
        gameboard[x][y] = EMPTY;
     
        //GUI update to blank
       }
      }
     }
     
      public static void changePlayer()
      {
       if (currentPlayer == 0) 
         currentPlayer = 1;
       else if (currentPlayer == 1)
         currentPlayer = 2;
       else currentPlayer = 1;
      }
     
      public static boolean canDropDisk ( int x )
      {
       if ( gameboard[x][0] == 0) 
         return true;
       else 
         return false;
      }
     
      public int diskLands(int x)
      {
       for ( int y = NUMROWS ; y <= 0 ; y-- )
       {
        if (gameboard[y][x] == 0) 
          return y;
        }
     
       return 0;
       }
     
      public static void dropDisk(int x)
      {
       //update array
       for ( int y = NUMROWS ; y <=0 ; y--)
       {
        if(gameboard[y][x] == 0)
        {
         gameboard[y][x] = currentPlayer;
        }
       }
       changePlayer();
      }
     
      public boolean didWin()
      {
       int numMatches;
     
       // check horizontally
       for ( int y = 0 ; y < NUMROWS ; y++)
       {
        numMatches = 0;
        for ( int x = 0 ; x + 1 < NUMCOLUMNS ; x++)
        {
         if ( ( gameboard[y][x] == gameboard[y][x+1]) && (gameboard[y][x] > 0) ) numMatches++;
         if (numMatches == 4)
           return true;
        }
       }
     
       //check vertically
       for ( int x = 0 ; x < NUMCOLUMNS ; x++)
       {
        numMatches = 0;
        for ( int y = 0 ; y + 1 < NUMROWS ; y++)
        {
        if ( ( gameboard[y][x] == gameboard[y+1][x]) && (gameboard[y][x] > 0 )) numMatches++;
        if ( numMatches == 4) 
          return true;
        }
       }
     
       //check right slanted diagonal
       for ( int y = 3 ; y < NUMROWS ; y++)
       {
        numMatches = 0;
        for ( int x = 0 ; x < 4 ; x++)
        {
         if ((gameboard[y][x] == gameboard[y+1][x+1]) && (gameboard [y][x] == gameboard[y+2][x+2]) && (gameboard[y][x] == gameboard[y+3][x+3]) && (gameboard[y][x] > 0)) 
           return true;
        }
       }
     
       //check left slanted diagonal
       for ( int y = 3 ; y < NUMROWS ; y ++)
       {
        numMatches = 0;
        for ( int x = 3 ; x < NUMCOLUMNS ; x++)
        {
         if ((gameboard[y][x] == gameboard[y-1][x-1]) && (gameboard [y][x] == gameboard[y-2][x-2]) && (gameboard[y][x] == gameboard[y-3][x-3]) && (gameboard[y][x] > 0)) 
           return true;
        }
       }
     
       return false;
     
      }
     
      public int whoWon()
      {
       int numMatches;
       int matchingNumber = 0;
     
       // check horizontally
       for ( int y = 0 ; y < NUMROWS ; y++)
       {
        numMatches = 0;
        for ( int x = 0 ; x + 1 < NUMCOLUMNS ; x++){
         if ( ( gameboard[y][x] == gameboard[y][x+1]) && (gameboard[y][x] > 0) )
         {
          numMatches++;
          matchingNumber = gameboard[y][x];
         }
         if (numMatches == 4) return matchingNumber;
        }
       }
     
       //check vertically
       for ( int x = 0 ; x < NUMCOLUMNS ; x++)
       {
        numMatches = 0;
        for ( int y = 0 ; y + 1 < NUMROWS ; y++)
        {
        if ( ( gameboard[y][x] == gameboard[y+1][x]) && (gameboard[y][x] > 0 )) 
        {
         numMatches++;
         matchingNumber = gameboard[y][x];
        }
        if ( numMatches == 4) return matchingNumber;
        }
       }
     
       //check right slanted diagonal
       for ( int y = 3 ; y < NUMROWS ; y++)
       {
        numMatches = 0;
        for ( int x = 0 ; x < 4 ; x++){
         if ((gameboard[y][x] == gameboard[y+1][x+1]) && (gameboard [y][x] == gameboard[y+2][x+2]) && (gameboard[y][x] == gameboard[y+3][x+3]) && (gameboard[y][x] > 0)) 
           return (gameboard[y][x]);
        }
       }
     
       //check left slanted diagonal
       for ( int y = 3 ; y < NUMROWS ; y ++)
       {
        numMatches = 0;
        for ( int x = 3 ; x < NUMCOLUMNS ; x++)
        {
         if ((gameboard[y][x] == gameboard[y-1][x-1]) && (gameboard [y][x] == gameboard[y-2][x-2]) && (gameboard[y][x] == gameboard[y-3][x-3]) && (gameboard[y][x] > 0)) 
           return (gameboard[y][x]);
        }
       }
     
       return matchingNumber;
      }
     
    }
     
    class Disk extends JLabel{
     
     public void animateFall(int column , int row, int player){
      //create a disk that starts above the top row in that column and falls to the desired row
      repaint();
     }
     
     protected void paintComponent(Graphics G){
     
     }
    }
     
    class Winner extends JLabel{
     
    }
     
     
    class Column_1ListenerClass implements ActionListener{
     public void actionPerformed(ActionEvent e){
      if (Connect4.canDropDisk(0) ){
       Connect4.dropDisk(0);
      }
     }
    }
     
    class Column_2ListenerClass implements ActionListener{
     public void actionPerformed(ActionEvent e){
      if (Connect4.canDropDisk(1) ){
       Connect4.dropDisk(1);
      }
     }
    }
     
    class Column_3ListenerClass implements ActionListener{
     public void actionPerformed(ActionEvent e){
      if (Connect4.canDropDisk(2) ){
      Connect4.dropDisk(2); 
      }
     }
    }
     
    class Column_4ListenerClass implements ActionListener{
     public void actionPerformed(ActionEvent e){
      if (Connect4.canDropDisk(3) ){
       Connect4.dropDisk(3);
      }
     }
    }
     
    class Column_5ListenerClass implements ActionListener{
     public void actionPerformed(ActionEvent e){
      if (Connect4.canDropDisk(4) ){
       Connect4.dropDisk(4);
      }
     }
    }
     
    class Column_6ListenerClass implements ActionListener{
     public void actionPerformed(ActionEvent e){
      if (Connect4.canDropDisk(5) ){
       Connect4.dropDisk(5);
      }
     }
    }
     
    class Column_7ListenerClass implements ActionListener{
     public void actionPerformed(ActionEvent e){
      if (Connect4.canDropDisk(6) ){
       Connect4.dropDisk(6);
      }
     }
    }


  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Connect Four GUI Help needed

    The tutorials are a good place to start for learning how to write GUI:
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member Hobbs's Avatar
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connect Four GUI Help needed

    Thanks. Il check that out and ask more questions if i have them


Similar Threads

  1. JAva GUI Help needed. Please Help
    By Mini83 in forum AWT / Java Swing
    Replies: 8
    Last Post: August 17th, 2011, 07:04 PM
  2. Replies: 1
    Last Post: June 17th, 2011, 06:01 PM
  3. Connect Four GUI
    By gromacs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 6th, 2011, 09:42 AM
  4. Replies: 3
    Last Post: February 1st, 2010, 12:24 AM

Tags for this Thread