【发布时间】:2013-12-09 06:23:31
【问题描述】:
所以我的问题如下:我正在创建俄罗斯方块的克隆。在我的一个课程中,我有一个 Jpanel,它可以绘制所有内容并具有一个绘制方法。
public class Action extends JPanel {
private final Color BACKGROUND_COLOR = Color.yellow;
public Action() {
setBackground(BACKGROUND_COLOR);
setPreferredSize(new Dimension(100, 220));
setVisible(true);
new Timer(1000, new TimerListener()).start();
}
@Override
protected void paintComponent(Graphics pen) {
super.paintComponent(pen);
pen.setColor(Color.gray);
GameMemory memory = new GameMemory();
int[][] grid = memory.getGrid();
for (int r = 0; r < grid.length; r++ )
for (int c = 0; c < grid[c].length; c++)
if (grid[r][c] == 1)
pen.fillRect(c * 30, r * 30, 40, 40);
}
private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Action.this.repaint();
}
}
}
我试图让它每秒重新绘制一次。
另一个类是一个数组,它保存在内存占用的单元格中,如果它们是,则将它们标记为状态 1。
/**
This class will contain a two-dimensional array that will
keep in itself the figures, as well as checking if the raws are
full.
*/
public class GameMemory {
private final int AXIS_X = 6; //Where axis of the
private final int AXIS_Y = 1; //figure will appear
private int curX = AXIS_X;
private int curY = AXIS_Y;
private final int[][] grid = new int[22][10];
FigureI figure = new FigureI();
GameMemory() {
Timer timer = new Timer(1000, new TimerListener());
timer.start();
}
public int[][] getGrid() {
return grid;
}
private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
curY ++;
grid[curY + figure.block0[1]][curX + figure.block0[0]] = 1;
grid[curY + figure.block1[1]][curX + figure.block1[0]] = 1;
grid[curY + figure.block2[1]][curX + figure.block2[0]] = 1;
grid[curY + figure.block3[1]][curX + figure.block3[0]] = 1;
System.out.println(Arrays.deepToString(grid)); // To debug.
}
}
第三个文件只包含图形坐标。
/**
This file contains all the figures.
*/
class FigureI {
int[] block1 = { 0, -1 };
int[] block0 = { 0, 0 };
int[] block2 = { 0, 1 };
int[] block3 = { 0, 2 };
}
class FigureJ {
int[] block1 = { 1, 0 };
int[] block0 = { 0, 0 };
int[] block2 = { -1, 0 };
int[] block3 = { -1, -1 };
}
class FigureL {
int[] block1 = { -1, 0 };
int[] block0 = { 0, 0 };
int[] block2 = { 1, 0 };
int[] block3 = { 1, 1 };
}
class FigureO {
int[] block1 = { 1, 0 };
int[] block0 = { 0, 0 };
int[] block2 = { -1, 0 };
int[] block3 = { -1, 1 };
}
class FigureS {
int[] block1 = { -1, 0 };
int[] block0 = { 0, 0 };
int[] block2 = { 0, 1 };
int[] block3 = { 1, 1 };
}
class FigureT {
int[] block1 = { 1, 0 };
int[] block0 = { 0, 0 };
int[] block2 = { -1, 0 };
int[] block3 = { 0, 1 };
}
class FigureZ {
int[] block1 = { 1, 0 };
int[] block0 = { 0, 0 };
int[] block2 = { -1, 1 };
int[] block3 = { 0, 1 };
}
我有几个问题,按重要性排序:
1)如何让 Action 类的绘制识别 Game 内存类随时间的变化?
2) 当我将网格和图形变量放入 Action 类中,并将 TimeListener 从 GameMemory 替换为 Action 时,图形确实会移动。如果我从另一个班级这样做有什么问题呢? 我试图将记忆与绘画分开,因为这对我来说似乎是合乎逻辑的。可能吗?
3) 我有一个包含一大堆包含相对图形坐标的包私有类的文件。一开始我试图让它成为一个包含私有内部类的公共类,但是我遇到了实例化内部私有类的问题,所以我正在考虑如何为我的带有数字的文件进行更好的设计。
我是一个完整的 Java 菜鸟,所以任何建议都将不胜感激。
【问题讨论】:
-
代码太多了。你能限制你的代码吗,这样可以让读者的生活更轻松。
-
好吧,我想你可以忽略带有数字的代码和第三个问题。有两个第一个 sn-ps 它不应该太多......