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 3 of 3

Thread: Connect Four GUI

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Connect Four GUI

    Hey guys, first post here asking for help with some grade 11 compsci work. We're required to make a connect four: no AI, 2 players, nothing fancy like reset buttons etc. just buttons that change colour and can detect when a player wins. I'm having a lot of trouble with this, could you take a few minutes to look it over and tell me what's wrong, if there's anything at all I did right, and what I should do to make it work? Right now, I suspect that I'm not allowed to make new jbuttons inside a for loop, but if not how should I do it? Thanks a dozen, I really appreciate this guys!

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.GridLayout;
     
    public class connectFour {
    	JFrame frame;
    	JPanel panel;
    	final int rowTiles = 6;
    	final int colTiles = 5;
    	int row, col, rowSelected, colSelected = 0;
    	int pTurn = 0;
    	boolean win = false;
    	JButton[][] button = new JButton[rowTiles][colTiles];
    	int[][] grid = new int[rowTiles][colTiles];
    	GridLayout myGrid = new GridLayout(6,7);
    	final ImageIcon c0 = new ImageIcon("p1.png");
    	final ImageIcon c1 = new ImageIcon("p2.png");
    	final ImageIcon c2 = new ImageIcon("p3.png");
     
    	public connectFour() {
    		frame = new JFrame("Connect Four");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		panel = new JPanel();
    		panel.setLayout(myGrid);
     
    		for (row = rowTiles; row >= 0; row--) {
    			for (col = colTiles; col >= 0; col--) {
    				button[row][col] = new JButton(c0);
    				button[row][col].addActionListener(new buttonListener());
    				panel.add(button[col][row]);
    			}
    		}
     
    		frame.setContentPane(panel);
    		frame.setSize(700,600);
    		frame.pack();
    		frame.setVisible(true);
    	}
     
    	class buttonListener implements ActionListener {
    		public void actionPerformed(ActionEvent event) {
    			String eventName = event.getActionCommand();
     
    /*			if (win = false) {
    				if (pTurn % 2 == 0) {
    					button[rowSelected][colSelected].setIcon(ImageIcon(c1));
    				}
    				if (pTurn % 2 == 1) {
    					button[rowSelected][colSelected].setIcon(ImageIcon(c2));
    				}
    				if (jLabelCheck(colSelected) == true) {
    					checkWin();
    					pTurn = pTurn + 1;
    				}
    			} *///I dunno where to go with this part, it's just here to give an idea of what I want to accomplish
    		}
    	}
     
    	public boolean checkWin() {
    		// check for a horizontal win
    		for (int row=0; row<6; row++) {
    			for (int col=0; col<4; col++) {
    				if (grid[col][row] != 0 &&
    				grid[col][row] == grid[col+1][row] &&
    				grid[col][row] == grid[col+2][row] &&
    				grid[col][row] == grid[col+3][row]) {
    					win = true;
    				}
    			}
    		}
    		// check for a vertical win
    		for (int row=0; row<3; row++) {
    			for (int col=0; col<7; col++) {
    			if (grid[col][row] != 0 &&
    			grid[col][row] == grid[col][row+1] &&
    			grid[col][row] == grid[col][row+2] &&
    			grid[col][row] == grid[col][row+3]) {
    				win = true;
    				}
    			}
    		}
    		// check for a diagonal win (positive slope)
    		for (int row=0; row<3; row++) {
    			for (int col=0; col<4; col++) {
    			if (grid[col][row] != 0 &&
    			grid[col][row] == grid[col+1][row+1] &&
    			grid[col][row] == grid[col+2][row+2] &&
    			grid[col][row] == grid[col+3][row+3]) {
    				win = true;
    				}
    			}
    		}
    		// check for a diagonal win (negative slope)
    		for (int row=3; row<6; row++) {
    			for (int col=0; col<4; col++) {
    			if (grid[col][row] != 0 &&
    			grid[col][row] == grid[col+1][row-1] &&
    			grid[col][row] == grid[col+2][row-2] &&
    			grid[col][row] == grid[col+3][row-3]) {
    				win = true;
    				}
    			}
    		}
    		return win;
    	}
     
    	public static void main(String[] args) {
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				JFrame.setDefaultLookAndFeelDecorated(true);
    				new connectFour();
    			}
    		});
    	}
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Connect Four GUI

    Hello gromacs. Welcome to the Java Programming Forums.

    What doesn't work? Where abouts are you stuck?

    It might be useful to attach p1.png, p2.png, p3.png.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Connect Four GUI

    This sticks out as strange:

    button[row][col] = new JButton(c0);
    button[row][col].addActionListener(new buttonListener());
    panel.add(button[col][row]);

    Are you working with [row, col] or [col, row]?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Need help to connect to PostgreSql
    By stab in forum JDBC & Databases
    Replies: 3
    Last Post: June 3rd, 2011, 09:01 AM
  2. Cant connect with database
    By ronn1e in forum JDBC & Databases
    Replies: 1
    Last Post: January 4th, 2011, 05:45 PM
  3. Cant connect with database
    By ronn1e in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 4th, 2011, 04:09 PM
  4. Cannot Connect to Server
    By Brt93yoda in forum Java Networking
    Replies: 0
    Last Post: December 9th, 2010, 05:54 PM
  5. How to connect keyboard through COM?
    By yogesh01 in forum Java Theory & Questions
    Replies: 0
    Last Post: July 6th, 2010, 05:00 AM