Почему бы не использовать простую сетку JPanel размером 20 x 20 в GridLayout (20, 20) и перевернуть цвет фона панели, если щелкнуть с помощью метода MousePistener mousePressed.Вы можете удерживать панели в 2D-массиве и запрашивать их цвет фона всякий раз, когда в этом возникает необходимость.
Вы также можете использовать JLabels для этого, но вам придется помнить, что их непрозрачные свойства должны иметь значение true.JButton будет работать так же хорошо, как JToggleButton, ... варианты практически безграничны.Однако я не рекомендую вам использовать AWT (Canvas), так как им нет необходимости отступать по функциональности, поскольку Swing хорошо справляется с этим.
Если вы застряли на этом, почему бы не вернуться и не показать нам свой коди мы лучше сможем оказать вам более конкретную помощь.
Еще один способ решить эту проблему - использовать один JPanel и переопределить его метод paintComponent.Вы можете дать ему массив int [] [], который будет служить в качестве его модели, а затем в методе paintComponent начертите прямоугольники любого желаемого цвета в зависимости от состояния модели.Затем дайте ему MouseListener, который изменяет состояние модели и вызывает перерисовку.
например,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class BlackWhiteGridPanel extends JPanel {
// can have multiple colors if desired
// public static final Color[] COLORS = {Color.black, Color.red, Color.blue, Color.white};
public static final Color[] COLORS = {Color.black, Color.white};
public static final int SIDE = 20;
private static final int BWG_WIDTH = 400;
private static final int BWG_HEIGHT = BWG_WIDTH;
private int[][] model = new int[SIDE][SIDE]; // filled with 0's.
public BlackWhiteGridPanel() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
myMousePressed(e);
}
});
}
private void myMousePressed(MouseEvent e) {
// find relative position of mouse press on grid.
int i = (e.getX() * SIDE) / getWidth();
int j = (e.getY() * SIDE) / getHeight();
int value = model[i][j];
// the model can only hold states allowed by the COLORS array.
// So if only two colors, then value can only be 0 or 1.
value = (value + 1) % COLORS.length;
model[i][j] = value;
repaint();
}
public int[][] getModel() {
// return a copy of model so as not to risk corruption from outside classes
int[][] copy = new int[model.length][model[0].length];
for (int i = 0; i < copy.length; i++) {
System.arraycopy(model[i], 0, copy[i], 0, model[i].length);
}
return copy;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int ht = getHeight();
for (int i = 0; i < model.length; i++) {
for (int j = 0; j < model[i].length; j++) {
Color c = COLORS[model[i][j]];
g.setColor(c);
int x = (i * width) / SIDE;
int y = (j * ht) / SIDE;
int w = ((i + 1) * width) / SIDE - x;
int h = ((j + 1) * ht) / SIDE - y;
g.fillRect(x, y, w, h);
}
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(BWG_WIDTH, BWG_HEIGHT);
}
private static void createAndShowGui() {
BlackWhiteGridPanel mainPanel = new BlackWhiteGridPanel();
JFrame frame = new JFrame("BlackWhiteGrid");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}