jframe没有更新

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

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

  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class Grid extends JFrame {
  4. private static Grid main;
  5. private Case[][] cases;
  6. public Grid() {
  7. this.cases = new Case[8][8];
  8. for (int i = 0; i < cases.length; i++) {
  9. for (int j = 0; j < cases[i].length; j++) {
  10. this.cases[i][j] = new Case(i, j);
  11. this.add(cases[i][j]);
  12. }
  13. }
  14. this.setSize(800, 800);
  15. this.setLayout(new GridLayout(8,8));
  16. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  17. }
  18. public Case getCase(int i, int j) {
  19. return this.cases[i][j];
  20. }
  21. public static Grid getGrid() {
  22. if(main == null) main = new Grid();
  23. return main;
  24. }
  25. public void swap(int x1, int y1, int x2, int y2) {
  26. Case temp = this.cases[x1][y1];
  27. cases[x1][y1] = cases[x2][y2];
  28. cases[x2][y2] = temp;
  29. }
  30. public void rebuild() {
  31. for(int i = cases.length-1-1; i > 0; i--) {
  32. for (int j = cases[i].length-1-1; j > 0; j--) {
  33. int k = i;
  34. while(!cases[k+1][j].isActive()) {
  35. this.swap(i, j, i+1, j);
  36. k++;
  37. }
  38. }
  39. }
  40. //swap(5,5,5,6);
  41. }
  42. }

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

  1. import utils.RandomGen;
  2. import javax.swing.*;
  3. import javax.swing.border.LineBorder;
  4. import java.awt.*;
  5. public class Case extends JPanel {
  6. private JButton btn;
  7. private Color color;
  8. private final int posX;
  9. private final int posY;
  10. private boolean isActive;
  11. public Case(int x, int y) {
  12. //Init Members
  13. this.posX = x;
  14. this.posY = y;
  15. //Init Components
  16. this.initColor();
  17. this.initButton();
  18. this.initPanel();
  19. }
  20. public void clicked() {
  21. this.setVisible(false);
  22. this.isActive = false;
  23. checkAdjacent();
  24. Grid.getGrid().rebuild;
  25. }
  26. private void initColor() {
  27. int i = RandomGen.get(0,4);
  28. switch(i) {
  29. case 0 -> this.color = new Color(255,0,0);
  30. case 1 -> this.color = new Color(0,0, 255);
  31. case 2 -> this.color = new Color(136, 0, 255);
  32. case 3 -> this.color = new Color(27, 255, 0);
  33. default -> this.color = new Color(0, 0, 0);
  34. }
  35. }
  36. private void initButton() {
  37. this.btn = new JButton();
  38. this.btn.setOpaque(false);
  39. this.btn.setContentAreaFilled(false);
  40. this.btn.setBorderPainted(false);
  41. this.btn.addActionListener(new ClickEventHandler(this));
  42. }
  43. private void initPanel() {
  44. this.setBorder(new LineBorder(new Color(0,0,0)));
  45. this.setBackground(this.color);
  46. this.add(btn);
  47. this.setLayout(new CardLayout());
  48. this.isActive = true;
  49. this.setVisible(true);
  50. }
  51. private void checkAdjacent() {
  52. //CLEAR DOWN
  53. 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();
  54. //CLEAR UP
  55. 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();
  56. //CLEAR RIGHT
  57. 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();
  58. //CLEAR LEFT
  59. 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();
  60. }
  61. public Color getColor() {
  62. return color;
  63. }
  64. public int getPosX() {
  65. return posX;
  66. }
  67. public int getPosY() {
  68. return posY;
  69. }
  70. public boolean isActive() {
  71. return isActive;
  72. }
  73. }

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

暂无答案!

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

相关问题