Results 1 to 5 of 5

Thread: Button Glitch

  1. #1
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default Button Glitch

    Hi. I'm fairly new to java, so my code may seem "immature", but I am hoping to get advice on a problem I am having.

    Java Code:
    package merp;
    import javax.swing.*;
    import java.awt.event.*;
    public class TicTacToe {
        int nSize = 100, nTurn = 0, nSquares[][] = new int[3][3], Player1 = 0, Player2 = 0, nTurnCount = 0, nDraw = 0;
        JButton Squares[][] = new JButton[3][3], Null = new JButton();
        JFrame Main = new JFrame();
        final ImageIcon X = new ImageIcon("X.png"), O = new ImageIcon("O.png");
        int MiddleX[] = {1, 1, 1, 0, 1, 2, 1, 1}, MiddleY[] = {0, 1, 2, 1, 1, 1, 1, 1};
        int LeftX[] = {0, 0, 0, 0, 1, 2, 0, 0}, LeftY[] = {0, 1, 2, 0, 0, 0, 0, 2};
        int RightX[] = {2, 2, 2, 0, 1, 2, 2, 2}, RightY[] = {0, 1, 2, 2, 2, 2, 2, 0};
        public TicTacToe(){
            Main.setSize(nSize*3 + 16, nSize*3 + 38);
            Main.setLocationRelativeTo(null);
            for (int i = 0; i < 3; i++){
                for (int j = 0; j < 3; j++){
                    final int x, y;
                    x = i;
                    y = j;
                    Squares[i][j] = new JButton();
                    Main.add(Squares[i][j]);
                    nSquares[i][j] = 2;
                    Squares[i][j].setBounds(nSize*i, nSize*j, nSize, nSize);
                    Squares[i][j].addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e){
                            if (nSquares[x][y] == 2) Turn(x, y);
                        }
                    });
                }
            }
            Main.add(Null);
            Null.setVisible(false);
            Main.setVisible(true);
            Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        public void Turn(int x, int y){
            if (nTurn<1){
                nTurn++;
                Squares[x][y].setIcon(X);
                nSquares[x][y] = 0;
            }
            else {
                nTurn = 0;
                Squares[x][y].setIcon(O);
                nSquares[x][y] = 1;
            }
            nTurnCount++;
            if (nTurnCount > 4) CheckGO();
        }
        public void CheckGO(){
            for (int i = 0; i < 8; i++){
                if (nSquares[MiddleX[i]][MiddleY[i]] != 2 && nSquares[LeftX[i]][LeftY[i]] == nSquares[MiddleX[i]][MiddleY[i]] && nSquares[RightX[i]][RightY[i]] == nSquares[MiddleX[i]][MiddleY[i]]){
                    NewGame();
                    if (nTurn < 1) Player1++;
                    else Player2++;
                    nTurnCount = 0;
                    System.out.println("Player O: " + Player1 + " wins" + "\n" + "Player X: " + Player2 + " wins" + "\n" + "Draws: " + nDraw);
                    break;
                }
            }
            if (nTurnCount == 9){
                NewGame();
                nDraw++;
                nTurnCount = 0;
                System.out.println("Player O: " + Player1 + " wins" + "\n" + "Player X: " + Player2 + " wins" + "\n" + "Draws: " + nDraw);
            }
        }
        public void NewGame(){
            for (int i = 0; i < 3; i++){
                for (int j = 0; j < 3; j++){
                    nSquares[i][j] = 2;
                    Squares[i][j].setIcon(null);
                }
            }
        }
        public static void main(String args[]){
            new TicTacToe();
        }
    }
    As you can see, I add a button called null after the button array, only because the buttons will glitch otherwise. I was wondering if there was a way of just being able to add the buttons to the frame, without having to add one more button at the end and setting it to invisible.

    I would also greatly appreciate constructive criticism of my code.

    Thanks!

    Jamie

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Start by looking at the section from the Swing tutorial on How to Use Buttons

    The examples will show you how to better structure your class. Also look at the proper naming conventions for variable names.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I may be missing it, but I don't see you using layout managers anywhere such as a GridLayout(3, 3). If this were my app, and I were adding a 3x3 grid of buttons, I'd put my buttons into a JPanel that used the GridLayout(3, 3), and then would add that JPanel to my JFrame's contentPane BorderLayout.CENTER.

    So in addition to camicrk's excellent suggestions above, I suggest that you have a look at the Sun Swing tutorial on laying out components in a container.

    Much luck!

  4. #4
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Is this better?
    Java Code:
    package merp;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TicTacToe {
        int nSize = 100, nTurn = 0, nSquares[][] = new int[3][3], Player1 = 0, Player2 = 0, nTurnCount = 0, nDraw = 0;
        JButton Squares[][] = new JButton[3][3];
        JFrame frame = new JFrame("Tic Tac Toe");
        JPanel Layout = new JPanel();
        final ImageIcon X = new ImageIcon("X.png"), O = new ImageIcon("O.png");
        int MiddleX[] = {1, 1, 1, 0, 1, 2, 1, 1}, MiddleY[] = {0, 1, 2, 1, 1, 1, 1, 1};
        int LeftX[] = {0, 0, 0, 0, 1, 2, 0, 0}, LeftY[] = {0, 1, 2, 0, 0, 0, 0, 2};
        int RightX[] = {2, 2, 2, 0, 1, 2, 2, 2}, RightY[] = {0, 1, 2, 2, 2, 2, 2, 0};
        public TicTacToe(){
            Layout.setLayout(new GridLayout(3, 3));
            Layout.setPreferredSize(new Dimension(nSize*3, nSize*3));
            for (int i = 0; i < 3; i++){
                for (int j = 0; j < 3; j++){
                    Squares[i][j] = new JButton();
                    final int x = i, y = j;
                    Layout.add(Squares[i][j]);
                    Squares[i][j].addActionListener(new ActionListener(){
                        public void actionPerformed (ActionEvent e){
                            if (nSquares[x][y] == 2) Turn(x, y);
                        }
                    });
                }
            }
            frame.getContentPane().add(Layout);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            frame.setLocationRelativeTo(null);
            NewGame();
        }
        public void Turn(int x, int y){
            if (nTurn<1){
                nTurn++;
                Squares[x][y].setIcon(X);
                nSquares[x][y] = 0;
            }
            else {
                nTurn = 0;
                Squares[x][y].setIcon(O);
                nSquares[x][y] = 1;
            }
            nTurnCount++;
            if (nTurnCount > 4) CheckGO();
        }
        public void CheckGO(){
            for (int i = 0; i < 8; i++){
                if (nSquares[MiddleX[i]][MiddleY[i]] != 2 && nSquares[LeftX[i]][LeftY[i]] == nSquares[MiddleX[i]][MiddleY[i]] && nSquares[RightX[i]][RightY[i]] == nSquares[MiddleX[i]][MiddleY[i]]){
                    NewGame();
                    if (nTurn < 1) Player1++;
                    else Player2++;
                    nTurnCount = 0;
                    System.out.println("Player O: " + Player1 + " wins" + "\n" + "Player X: " + Player2 + " wins" + "\n" + "Draws: " + nDraw);
                    break;
                }
            }
            if (nTurnCount == 9){
                NewGame();
                nDraw++;
                nTurnCount = 0;
                System.out.println("Player O: " + Player1 + " wins" + "\n" + "Player X: " + Player2 + " wins" + "\n" + "Draws: " + nDraw);
            }
        }
        public void NewGame(){
            for (int i = 0; i < 3; i++){
                for (int j = 0; j < 3; j++){
                    nSquares[i][j] = 2;
                    Squares[i][j].setIcon(null);
                }
            }
        }
        public static void main(String args[]){
            new TicTacToe();
        }
    }

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Your still haven't looked at the examples from the Swing tutorial:

    a) most of your variable names to not follow standards
    b) your method names to not follow standards
    c) the creation of the GUI does not follow the recommended approach.

Similar Threads

  1. Button
    By Tb0h in forum New To Java
    Replies: 6
    Last Post: 07-22-2009, 01:28 AM
  2. Please help on Button action
    By michaelxiao in forum New To Java
    Replies: 4
    Last Post: 04-24-2009, 10:55 PM
  3. Back button
    By Tokajac in forum Web Frameworks
    Replies: 1
    Last Post: 11-27-2008, 05:27 AM
  4. Using SWT Button
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •