在此程序中,我们需要创建一个8x8的“ LifeCell”小部件网格。讲师没有提到小部件必须是Shape的对象,因此我继续使用GridLayout类。 GridLayout类工作正常(据我所知,因为没有视觉辅助可确认。)该程序的目的是玩“生命游戏”,用户可以单击其中一个LifeCell小部件并在之间切换状态为“存活”或“死亡”。

我的问题在很大程度上取决于如何绘制单元格。我的代码可能有问题,但我不确定100%。

程式2.java

public class Program2 extends JPanel implements ActionListener {
private LifeCell[][] board; // Board of life cells.
private JButton next; // Press for next generation.
private JFrame frame; // The program frame.

public Program2() {
    // The usual boilerplate constructor that pastes the main
    // panel into a frame and displays the frame. It should
    // invoke the "init" method before packing the frame
    frame = new JFrame("LIFECELL!");
    frame.setContentPane(this);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.init();
    frame.pack();
    frame.setVisible(true);
}
    public void init() {
    // Create the user interface on the main panel. Construct
    // the LifeCell widgets, add them to the panel, and store
    // them in the two-dimensional array "board". Create the
    // "next" button that will show the next generation.
    LifeCell[][] board = new LifeCell[8][8];
    this.setPreferredSize(new Dimension(600, 600));
    this.setBackground(Color.white);
    this.setLayout(new GridLayout(8, 8));
    // here is where I initialize the LifeCell widgets
    for (int u = 0; u < 8; u++) {
        for (int r = 0; r < 8; r++) {
            board[u][r] = new LifeCell(board, u, r);
            this.add(board[u][r]);
            this.setVisible(true);

        }
    }


LifeCell.java

 public class LifeCell extends JPanel implements MouseListener {
   private LifeCell[][] board; // A reference to the board array.
   private boolean alive;      // Stores the state of the cell.
   private int row, col;       // Position of the cell on the board.
   private int count;          // Stores number of living neighbors.

   public LifeCell(LifeCell[][] b, int r, int c) {
       // Initialize the life cell as dead.  Store the reference
       // to the board array and the board position passed as
       // arguments.  Initialize the neighbor count to zero.
       // Register the cell as listener to its own mouse events.
       this.board = b;
       this.row = r;
       this.col = c;
       this.alive = false;
       this.count = 0;
       addMouseListener(this);
   }


这是paintComponent方法:

   public void paintComponent(Graphics gr) {
       // Paint the cell.  The cell must be painted differently
       // when alive than when dead, so the user can clearly see
       // the state of the cell.
           Graphics2D g = (Graphics2D) gr;
           super.paintComponent(gr);
           g.setPaint(Color.BLUE);
   }


我不需要确切的解决方案来修复它,但是我竭尽全力尝试使其正常工作。

谢谢。

编辑:

我增加了Program2.java类的更多部分,明天我可以回去上床睡觉,感谢所有帮助人员。

编辑#2:

当我用8x8 GridLayout填充框架时,我的真正困惑是每个“单元”由于缺少更好的单词而类型为LifeCell。如何为每个LifeCell绘制不同的颜色?如果这对你们完全有意义,我可以尝试尽可能多地修改。 camickr,我将浏览该网站,谢谢。

可以找到here分配,以避免与我的问题和/或代码段有关的所有混淆。

最佳答案

java - Java Swing有关GridLayout和paintComponent方法的问题-LMLPHP


您在正确的轨道上。

如果要使用现有组件(如JPanel,JLabel,JButton等),则最好兑现该组件已执行的操作,而只是参数化所需的参数。

因此,在您使用JPanel的情况下,此(和其他JComponents)具有可以更改的background属性。因此,与其设置自己的颜色(现在是失败的方法),不如设置自己的颜色,而是设置该值并自己绘制。

您可以添加“ getLifeColor”,根据单元格状态返回不同的颜色:

   private Color getLifeColor() {
       return this.alive?liveColor:deadColor;
   }


然后让单元格用这种颜色绘制背景:

  public void paintComponent(Graphics gr) {
       setBackground( getLifeColor() );
       super.paintComponent( gr );
  }


之后,您只需将单元格的状态设置为活动或死亡,组件将以相应的颜色显示:

java - Java Swing有关GridLayout和paintComponent方法的问题-LMLPHP

Here's您发布的代码的简短自包含正确示例(SSCCE)+实时/无效颜色用法。我认为您可以从那里继续。

07-24 13:29