【问题标题】:Tetris GUI by coloring JButtons. Problems with rendering通过为 JButton 着色的俄罗斯方块 GUI。渲染问题
【发布时间】:2013-03-10 03:01:23
【问题描述】:

我正在制作一个俄罗斯方块游戏,对于我的 GUI,我选择为 JButton 着色以用作我的俄罗斯方块板。我设置了一个 JButton 网格。我计划遍历从

返回的俄罗斯方块网格
newGrid = game.gamePlay(oldGrid);

并根据每个网格元素中的整数为每个 JButton 着色。返回的俄罗斯方块网格是一个整数数组,每个数字代表一种颜色。到目前为止,我还没有用户交互,我只是想拥有基本的 GUI,其中块直接下降。

final JPanel card3 = new JPanel();
// Tetris setup
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) {
    card3.remove(0); //remove start button

    Game game = new Game();
    int[][] oldGrid = null;
    int[][] newGrid = null;
    boolean firstTime = true;

    JButton[][] grid; // tetris grid of buttons
    card3.setLayout(new GridLayout(20, 10));
    grid = new JButton[20][10];
    for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 10; j++) {
            grid[i][j] = new JButton();
            card3.add(grid[i][j]);
        }
    }               

    while (true) {
            if (firstTime) {
                newGrid = game.gamePlay(null);
            } else {
                newGrid = game.gamePlay(oldGrid);
            }

            //Coloring Buttons based on grid

            oldGrid = newGrid;
            firstTime = false;
            card3.revalidate();
        }
    }
});

这是来自 Game 类的代码

public class Game
{
    static Tetris game;

    public int[][] gamePlay(int[][] grid) {
        if (grid == null) {
            game = new Tetris();
            System.out.println("first time");
        }
        else {
                game.setGrid(grid);
            }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        game.move_Down();
        game.print_Game();

        return game.getGrid();
    }
}

游戏.print_Game();将网格打印到控制台窗口,以便我可以从文本中看到正在发生的事情。但是 card3.revalidate();似乎没有工作,因为 GUI 在打印开始时暂停。如果我将 revalidate 移到 while 循环之前,然后注释掉 while 循环,则 GUI 输出:

这就是我想要的。但是为了给按钮涂上某种颜色,我需要在网格发生变化时在 while 循环中进行重新验证。

有什么建议吗?

【问题讨论】:

  • 我可以问... 为什么你在使用JButtons?
  • 使用 javax.swing.Timer 代替 while 循环
  • 我实际上决定使用 JTable...老实说,我不知道我在想什么。

标签: java swing user-interface jbutton tetris


【解决方案1】:
  1. 使用GridLayout(更简单的LayoutManager)代替GridBagLayout

  2. 使用Swing Timer 而不是Runnable#Thread

  3. while (true) { 是无限循环

  4. Thread.sleep(1000); 可以冻结 Swing GUI 直到睡眠结束,Thread.sleep 的无限循环可能导致不负责任的应用程序

  5. 那里看不到 JButton.setBackground(somecolor)

  6. 使用 KeyBindings(添加到JButtons container)进行旋转

【讨论】:

  • 我决定使用 JTable。出于某种原因,因为俄罗斯方块通常具有 3D 外观块,我认为使用按钮会产生类似的效果。我知道无限循环就在那里,我只是想看看我是否可以让碎片落到位。
猜你喜欢
  • 1970-01-01
  • 2011-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多