2016-12-20 67 views
0

我是一個在java中做蛇遊戲的校內業餘愛好者,它並不完整,我遇到了一個我無法修復的問題。在吃完目標後,身體的第一部分和第二部分表現得很好,但在那之後他們沒有。JPanels沒有顯示出來,有點

代碼,增加了身體的一部分:移動所述身體部位(爲moveUp,下移等:

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(); 
    } 

代碼調用addBody

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(); 
    } 
} 

代碼:

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

+0

你可以包含一張圖片或更好的描述什麼沒有顯示?您的JPanel是否完全不顯示,或者只是您嘗試呈現的那部分蛇? –

+0

添加了蛇的圖片 – LegendaryGoomba

回答

0

原來,面板顯示在對方之上,因此我看不到它們。