2

Note: this question may look a bit like another I've posted a few weeks ago. Back then I was not working with adding the buttons as arrays, thats what makes it more difficult for me this time.

I'm working with a chessgame, and I have been able to set up a board of 64 squares on my own. However it seems to be a little too complicated for me to manage adding the colors to the squares.

My code looks like this:

Chess.java

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Chess implements config {

public static void main(String[] args) {

    int[] squareArray;

    squareArray = new int[65];

    int i = 1;

    JFrame frame = new JFrame("Chessboard");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(ROWS, COLS, 2, 2));

    for (i = 1; i < 65; i++) {
        squareArray[i] = i;

        frame.add(new JButton("" + squareArray[i]));
        }

    frame.setSize(800, 800);
    frame.setVisible(true);
    }
}

Piece.java

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

public class Piece extends JFrame implements config {

public Piece (int n) {

    setBackground(calcColor(n));
    }

public void Pieces() {
    new Pieces();
        //This class contains nothing at the moment.
    }

Color calcColor(int n) {
    boolean everysecondSquare = (n % 2 == 0);
    boolean everysecondRow = ((n / ROWS) % 2 == 0);
    return (everysecondSquare != everysecondRow ? P1Color : P2Color);

    }

}

config.java

import java.awt.Color;


public interface config {

public int ROWS = 8;
public int COLS = 8;

Color P1Color = (new Color(245,222,179));
Color P2Color = (new Color(244,164,96));

}

I'm very aware that this probably is pretty bad coded as I am very new to Java. I would be very happy and thankful if someone could help me out with the colors here as I have been stuck for several days now without getting any further. I don't expect someone to finish the code for me, but merely help me on the way to get there. :)

Xantrax
  • 199
  • 2
  • 5
  • 13
  • See also this [example](http://stackoverflow.com/a/2562685/230513) and [variation](http://stackoverflow.com/a/2563350/230513). – trashgod Feb 28 '13 at 11:01

1 Answers1

5

What about this?

for (i = 1; i < 65; i++) {
    squareArray[i] = i;

    JButton b=new JButton("" + squareArray[i]);
    b.setBackground(desiredColorHere);
    frame.add(b);
}
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • May need `setOpaque()` or a [background panel](http://stackoverflow.com/a/3420431/230513). – trashgod Feb 28 '13 at 11:02
  • Thanks! It works great for adding backgroundcolor to the whole board, but I can't manage to use my calcColor method from config.java – Xantrax Feb 28 '13 at 11:14
  • @Xantrax : Please learn [Java Naming Conventions](http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html), and try to implement them while writing Java Code. Moreover, fields specified in interfaces, are always `public, static and final`, so you must be able to access fields of `config.java` into `Chess.java` by simply writing `config.P1Color/config.P2Color` – nIcE cOw Feb 28 '13 at 12:29