【发布时间】:2015-08-13 16:21:32
【问题描述】:
我正在尝试制作俄罗斯方块克隆。游戏使用JTable 作为棋盘的表示。棋盘是一个二维整数数组。
我正在努力做到这一点,当某个单元格具有某个值时,该单元格将变为某种颜色。我以为我让它正常工作,但它不工作。非常感谢您的帮助。
谢谢。
这是我的代码:
董事会:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.Timer;
import javax.swing.table.*;
/**
* @author _______________
*
* Board.java
*
* The Board class gives information to the InitializeJTable class regarding
* the data-type of the JTable elements, how many rows/columns are in the JTable, etc;
*
* I extended Board with AbstractTableModel, a class that allows me to display any data-type
* inside the elements of the JTable.
*
* This class also has method declarations that are required by AbstractTableModel, these methods are:
* - getRowCount()
* - getColumnCount()
* - getValueAt(int, int)
*
*/
@SuppressWarnings("serial")
public class Board extends AbstractTableModel {
TableColorSetter TCS;
boolean lost = false;
InitializeGUI iGUI;
InitializeJTable iJT;
public int SPEED = 1000;
L l = new L();
public Block currentBlock = l;
public int[][] boxes;//A 2D Array of booleans that defines the status of the Tetris board. Used by InitializeJTable and InitializePreviewJTable
/*
* boardType is an integer that works with the constructor.
*
* If boardType is equal to 0, the constructor generates a 22x10 array (This is used with the main board)
* However, if boardType is equal to 1, the constructor generates a 4x4 array (This is used with the preview/hold board, a smaller board on the side)
*/
public Board(int boardType) {
if(boardType == 0) {
boxes = new int[22][10];
loop();
} else if(boardType == 1) boxes = new int[4][4];
}
/*
* getRowCount()
*
* getRowCount is a required declaration by the AbstractTableModel class.
* @return the amount of rows in the boxes boolean
*/
@Override
public int getRowCount() {
return boxes.length;
}
public int getTetrisType(Block block) {
return block.getTetrisType();
}
public void letPieceDown(int origA, int origB, int tetrisType) {
boxes[origA][origB] = 0;
boxes[origA + 1][origB] = tetrisType;
}
/*
* getColumnCount()
*
* getColumnCount() is a required declaration by the AbstractTableModel class.
* @return the amount of columns in the boxes boolean
*/
@Override
public int getColumnCount() {
return boxes[0].length;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
boxes[rowIndex][columnIndex] = (int)aValue;
fireTableCellUpdated(rowIndex, columnIndex);
}
/*
* getValueAt()
*
* getValueAt() is a required declaration by the AbstractTableModel class.
* returns the amount of columns in the boxes boolean
*
* @param int rowIndex The row index of the boolean that you want to return.
* int columnIndex The column index of the boolean that you want to return.
*
* @return the value of the boolean at the specified location in the boolean array.
*/
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return boxes[rowIndex][columnIndex];
}
public int getVal(int row, int col) {
return boxes[row][col];
}
/**
*
* loop();
*
* The loop() method is a very rough draft in regards to placing Tetris objects on the board and having them move, and stack.
*
* TODO:
*/
public void loop() {
Timer timer = new
Timer(SPEED, new ActionListener() {
int x = 0;
int y = 0;
@Override
public void actionPerformed(ActionEvent e) {
while(lost = false) {
for(int a = 0; a < getColumnCount(); a++) {
for(int b = 0; b < getRowCount(); b++) {
TCS.getTableCellRendererComponent(iJT.getTable(), "", false, false, b, a);
}
}
}
if(x == getRowCount() - 1) {
x = 0;
} else if(boxes[x + 1][y] != 0 && x == 0) {
boxes[x][y] = getTetrisType(currentBlock);
fireTableDataChanged();
//System.out.println("GAME OVER!");
//y++; //Just for testing.
PlaySound sound = new PlaySound();
sound.setSoundType(1);
sound.start();
((Timer)e.getSource()).stop();
} else if(boxes[x + 1][y] != 0) {
x = 0;
} else {
//System.out.println("Pos: " + x + ", 0. Row count: " + getRowCount() + " -- " + x);
letPieceDown(x, y, getTetrisType(currentBlock));
fireTableDataChanged();
x++;
}
}
});
timer.start();
}
}
初始化JTable:
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
/**
* @author _________________
*
* InitializeJTable.java
*
* The InitializeJTable class sets up the JTable to look like a Tetris board.
*
* @TODO: Add more commenting
*/
@SuppressWarnings("serial")
public class InitializeJTable extends JPanel {
private int COLS = 10;
private int HEIGHT = 30;
Color white = Color.WHITE;
Color black = Color.BLACK;
Board tetrisBoard;
JTable table;
public Board returnBoard() {
return tetrisBoard;
}
public JTable getTable() {
return table;
}
public InitializeJTable() { //The InitializeJTable constructor
tetrisBoard = new Board(0); //Create a new board of 22x10 booleans, with a boardType of 0.
table = new JTable(tetrisBoard); //Apply the information from the Board constructor to the JTable. (Information like, how many rows/cols in the JTable, what data-type the JTable elements are, etc)
//setLayout(new GridBagLayout()); //Sets the Layout of the JTable to a GridBagLayout. This means, it makes the JTable look like a grid.
table.setCellSelectionEnabled(false); //Disables dragging/selection of columns on the JTable with the mouse. We won't be needing that.
TableColumnModel columns = table.getColumnModel(); //Gets information about the columns of the JTable. I need this to set the length of the columns (to make them square)
table.setRowHeight(HEIGHT); //Sets the height of the JTable rows to HEIGHT int, currently set to 30. Can be easily changed.
//table.setForeground(white); //Use this to make all blocks white. Still need to make it work correctly.
/*
* Sadly, there isn't a -- table.setColumnHeight(int) -- method in the JTable API. (to set the length of the columns to the specified integer)
*
* So I have to manually set the width of each column. This is why I declared the TableColumnModel above
*/
for(int a = 0; a < COLS; a++) {
columns.getColumn(a).setPreferredWidth(HEIGHT); //Sets the height of the JTable columns to HEIGHT int, currently set to 30. Can be changed easily.
}
add(table);//The table's all set up correctly, add it.
}
}
【问题讨论】:
-
欢迎来到 StackOverflow。请提供一个最小的可重现示例和一个特定的问题,而不是调试代码的请求。见how-to-ask。
标签: java swing colors jtable tablecellrenderer