2017-05-09 76 views
1

我一直在爲此工作了大約四個小時。我擁有所有的邏輯關係,定時器工作,鍵盤和鼠標輸入工作,但不調用paintComponent方法來繪製更新!我對這些實時應用程序沒有經驗,所以可能是任何導致這種情況不起作用的事情。任何幫助,將不勝感激!卡住爪哇Swing項目:康威的生命遊戲

gamemain.java

import javax.swing.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.Graphics; 

public class gamemain extends JPanel { 

public static boolean paused = true; 
public static JFrame gui; 
public static JPanel panel; 

static int[][] worldsize = new int[1000][1000]; 
static world playspace = new world(worldsize); 

static Timer timer = new Timer(250, new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     System.out.println("Debug: timer ticked"); 
     playspace.updateWorldState(); 
     panel.repaint(); 

    } 

}); 

@Override 
public void paintComponent(Graphics g) { 

    super.paintComponent(g); 
    System.out.println("Hit painter"); 
    g.drawString("Test", 50, 50); 

    for(int i = 0; i < 999; i++) { 
     for(int j = 0; j < 999; i++) { 
      if(playspace.getWorldStateAt(i, j) == 1) { 
       g.drawRect(i, j, 1, 1); 
      } 
     } 
    } 

} 

public static class listener implements KeyListener { 

    @Override 
    public void keyTyped(KeyEvent e) { 

     if(e.getKeyChar() == ' ') { 
      if(paused) { 
       timer.start(); 
       paused = false; 
       System.out.println("Unpaused"); 
      } 
      else { 
       timer.stop(); 
       paused = true; 
       System.out.println("Paused"); 
      } 
     } 

    } 

    @Override 
    public void keyPressed(KeyEvent e) { 
     // unused 

    } 

    @Override 
    public void keyReleased(KeyEvent e) { 
     // unused 

    } 

} 

public static class mouselistener implements MouseListener { 

    @Override 
    public void mouseClicked(MouseEvent e) { 

     int x, y; 
     x = e.getX(); 
     y = e.getY(); 
     playspace.placeCell(x, y); 

    } 

    @Override 
    public void mousePressed(MouseEvent e) { 
     // unused 

    } 

    @Override 
    public void mouseReleased(MouseEvent e) { 
     // unused 

    } 

    @Override 
    public void mouseEntered(MouseEvent e) { 
     // unused 

    } 

    @Override 
    public void mouseExited(MouseEvent e) { 
     // unused 

    } 

} 

public static void main(String[] args) throws InterruptedException { 

    SwingUtilities.invokeLater(new Runnable() { 
    public void run() { 
    gui = new JFrame(); 
    gui.setTitle("Game of Life"); 
    gui.setSize(1000, 1000); 
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    panel = new JPanel(); 
    panel.addKeyListener(new listener()); 
    panel.addMouseListener(new mouselistener()); 
    panel.setBackground(Color.WHITE); 
    panel.setOpaque(true); 
    Container pane = gui.getContentPane(); 
    pane.add(panel); 
    gui.setVisible(true); 
    panel.requestFocus(); 
     } 
    }); 


} 


} 

world.java

public class world { 

int[][] worldstate = new int[1000][1000]; 

public world(int[][] world) { 

    for(int i = 0; i < 1000; i++) { // make all cells dead 
     for(int j = 0; j < 1000; j++) { 

      world[i][j] = 0; 

     } 
    } 

    this.worldstate = world; 

} 

public int getWorldStateAt(int i, int j) { 

    return this.worldstate[i][j]; 

} 

public void placeCell(int x, int y) { 

    this.worldstate[x][y] = 1; 

} 

public void updateWorldState() { 

    int n1,n2,n3,n4,n5,n6,n7,n8,total; // all possible buddies of a cell 

    for(int i = 1; i < 999; i++) { // go to each cell 
     for(int j = 1; j < 999; j++) { 

      n1 = this.worldstate[i-1][j]; 
      n2 = this.worldstate[i][j-1]; 
      n3 = this.worldstate[i-1][j-1]; 
      n4 = this.worldstate[i+1][j]; 
      n5 = this.worldstate[i][j+1]; 
      n6 = this.worldstate[i+1][j+1]; 
      n7 = this.worldstate[i+1][j-1]; 
      n8 = this.worldstate[i-1][j+1]; 

      total = n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8; // add them up 

      if(total < 2 && (this.worldstate[i][j] == 1)) { // rule 1 

       this.worldstate[i][j] = 0; 

      } 

      if(total > 3 && (this.worldstate[i][j] == 1)) { //rule 3 

       this.worldstate[i][j] = 0; 

      } 

      if((total == 3) && (this.worldstate[i][j] == 0)) { //rule 4 

       this.worldstate[i][j] = 1; 

      } 

     } 
    } 

} 

} 

回答

3

paintComponent永遠不會被調用,因爲gamemain是從來沒有真正加入到能夠顯示任何內容。

你的第一個任務是去除依賴static

東西也許更喜歡...

public class gamemain extends JPanel { 

    public boolean paused = true; 

    int[][] worldsize = new int[1000][1000]; 
    world playspace = new world(worldsize); 

    Timer timer = new Timer(250, new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 

      System.out.println("Debug: timer ticked"); 
      playspace.updateWorldState(); 
      repaint(); 

     } 

    }); 

    @Override 
    public void paintComponent(Graphics g) { 

     super.paintComponent(g); 
     System.out.println("Hit painter"); 
     g.drawString("Test", 50, 50); 

     for(int i = 0; i < 999; i++) { 
      for(int j = 0; j < 999; i++) { 
       if(playspace.getWorldStateAt(i, j) == 1) { 
        g.drawRect(i, j, 1, 1); 
       } 
      } 
     } 

    } 

    //... 

    public static void main(String[] args) throws InterruptedException { 

     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       JFrame gui = new JFrame(); 
       gui.setTitle("Game of Life"); 
       gui.setSize(1000, 1000); 
       gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       gamemain panel = new gamemain(); 
       panel.addKeyListener(new listener()); 
       panel.addMouseListener(new mouselistener()); 
       panel.setBackground(Color.WHITE); 
       panel.setOpaque(true); 
       Container pane = gui.getContentPane(); 
       pane.add(panel); 
       gui.setVisible(true); 
       panel.requestFocus(); 
      } 
     }); 


    } 

} 

那麼我會集中精力處理更多的信息傳遞給gamemain類的能力,例如worldszieworld屬性。

我還可以考慮使用Key Bindings APIKeyListener,這將有助於解決的焦點相關的問題與KeyListener

我也建議在看看Code Conventions for the Java Programming Language - 這將使它更容易爲其他人閱讀你的代碼和你的閱讀他們

+0

謝謝,你能解釋我需要添加gamemain到什麼?它已經繪製了一個JFrame和JPanel。 –

+0

@MichaelSimanski您需要將它添加到可以顯示的內容中,如主窗口,請參閱更新示例 – MadProgrammer

+0

好的,按照您的說法操作。現在我在「gamemain panel = new JPanel();」上遇到類型不匹配錯誤;「 –