所以我的问题如下:我正在创建俄罗斯方 block 的克隆。在我的一个类中,我有一个 Jpanel,它可以绘制所有内容并有一个 Paint 方法。
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,则将它们标记为状态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 时,图形确实移动了。如果我从另一个类(class)做的话会有什么问题? 我试图将内存与绘画分开,因为这对我来说似乎合乎逻辑。是否可以?
3)我有一个文件,其中包含一大堆包含相对图形坐标的包私有(private)类。一开始我试图将其设为包含私有(private)内部类的公共(public)类,但是我遇到了实例化内部私有(private)类的问题,所以我正在考虑如何更好地设计我的带有数字的文件。
我是一个完全的 Java 菜鸟,所以任何建议将不胜感激。
最佳答案
不要在paintComponent()方法中调用GameMemory memory = new GameMemory()
。单独定义模型并重用它。您的渲染使用模型并仅反射(reflect) PaintComponent() 方法中的模型状态。
在您的 TimerListener 中,只需更新模型(移动数字、清除行等)并调用 View 的 repaint() 来反射(reflect)模型变化。
关于java - 为什么一个类的绘制看不到另一类的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20464414/