2017-11-29 2 views
0

새 게임 작업에서 만드는 스네이크 게임을 다시 채색하려고합니다. 작동하지만 이전 뱀 본체 또는 블록을 화면에서 지우지 않습니다.Java 다시 그리기 gridlayout

public class View extends JFrame implements ActionListener { 

private static final long serialVersionUID = -2542001418764869760L; 
public static int viewWidth = 20; 
public static int viewHeight = 20; 
private SidePanel side; 

private JMenuBar menuBar; 
private JMenuItem newGameButton; 
private JMenu menu, mode; 
private SnakeController sController; 

private GamePanel gamePanel; 

/* 
* Initialize the game's panels and add them to the window. 
*/ 
public View() { 

    menuBar = new JMenuBar(); 
    menu = new JMenu("Menu"); 
    menuBar.add(menu); 

    newGameButton = new JMenuItem("New Game"); 
    menu.add(newGameButton); 
    newGameButton.addActionListener(this); 

    mode = new JMenu("Mode"); 
    menuBar.add(mode); 

    this.setJMenuBar(menuBar); 
    this.gamePanel = new GamePanel(this); 

    this.side = new SidePanel(this); 

    this.add(gamePanel, BorderLayout.CENTER); 
    this.add(side, BorderLayout.EAST); 

    Tuple position = new Tuple(10, 10); 
    sController = new SnakeController(position, side, gamePanel); 
    this.addKeyListener((KeyListener) new Listener()); 
    // this.requestFocus(); 

    pack(); 

} 

public static void main(String args[]) { 
    View window = new View(); 
    window.setTitle("og-snake"); 
    window.setSize(700, 400); 
    window.setVisible(true); 
    window.setResizable(false); 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

} 

@Override 
public void actionPerformed(ActionEvent e) { 
    if (e.getSource() == newGameButton) { 
     System.out.println("clicked"); 

     gamePanel.removeAll(); 
     gamePanel.revalidate(); 
     gamePanel.repaint(); 

     Tuple position = new Tuple(10, 10); 

     sController = new SnakeController(position, side, gamePanel); 
     sController.start(); 


    } 

} 

이 기본적으로보기는 게임 패널의 새로운 게임 액션을 수행 수행 측면 패널과 보드 panel.The 작업을 추가하게 내 주요 클래스입니다.

public class GamePanel extends JPanel { 

public static ArrayList<ArrayList<ColorCell>> snakeGrid; 
public static int viewWidth = 20; 
public static int viewHeight = 20; 
ArrayList<ColorCell> data; 
SnakeController sc ; 
private View game; 

public GamePanel(View game) { 
    this.game = game; 
    this.snakeGrid = new ArrayList<ArrayList<ColorCell>>(); 
    this.data = new ArrayList<ColorCell>(); 
    for (int i = 0; i < viewWidth; i++) { 
     data = new ArrayList<ColorCell>(); 
     for (int j = 0; j < viewHeight; j++) { 
      ColorCell c = new ColorCell(2); 
      data.add(c); 
     } 
     snakeGrid.add(data); 
    } 
    setLayout(new GridLayout(viewWidth, viewHeight, 0, 0)); 
    setPreferredSize(new Dimension(400,400)); 

} 

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    for (int i = 0; i < viewWidth; i++) { 
     for (int j = 0; j < viewHeight; j++) { 
      add(snakeGrid.get(i).get(j).viewCell); 
     } 
    } 
} 

} 

업데이트 : spawning two at a time

답변

2
gamePanel.removeAll(); 
    gamePanel.fillGrid(); 
    gamePanel.revalidate(); 
    gamePanel.repaint(); 
    Tuple position = new Tuple(10, 10); 
    this.gamePanel = new GamePanel(this); 
    this.add(gamePanel,BorderLayout.CENTER); 

그 코드는 정말 아무것도하지 않습니다. BorderLayout의 CENTER에 컴퍼넌트를 추가하면 (자), 원의 컴퍼넌트는 옮겨지지 않습니다.

스윙 페인팅이 작동하는 방법은 추가 된 마지막 구성 요소가 먼저 페인트되는 것입니다. 즉, 새로 추가 된 패널이 칠해진 다음 새로 추가 된 패널의 맨 위에 원래 패널이 칠해진다는 의미입니다.

원래 gamePanel에서 모든 구성 요소를 제거하는 논리가 있으므로 새로운 gamePanel을 만들 필요가 없습니다. 게임 패널의 상태를 재설정하면됩니다.

+0

좋아, 지금은 알겠지만, 지금은 단지 오래된 뱀을 제거하지 않고 산란을 재현하는 것입니다. 나는 스네이크 그리드 객체를 지우고 다시 채우려고했지만, 언제나 나는 보드를 지울 때 첫 번째 이미지를 얻을 것입니다. 내 업데이트를위한 다른 이미지를 추가했습니다. – 3rdeye7

+0

이미지가 도움이되지 않습니다. 당신은 당신의 논리를 썼습니다. 보드를 지우려면 코드에 "재설정"논리를 추가해야합니다. 나는 당신의 게임이 어떻게 작동하는지 모르기 때문에 그렇게하는 법을 당신에게 말할 수 없습니다. – camickr

+0

안녕하세요, 모든 논리와 코드를 업데이 트했습니다 내 문제는 내가 초기화하고 내가 다시 칠할 때 snakeGrid 배열에 할당하는 방법을 생각합니다. 나는 전체 snakeGrid arraylist를 지우려고했지만이 didnt 작업 중 하나. – 3rdeye7