0

So I'm trying to create Chess and to start I'm building what I will use as the board. In this code, I'm able to "switch" the positions of two buttons, however, every time I click any one of the buttons, which for testing purposes calls on the Rook class, it causes a new JFrame to open for an unknown reason.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ChessTesting1 extends JFrame implements ActionListener
{
    JButton tiles[] [] = new JButton [8] [8];
    public int x, y, turn;
    String buttonSwitch, buttonText = "";
    JFrame frame;

    public static void main (String args[])
    {
        new ChessTesting1 ();
    }


    public ChessTesting1 ()
    {
        frame = new JFrame ();
        frame.setSize (800, 800);
        frame.setLocation (150, 50);
        frame.setResizable (false);
        frame.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);
        JPanel contentPane = (JPanel) frame.getContentPane ();
        contentPane.setLayout (new GridLayout (8, 8));

        for (int x = 0 ; x < 8 ; x++)
        {
            for (int y = 0 ; y < 8 ; y++)
            {
                tiles [x] [y] = new JButton ("" + (y + 1));
            }
        }

        for (int x = 0 ; x < 8 ; x++)
        {
            for (int y = 0 ; y < 8 ; y++)
            {
                contentPane.add ((tiles [x] [y]));
                tiles [i] [j].addActionListener (this);
            }
        }
        frame.show ();
    }




    public void actionPerformed (ActionEvent e)
    {
        Object command = e.getSource ();
        for (int i = 0 ; i < 8 ; i++)
        {
            for (int j = 0 ; j < 8 ; j++)
            {
                if (command == tiles [i] [j])
                {
                    if (turn == 0)
                    {
                        buttonText = tiles [i] [j].getText ();
                        x = i;
                        y = j;
                        new Bishop (x, y);
                        frame.dispose ();

                        buttonSwitch = buttonText;
                        turn++;
                    }
                    else
                    {
                        tiles [x] [y].setText (tiles [i] [j].getText ());
                        tiles [i] [j].getText ();
                        tiles [i] [j].setText ("" + buttonSwitch);
                        turn = 0;
                    }
                buttonText = tiles [x] [y].getText ();
                }
            }
        }
    }
}

Bishop Class:

public class Bishop extends ChessTesting1
{
    public Bishop (int x, int y)
    {
        for (int i = 0 ; i < 8 ; i++)
        {
            for (int j = 0 ; j < 8 ; j++)
            {
                this.tiles [i] [j].setEnabled (false);
            }
        }
        for (int i = 0 ; i < 8 ; i++)
        {
            this.tiles [x + 1] [y + 1].setEnabled (true);
            if ((x + 1) >= 8 || (y + 1) >= 8)
            {
                break;
            }
        }
        for (int i = 0 ; i < 8 ; i++)
        {
            this.tiles [x + 1] [y - 1].setEnabled (true);
            if ((x + 1) >= 8 || (y + 1) >= 8)
            {
                break;
            }
        }
        for (int i = 0 ; i < 8 ; i++)
        {
            this.tiles [x - 1] [y + 1].setEnabled (true);
            if ((x + 1) >= 8 || (y + 1) >= 8)
            {
                break;
            }
        }
        for (int i = 0 ; i < 8 ; i++)
        {
            this.tiles [x - 1] [y - 1].setEnabled (true);
            if ((x + 1) >= 8 || (y + 1) >= 8)
            {
                break;
            }
        }
    }
}

I'm aware that currently the code doesn't do much, but I was wondering how I could stop a new JFrame from opening on each click.

Any help would be appreciated! Thanks!

1 Answers1

1

You're extending ChessTesting1 on your pieces and as such, when you instantiate something like new Bishop (x, y); you're also calling the super constructor and therefore are creating a new JFrame every time. From an OOP, it doesn't make sense that the chess pieces extend ChessTesting1 and as such, you should carefully re-consider your design. I would recommend reading about design patterns such as the Model View Controller (MVC).

Consider the following:

import java.awt.EventQueue;
import javax.swing.JFrame;

public class SuperClass {
    public SuperClass() {
        JFrame frame = new JFrame();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new SuperClass();
                    new ChildClass();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

public class ChildClass extends SuperClass {
    public ChildClass() {
        // implicit super() call
    }
}

Two JFrame elements will appear because the ChildClass extends the functionality of the SuperClass via an implicit super() call in the constructor of ChildClass().

David Yee
  • 3,515
  • 25
  • 45
  • Really appreciate the help, however, can you think of anyway I could solve this issue, or will I have to redesign my program. – Shalin Upadhyay Jun 05 '14 at 22:26
  • Well the short answer would be to instantiate the ```JFrame``` elsewhere, although I highly recommend redesigning and learning about composition over inheritance. – David Yee Jun 05 '14 at 22:29
  • Where would you say is a good place to instantiate the JFrame? Also, thank you for the recommendation I'll definitely try and learn more of inheritance. – Shalin Upadhyay Jun 05 '14 at 22:36
  • It's hard to say, really unless I know the entire program. You should probably put your ```JFrame``` in a separate class that holds the view and controller and then keep your models, the chess pieces as separate classes as well. – David Yee Jun 05 '14 at 22:54
  • Alright thank you I will try that see what I can do. As well, instead of extending to ChessTesting1 in the Bishop class, I tried to pass JButton tiles as a parameter when I called on the Bishop class, however, I get an error where the compiler does not understand the type. Do you know what the issue could be for this? – Shalin Upadhyay Jun 05 '14 at 23:25
  • Please open a new question with code that shows your issue. – David Yee Jun 05 '14 at 23:27