Java 基于俄罗斯方块游戏创建的数组为JTable单元格着色

Java 基于俄罗斯方块游戏创建的数组为JTable单元格着色,java,swing,jtable,rendering,background-color,Java,Swing,Jtable,Rendering,Background Color,我正在尝试创建一个俄罗斯方块图形用户界面。我的朋友写了后端。到目前为止,我们只是将俄罗斯方块(或我在代码中提到的网格)打印到控制台窗口。在下面的代码中,我正在设置一个JTable作为俄罗斯方块游戏的棋盘。我想知道如何让我的JTable根据窗口类顶部声明的俄罗斯方块“游戏”传递的网格渲染每个网格元素。此网格是一个整数值的二维数组,它引用Pieces类中枚举的颜色。有什么建议吗?到目前为止,它只打印一种颜色 我还发布了一些俄罗斯方块类的代码,以便您可以看到那里可用的方法和参数 下面是我的代码(希望是

我正在尝试创建一个俄罗斯方块图形用户界面。我的朋友写了后端。到目前为止,我们只是将俄罗斯方块(或我在代码中提到的网格)打印到控制台窗口。在下面的代码中,我正在设置一个JTable作为俄罗斯方块游戏的棋盘。我想知道如何让我的JTable根据窗口类顶部声明的俄罗斯方块“游戏”传递的网格渲染每个网格元素。此网格是一个整数值的二维数组,它引用Pieces类中枚举的颜色。有什么建议吗?到目前为止,它只打印一种颜色

我还发布了一些俄罗斯方块类的代码,以便您可以看到那里可用的方法和参数

下面是我的代码(希望是SSCCE=p):

公共类窗口{
JPanel卡;
最终静态字符串SPLASHSCREEN=“SPLASHSCREEN”;
最终静态字符串MAINMENU=“MAINMENU”;
最终静态字符串俄罗斯方块=“俄罗斯方块”;
最终静态整数网格行高=30;
最终静态整数行数=20;
最终静态整数=10;
JTable table=newjtable(newmytablemodel());
俄罗斯方块游戏=新俄罗斯方块();
public void addComponentToWindow(容器窗格){
//创建“卡片”
.
.
.
//溅屏设置
.
.
.
//主菜单设置
.
.
.
//俄罗斯方块设置
最终JButton startGame=新JButton(“开始游戏”);
card3.setLayout(新的GridBagLayout());
GridBagConstraints gbc2=新的GridBagConstraints();
gbc.gridx=0;
gbc.gridy=0;
gbc.插图=新插图(2,2,2,2);
卡片3.添加(起始名称,gbc2);
gbc.gridy=1;
startName.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
setDefaultRenderer(Object.class,new MyRenderer());
表.设置行高(网格行高);
表1.setFocusable(假);
table.setRowSelectionAllowed(false);
for(int i=0;i
俄罗斯方块课:

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(){...}
}
公共类俄罗斯方块
{
int NUM_ROWS=20;
int NUM_COLS=10;
整数网格[];
int cur_row;
国际货币基金组织;
件颜色;
风格cur_风格;
下一个颜色;
样式下一个样式;
布尔覆盖;
公共俄罗斯方块()
{
网格=新整数[10][20];
对于(int i=0;i<10;i++)
{
对于(int j=0;j<20;j++)
{
grid[i][j]=Pieces.BLANK.getKey();
}
}
next_color=parties.createColor();
next_style=style.createStyle();
过度=错误;
创建_-Piece();
}
public void createPiece(){…}
公共无效设置项(){…}
公共无效删除程序OW(){…}
}
以及所有字段的moveLeft、moveRight、moveDown、rotateLeft、rotateRight、printGame和Getter和Setter。俄罗斯方块类中的所有方法都在控制台中进行了测试,并且工作正常。这是我到目前为止的成果。每次颜色都不一样,我很确定我知道为什么,但是我很难思考如何根据在Tetris类中创建的网格数组为每个单元格着色


这纯粹是概念的证明

基本上,你已经有了一个游戏板的模型。您需要能够将其建模回屏幕

我会将您的电路板模型包装在一个
表格模型中
,并与
表格单元渲染器
一起,允许两者一起工作

当线路板数据发生变化时,您所需要做的就是相应地更新表模型

如果你写的关系正确,边界
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(){...}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

public class TetrisTable {

    public static void main(String[] args) {
        new TetrisTable();
    }

    public TetrisTable() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTable table;
        private TetrisTabelModel model;
        private int currentRow = 0;
        private int blockHeight = 3;
        private int blockWidth = 3;

        public TestPane() {
            model = new TetrisTabelModel();
            table = new JTable(model);
            table.setDefaultRenderer(Integer.class, new TetrisTabelCellRenderer());
            table.setRowHeight(24);
            Enumeration<TableColumn> columns = table.getColumnModel().getColumns();
            while (columns.hasMoreElements()) {
                TableColumn column = columns.nextElement();
                column.setPreferredWidth(24);
                column.setMinWidth(24);
                column.setMaxWidth(24);
                column.setWidth(24);
            }
            setLayout(new GridBagLayout());
            add(table);

            Timer timer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    int col = (model.getColumnCount() - blockWidth) / 2;
                    int row = currentRow - blockHeight;
                    if (row + blockHeight >= model.getRowCount()) {
                        ((Timer) e.getSource()).stop();
                    } else {
                        drawShape(row, col, 0);
                        currentRow++;
                        row = currentRow - blockHeight;
                        drawShape(row, col, 3);
                    }

                }

                public void drawShape(int row, int col, int color) {

                    for (int index = 0; index < blockHeight; index++) {

                        if (row >= 0 && row < model.getRowCount()) {

                            switch (index) {
                                case 0:
                                case 1:
                                    model.setValueAt(color, row, col);
                                    break;
                                case 2:
                                    model.setValueAt(color, row, col);
                                    model.setValueAt(color, row, col + 1);
                                    model.setValueAt(color, row, col + 2);
                                    break;
                            }

                        }
                        row++;

                    }
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();

        }
    }

    public class TetrisTabelModel extends AbstractTableModel {

        private int[][] values;

        public TetrisTabelModel() {
            values = new int[20][10];
        }

        @Override
        public int getRowCount() {
            return values.length;
        }

        @Override
        public int getColumnCount() {
            return values[0].length;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return Integer.class;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return values[rowIndex][columnIndex];
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            values[rowIndex][columnIndex] = (int) aValue;
            fireTableCellUpdated(rowIndex, columnIndex);
        }
    }

    public class TetrisTabelCellRenderer extends DefaultTableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            super.getTableCellRendererComponent(table, "", false, false, row, column);
            setOpaque(true);
            if (value != null) {
                if (value == 0) {
                    setBackground(Color.WHITE);
                } else if (value == 1) {
                    setBackground(Color.RED);
                } else if (value == 2) {
                    setBackground(Color.GREEN);
                } else if (value == 3) {
                    setBackground(Color.BLUE);
                } else if (value == 4) {
                    setBackground(Color.YELLOW);
                }
            } else {
                setBackground(Color.DARK_GRAY);
            }
            return this;
        }
    }
}