注: この質問は、数週間前に投稿した別の質問と少し似ているかもしれません。当時、私はボタンを配列として追加する作業をしていませんでした。
私はチェスゲームで働いていて、自分で64マスのボードを立てることができました。ただし、正方形に色を追加するのは少し複雑すぎるようです。
私のコードは次のようになります。
チェス.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);
}
}
ピース.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));
}
私はJavaに非常に慣れていないので、これはおそらくかなり悪いコーディングであることをよく知っています。数日間立ち往生しているので、誰かがここの色で私を助けてくれたら、とても幸せで感謝します. 誰かが私のためにコードを仕上げてくれるとは思っていませんが、そこにたどり着くまでの道のりを手伝ってくれるだけです。:)