TetrisGUIを作成しようとしています。私の友人はバックエンドを書きました。これまでは、テトリスボード(またはコードで参照しているグリッド)をコンソールウィンドウに出力していました。以下のコードでは、テトリスゲームのボードとして機能するJTableを設定しています。Windowクラスの最上位で宣言されたTetrisの「ゲーム」から渡されたグリッドに基づいて各グリッド要素をレンダリングするようにJTableを取得するにはどうすればよいか疑問に思いました。このグリッドは、Piecesクラスで列挙された色を参照する整数値の2D配列です。助言がありますか?現在のところ、1色しか印刷されません。
Tetrisクラスのコードも投稿したので、そこで使用できるメソッドとパラメーターを確認できます。
これが私のコードです(うまくいけば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、およびすべてのフィールドのゲッターとセッターに加えて。Tetrisクラスのすべてのメソッドはコンソールでテストされ、正しく機能します。これが私がこれまでに持っているものの出力です。毎回色が異なり、その理由は確かですが、テトリスクラスで作成されたグリッド配列に基づいて各セルに色を付ける方法を考えるのに苦労しています。

