Предполагается, что пользователь сможет вводить строки и столбцы через JOptionPane, что я и сделал. Но когда я вхожу в класс ColorPanel, я не могу получить свои цвета для шахматной доски, чтобы показать в JFrame. Некоторая помощь будет принята с благодарностью. Вот как выглядит Код: 1-й класс:
import javax.swing.*;
import java.awt.*;
public class CheckerMyChaps {
public static void main(String[] args) {
JFrame check_check = new JFrame ();
check_check.setTitle("Checker of the Board ");
check_check.setSize(600,600);
check_check.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String King_Me =JOptionPane.showInputDialog("Enter the number of rows you would like ");
int rows = Integer.parseInt(King_Me);
if (King_Me == null) return;
String Queen_Me = JOptionPane.showInputDialog("Enter the number of columns you would like ");
int columns = Integer.parseInt(Queen_Me);
if (Queen_Me == null) return;
ColorForSquares panel = new ColorForSquares (Color.black);
Container pane = check_check.getContentPane();
pane.add(panel);
pane.setLayout(new GridLayout (rows,columns));
check_check.setVisible(true);
}
{
}
}
2-й класс для ColorPanel:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class ColorForSquares extends JPanel{
public ColorForSquares (Color backColor) {
setBackground (Color.pink); }
public void paint (Graphics i) {
int rows = 0, columns = 0;
for (int x = 0 ; x < rows; x++) {
for (int y = 0; y < columns; y++) {
x = rows * 20;
y = columns * 20;
if ((rows % 2) == (columns % 2 ) )
i.setColor (Color.blue);
else
i.setColor (Color.green);
i.drawRect(x, y, 20, 20);
}
}
{
{
}
}
}
}