简体   繁体   English

JPanels没有出现,有点

[英]JPanels not showing up, kinda

I am an in-school amateur making a snake game in java, it is not complete and i have run into a problem that i cant seem to fix. 我是一名在校业余爱好者,正在用Java做蛇游戏,但它并不完整,我遇到了我似乎无法解决的问题。 the first and second parts of the body show up just fine after eating the target, but after that they do not. 吃完目标后,身体的第一部分和第二部分会很好地显示出来,但是之后就没有了。

code that adds a body part: 添加正文部分的代码:

  public void addBody(int x, int y) {
    snekBody.add(new JPanel());
    snekBody.get(snekBody.size() - 1).setBackground(new Color(new Random().nextInt(255), new Random().nextInt(255), new Random().nextInt(255)));
    snekBody.get(snekBody.size() - 1).setVisible(true);
    snekBody.get(snekBody.size() - 1).setBounds(x*score, y*score, BODY_WIDTH, BODY_HEIGHT);
    game.add(snekBody.get(this.snekBody.size() - 1));
    revalidate();
  }
}

code that moves the body parts(moveUp, moveDown, etc.: 移动身体部位的代码(moveUp,moveDown等:

public synchronized void moveRight(int x, int y) {
    timer++;
    while (!this.right) {
      try {
        wait();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    head.setBounds(x, y, 20, 20);
    xCoords.add(x);
    yCoords.add(y);
    //snekBody is an ArrayList of type JPanel
    for(int i = 0; i < snekBody.size(); i++) {
      if (i > 0) {
        snekBody.get(i).setBounds(xCoords.get(timer-(10*score)), yCoords.get(timer-(10*score))+5, BODY_WIDTH, BODY_HEIGHT);
      } else {
        snekBody.get(i).setBounds(xCoords.get(timer-10), yCoords.get(timer-10)+5, BODY_WIDTH, BODY_HEIGHT);
      }
    }
    repaint();
    notifyAll();
    revalidate();
  }

code that calls addBody: 调用addBody的代码:

 if (snek.up) {
          snek.addBody(snek.xCoords.get(snek.timer-20), snek.yCoords.get(snek.timer-20));

the second picture is the snake when more than two targets have been eaten. 第二张图片是吃了两个以上目标时的蛇。 the panels for the body just stop showing up. 车身面板停​​止显示。 The first picture is the snake when two targets have been eaten 第一张照片是吃完两个目标后的蛇

原来,这些面板彼此重叠显示,因此为什么我看不到它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM