jframe没有更新

cuxqih21  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(202)

ps:这是为那些感兴趣的人准备的githubhttps://github.com/yasakidev/animalrescueup/tree/master
我正在swing中开发一个“宠物救援传奇”java游戏。我对荡秋千还是个新手,遇到了问题。
我希望我的碎片被移除时会掉下来。在这里,我已经设法通过改变它们的颜色来“移除它们”,但我希望它们像俄罗斯方块一样掉落。
这是我的密码:
grid(jframe和singleton类)

import javax.swing.*;
import java.awt.*;

public class Grid extends JFrame {
    private static Grid main;
    private Case[][] cases;

    public Grid() {
        this.cases = new Case[8][8];
        for (int i = 0; i < cases.length; i++) {
            for (int j = 0; j < cases[i].length; j++) {
                this.cases[i][j] = new Case(i, j);
                this.add(cases[i][j]);
            }
        }
        this.setSize(800, 800);
        this.setLayout(new GridLayout(8,8));
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public Case getCase(int i, int j) {
        return this.cases[i][j];
    }

    public static Grid getGrid() {
        if(main == null) main = new Grid();
        return main;
    }

    public void swap(int x1, int y1, int x2, int y2) {
        Case temp = this.cases[x1][y1];
        cases[x1][y1] = cases[x2][y2];
        cases[x2][y2] = temp;
    }

    public void rebuild() {
        for(int i = cases.length-1-1; i > 0; i--) {
            for (int j = cases[i].length-1-1; j > 0; j--) {
                int k = i;
                while(!cases[k+1][j].isActive()) {
                    this.swap(i, j, i+1, j);
                    k++;
                }
            }
        }
        //swap(5,5,5,6);
    }
}

case(它定义网格中的块,单击时,它们运行clicked(),并递归删除所有相邻的相同颜色的块)

import utils.RandomGen;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;

public class Case extends JPanel {
    private JButton btn;
    private Color color;
    private final int posX;
    private final int posY;

    private boolean isActive;

    public Case(int x, int y) {
        //Init Members
        this.posX = x;
        this.posY = y;

        //Init Components
        this.initColor();
        this.initButton();
        this.initPanel();
    }

    public void clicked() {
        this.setVisible(false);
        this.isActive = false;
        checkAdjacent();
        Grid.getGrid().rebuild;
    }

    private void initColor() {
        int i = RandomGen.get(0,4);
        switch(i) {
            case 0 -> this.color = new Color(255,0,0);
            case 1 -> this.color = new Color(0,0, 255);
            case 2 -> this.color = new Color(136, 0, 255);
            case 3 -> this.color = new Color(27, 255, 0);
            default -> this.color = new Color(0, 0, 0);
        }
    }

    private void initButton() {
        this.btn = new JButton();
        this.btn.setOpaque(false);
        this.btn.setContentAreaFilled(false);
        this.btn.setBorderPainted(false);
        this.btn.addActionListener(new ClickEventHandler(this));
    }

    private void initPanel() {
        this.setBorder(new LineBorder(new Color(0,0,0)));
        this.setBackground(this.color);
        this.add(btn);
        this.setLayout(new CardLayout());
        this.isActive = true;
        this.setVisible(true);
    }

    private void checkAdjacent() {
        //CLEAR DOWN
        if(this.posX != 7 && Grid.getGrid().getCase(this.posX+1, this.posY).isActive && Grid.getGrid().getCase(this.posX+1, this.posY).getColor().getRGB() == this.color.getRGB()) Grid.getGrid().getCase(this.posX+1, this.posY).clicked();
        //CLEAR UP
        if(this.posX != 0 && Grid.getGrid().getCase(this.posX-1, this.posY).isActive && Grid.getGrid().getCase(this.posX-1, this.posY).getColor().getRGB() == this.color.getRGB()) Grid.getGrid().getCase(this.posX-1, this.posY).clicked();
        //CLEAR RIGHT
        if(this.posY != 7 && Grid.getGrid().getCase(this.posX, this.posY+1).isActive && Grid.getGrid().getCase(this.posX, this.posY+1).getColor().getRGB() == this.color.getRGB()) Grid.getGrid().getCase(this.posX, this.posY+1).clicked();
        //CLEAR LEFT
        if(this.posY != 0 && Grid.getGrid().getCase(this.posX, this.posY-1).isActive && Grid.getGrid().getCase(this.posX, this.posY-1).getColor().getRGB() == this.color.getRGB()) Grid.getGrid().getCase(this.posX, this.posY-1).clicked();
    }

    public Color getColor() {
        return color;
    }

    public int getPosX() {
        return posX;
    }

    public int getPosY() {
        return posY;
    }

    public boolean isActive() {
        return isActive;
    }
}

下面是随机生成的问题网格的两个截图
例如,当单击红色图案时,我希望其余的块落下
我试着使用grid.rebuild()使这些块掉落,但显示器上没有任何变化。
如果你发现了坏习惯或错误,请把它们指给我看。
谢谢你的帮助!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题