Jump to content

JPanel

- - - - -

  • Please log in to reply
15 replies to this topic

#1
Daigan

Daigan

    Newbie

  • Members
  • PipPip
  • 19 posts
class SingleBoard extends JPanel

    {

        public SingleBoard (ActionListener e)

        {

            setLayout (new GridLayout (3, 3));

            for (int i = 0 ; i <= 8 ; i++)

            {

                buttons [i] = new JButton ();

                buttons [i].addActionListener (e);

                add (buttons [i]);

            }


        }

    }



    public void init ()

    {

        //Set the applet size for better viewing

        setSize (300, 700);


        //Message for the players

        JLabel message = new JLabel ("X will go first");

        getContentPane ().setLayout (new BorderLayout ());

        getContentPane ().add (message, BorderLayout.NORTH);


        //Adding 3 boards

        getContentPane ().setLayout (new GridLayout (5, 5, 10, 10));

        for (int i = 0 ; i < 3 ; i++)

        {

            SingleBoard sb = new SingleBoard (this);

            getContentPane ().add (sb);

        }

    }


My only problem is that the last panel is the only one I think retains the variable names buttons[0]-buttons[8], so I'm having problem to check the winner. I don't know how to access/reference the buttons on the first 2 panels

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Setup an accessor method to get the reference to your button array.
Example:

private JButton[] buttons = ...


...


public JButton[] getButtons() {

    return buttons;

}



#3
Daigan

Daigan

    Newbie

  • Members
  • PipPip
  • 19 posts
Sorry, but I don't get the whole concept of it? Could you elaborate a little bit more? Where should I put it in?

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
You can create "getter" and "setter" methods that allow you to "get" and "set" private variables within a class.
Example:

public class Person {

    private String name;

    private int age;

    public(String name, int age) {

        this.name = name;

        this.age = age;

    }

}

Now in order to get the name or age from this person object, you cannot simply write something like:

public static void main(String[] args) {

    Person user = new Person("John Doe", 22);

    String name = user.name; [COLOR="#FF0000"]// <-- Error[/COLOR]

    int age = user.age; // [COLOR="#FF0000"]<-- Error[/COLOR]

}


So, to gain access to these variables most programmers add accessor methods to the Person class:

public class Person {

    private String name;

    private int age;

    public(String name, int age) {

        this.name = name;

        this.age = age;

    }


[COLOR="#008000"]/* ---- Accessor Method ---- */[/COLOR]

    public String getName() {

        return this.name;

    }


    public void setName(String newName) {

        this.name = newName;

    }


    [COLOR="#008000"]// ... add similar methods for getting/setting name[/COLOR]

}


Example:

public static void main(String[] args) {

    Person user = new Person("John Doe", 22);

    String name = user.getName();

    int age = user.getAge();

}



#5
Daigan

Daigan

    Newbie

  • Members
  • PipPip
  • 19 posts
Okay so I added this code:

public JButton[] getButtons() {

    return buttons;

}

but it is still not working? Only the last panel is checking the winner.

#6
Daigan

Daigan

    Newbie

  • Members
  • PipPip
  • 19 posts
Does this method sets like a reference to each of the JButtons that are added?

public JButton[] getButtons ()

{

return buttons;

}


#7
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
It's really hard to know what is not working without more code. The code you have given so far isn't even compilable.

The getButtons() method returns a reference to the location of the buttons object.

#8
Daigan

Daigan

    Newbie

  • Members
  • PipPip
  • 19 posts
// The "TicTacToe3d" class.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

import hsa.*;


public class TicTacToe3D extends JApplet implements ActionListener

{

    /*

        * Variable Dictionary:

        * int count     = Used for counting the terms

        * String symbol = Used for setting X or O to the buttons

        */


    /*

    * No. of panels which are basically the boards in Tic-Tac-Toe Game

    * We're going to use 3 panels to make 3 Boards and set it to final

    */

    public static final int panels = 3;

    private GameBoard[] boards = new GameBoard [panels];


    /*

    * The JButtons that are used for placing X or O

    */

    private JButton[] buttons = new JButton [9];


    /*

    * All the posible winning combinations in the game

    */

    private int[] [] winCombinations = new int[] []

    {

        {

            0, 1, 2

        }

        ,

        {

            3, 4, 5

        }

        ,

        {

            6, 7, 8

        }

        ,

        {

            0, 3, 6

        }

        ,

        {

            1, 4, 7

        }

        ,

        {

            2, 5, 8

        }

        ,

        {

            0, 4, 8

        }

        ,

        {

            2, 4, 6

        }

    }



    ;

    private boolean checkWin = false;

    int count = 0;

    String symbol = "";



    class GameButton extends JButton

    {

        public int row, col, plane;

        public GameButton (int row, int col, int plane)

        {

            this.row = row;

            this.col = col;

            this.plane = plane;

            this.setText (" ");

            //this.setBackground (

        }

        public String toString ()

        {

            return "(" + row + "," + col + "," + plane + ") = " + this.getText ();

        }



    }



