基于俄罗斯方块游戏创建的数组的java着色JTable单元格
我正在尝试创建一个俄罗斯方块图形用户界面。我的朋友写了后端。到目前为止,我们只是将俄罗斯方块(或我在代码中提到的网格)打印到控制台窗口。在下面的代码中,我正在设置一个JTable作为俄罗斯方块游戏的棋盘。我想知道如何让我的JTable根据窗口类顶部声明的俄罗斯方块“游戏”传递的网格渲染每个网格元素。此网格是一个整数值的二维数组,它引用Pieces类中枚举的颜色。有什么建议吗?到目前为止,它只打印一种颜色
我还发布了一些俄罗斯方块类的代码,以便您可以看到那里可用的方法和参数
下面是我的代码(希望是SSCCE=p):
public class Window {
JPanel cards;
final static String SPLASHSCREEN = "SplashScreen";
final static String MAINMENU = "MainMenu";
final static String TETRIS = "Tetris";
final static int GRID_ROW_HEIGHT = 30;
final static int NUM_ROWS = 20;
final static int NUM_COLS = 10;
JTable table = new JTable(new MyTableModel());
Tetris game = new Tetris();
public void addComponentToWindow(Container pane) {
// Create the "cards"
.
.
.
// SplashScreen setup
.
.
.
// MainMenu setup
.
.
.
// Tetris setup
final JButton startGame = new JButton("START GAME");
card3.setLayout(new GridBagLayout());
GridBagConstraints gbc2 = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);
card3.add(startGame, gbc2);
gbc.gridy = 1;
startGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
table.setDefaultRenderer(Object.class, new MyRenderer());
table.setRowHeight(GRID_ROW_HEIGHT);
table.setFocusable(false);
table.setRowSelectionAllowed(false);
for (int i = 0; i < game.getNumCols(); i++) {
table.getColumnModel().getColumn(i).setPreferredWidth(table.getRowHeight());
}
card3.add(table);
card3.remove(0); //Removes button
card3.revalidate(); //Redraws graphics
}
});
// Sets up layout
cards = new JPanel(new CardLayout());
cards.add(card1, SPLASHSCREEN);
cards.add(card2, MAINMENU);
cards.add(card3, TETRIS);
// Creates the actual window
pane.add(cards, BorderLayout.CENTER);
}
public Color getTableCellBackground(JTable table, int row, int col) {
TableCellRenderer renderer = table.getCellRenderer(row, col);
Component component = table.prepareRenderer(renderer, row, col);
return component.getBackground();
}
class MyRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JTextField editor = new JTextField();
if (value != null) {
editor.setText(value.toString());
}
if (game.getCur_color().getKey() == 0) {
editor.setBackground(Color.WHITE);
}
else if (game.getCur_color().getKey() == 1) {
editor.setBackground(Color.RED);
}
else if (game.getCur_color().getKey() == 2) {
editor.setBackground(Color.GREEN);
}
else if (game.getCur_color().getKey() == 3) {
editor.setBackground(Color.BLUE);
}
else if (game.getCur_color().getKey() == 4) {
editor.setBackground(Color.YELLOW);
}
return editor;
}
}
@SuppressWarnings("serial")
class MyTableModel extends AbstractTableModel {
public int getColumnCount() {
return NUM_COLS;
}
public int getRowCount() {
return NUM_ROWS;
}
public Object getValueAt(int row, int col) {
return null;
}
}
}
俄罗斯方块课:
public class Tetris
{
int NUM_ROWS = 20;
int NUM_COLS = 10;
int grid[][];
int cur_row;
int cur_col;
Pieces cur_color;
Style cur_style;
Pieces next_color;
Style next_style;
boolean over;
public Tetris()
{
grid = new int[10][20];
for(int i = 0; i < 10; i ++)
{
for(int j = 0; j < 20; j ++)
{
grid[i][j] = Pieces.BLANK.getKey();
}
}
next_color = Pieces.createColor();
next_style = Style.createStyle();
over = false;
create_Piece();
}
public void createPiece(){...}
public void setPiece(){...}
public void removeRow(){...}
}
以及所有字段的moveLeft、moveRight、moveDown、rotateLeft、rotateRight、printGame和Getter和Setter。俄罗斯方块类中的所有方法都在控制台中进行了测试,并且工作正常。这是我到目前为止的成果。每次颜色都不一样,我很确定我知道为什么,但是我很难思考如何根据在Tetris类中创建的网格数组为每个单元格着色
# 1 楼答案
这纯粹是概念的证明
基本上,你已经有了一个游戏板的模型。你需要能够在屏幕上建模
我会把你的董事会模型包装在一个
TableModel
中,并与TableCellRenderer
一起,允许两者一起工作当电路板数据发生变化时,您所需要做的就是相应地更新表模型
如果正确写入关系,边框将通知表模型,而表模型将通知表