    class GameBoard extends JPanel

    {

        /*

        * GameBoard Class Variable Dictionary:

        *

        */

        public static final int rowsB = 3;

        public static final int colsB = 3;


        private GameButton[] buttons = new GameButton [rowsB * colsB];


        public GameBoard (int plane, ActionListener e)

        {

            setLayout (new GridLayout (rowsB, colsB));

            for (int row = 0 ; row < rowsB ; row++)

            {

                for (int col = 0 ; col < colsB ; col++)

                {

                    GameButton b = new GameButton (row, col, plane);

                    b.addActionListener (e);

                    add (b);

                    buttons [colsB * row + col] = b;

                }

            }


        }


        public char getValue (int y, int x)

        {

            String letter = buttons [colsB * y + x].getText ();

            return (letter == null || letter.length () == 0) ? ' ':

            letter.charAt (0);

        }


        public void setValue (int y, int x, char val)

        {

            buttons [colsB * y + x].setText (String.valueOf (val));

        }


    }



    public void init ()

    {

        //Set the applet size for better viewing

        setSize (300, 700);


        //Message for the players

        JLabel message = new JLabel ("X will go first");

        getContentPane ().setLayout (new BorderLayout ());

        getContentPane ().add (message, BorderLayout.NORTH);


        //Adding 3 boards

        getContentPane ().setLayout (new GridLayout (5, 5, 10, 10));


        //Adding 3 boards

        getContentPane ().setLayout (new GridLayout (5, 5, 10, 10));

        for (int i = 0 ; i < 3 ; i++)

        {

            GameBoard gb = new GameBoard (i, this);

            boards [i] = gb;

            getContentPane ().add (gb);

        }



    }




    public void actionPerformed (ActionEvent click)

    {


        count++;


        //If the count is an even number (divisible by 2) then it sets the symbol as O

        if (count % 2 == 0)

        {

            symbol = "O";

        }

        else

        {

            symbol = "X";

        }


        //If a button is click it either puts on an X or O depending on count then disable clicking it again

        GameButton b = (GameButton) click.getSource ();

        b.setText (symbol);

        b.setEnabled (false);


        GameBoard gb = new GameBoard (count, this);


        if (gb.buttons [0].getText () == gb.buttons [1].getText () && gb.buttons [0].getText () == gb.buttons [2].getText () && gb.buttons [0].getText() != " ")

        {

            System.out.println ("+1");

        }


    }

} // TicTacToe3d class




Here's the whole code so far..

#9
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I can't see any panel (not even the last one) checking for a winner.
I'm also confused as to what you are trying to accomplish here:

        GameBoard gb = new GameBoard (count, this);


        if (gb.buttons [0].getText () == gb.buttons [1].getText () && gb.buttons [0].getText () == gb.buttons [2].getText () && gb.buttons [0].getText() != " ")

        {

            System.out.println ("+1");

        }


#10
Daigan

Daigan

    Newbie

  • Members
  • PipPip
  • 19 posts

lethalwire said:

I can't see any panel (not even the last one) checking for a winner.
I'm also confused as to what you are trying to accomplish here:

        GameBoard gb = new GameBoard (count, this);


        if (gb.buttons [0].getText () == gb.buttons [1].getText () && gb.buttons [0].getText () == gb.buttons [2].getText () && gb.buttons [0].getText() != " ")

        {

            System.out.println ("+1");

        }

Isn't the GameBoard class a JPanel? So after calling it three times inside the loop make 3 panels, no? And I was just testing on that code if I can access the last panel's first three buttons.

#11
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Why are you creating a new GameBoard object though?
GameBoard gb = new GameBoard (count, this);

Why don't you reference the 3 GameBoards you created and setup in the beginning stages of the program?
 private GameBoard[] boards = new GameBoard [panels];

Example:

 GameBoard gb =[COLOR="#FF0000"] boards[0];[/COLOR]

        

        if (gb.buttons [0].getText ().equals( gb.buttons [1].getText ())  && gb.buttons [0].getText ().[COLOR="#FF0000"]equals[/COLOR]( gb.buttons [2].getText ())

        		&& !gb.buttons [0].getText().[COLOR="#FF0000"]equals[/COLOR]( " "))

        {

            System.out.println ("+1");

        }

Also, you shouldn't compare strings using ==. To correctly compare strings use String's equals(String other) method.

How do I compare strings in Java? - Stack Overflow

#12
Daigan

Daigan

    Newbie

  • Members
  • PipPip
  • 19 posts
So I declared this:
 private GameBoard[] boards = new GameBoard [panels];

Then I have this on the init method:
GameButton gb = new GameButton (count, this);

        for (int i = 0 ; i < boards.length ; i++)

        {


            boards [i].add (gb);

        }


Then it errors:
java.lang.NullPointerException
at TicTacToe3D.init(TicTacToe3D.java:156)
at ready.AppletRunner.run(AppletRunner.java:209)
at java.lang.Thread.run(Unknown Source)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users