Java应用程序无法正确呈现

时间:2013-11-06 10:16:17

标签: java rendering conways-game-of-life

我正在制作"生命游戏"的基本版本。在Java中,但我似乎无法正确呈现单元格。以下是该软件包中仅有的两个类。

这是第一堂课:

package gameLogics;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Game extends Canvas implements Runnable {

    public static final int width = 128;
    public static final int height = 96;
    public static final String name = "The Game of Life";
    public static final int cellWidth = 6;

    private JFrame frame;

    public static boolean running = false;

    public static ObjCell[] objCell = new ObjCell[width * height];

    public Game() {

        setPreferredSize(new Dimension(width * cellWidth, height * cellWidth));

        frame = new JFrame(name);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(this, BorderLayout.CENTER);
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        // addKeyListener(this);

        for (int i = 0; i < objCell.length; i++) {
            objCell[i] = new ObjCell(i % Game.height, (int) Math.floor(i
                    / Game.height));
            objCell[i].on = Math.random() < 0.5;
        }

    }

    public static void main(String[] args) {

        new Game().start();

    }

    private void start() {

        running = true;
        new Thread(this).start();

    }

    public void run() {

        while (running) {

            // for (int i = 0; i < objCell.length; i++) {
            // if (objCell[i] == null) {
            // System.out.println("objCell[" + i
            // + "] was the first null value.");
            // break;
            // }
            // }

            for (int i = 0; i < objCell.length; i++)
                objCell[i].countNeighbours();
            for (int i = 0; i < objCell.length; i++)
                objCell[i].changeState();

            render();

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }

    public void render() {

        BufferStrategy bs = getBufferStrategy();

        if (bs == null) {
            createBufferStrategy(3);
            bs = getBufferStrategy();
        }

        Graphics2D g = (Graphics2D) bs.getDrawGraphics();

        // Draw background
        g.setPaint(Color.white);
        g.fill(new Rectangle2D.Double(0, 0, width * cellWidth, height
                * cellWidth));

        // Draw cells
        g.setPaint(Color.black);
        for (int i = 0; i < objCell.length; i++) {
            if (objCell[i].on)
                g.fill(new Rectangle2D.Double(objCell[i].x * cellWidth,
                        objCell[i].y * cellWidth, cellWidth, cellWidth));
        }

        g.dispose();

        bs.show();

    }

}

这是第二堂课:

package gameLogics;

public class ObjCell {

    public int x, y;
    public boolean on = false;

    private short neighbours;

    public ObjCell(int x, int y) {

        this.x = x;
        this.y = y;

    }

    public void countNeighbours() {

        neighbours = 0;
        if (inScreen(x - 1, y - 1)
                && Game.objCell[x - 1 + (y - 1) * Game.height].on)
            neighbours++;
        if (inScreen(x, y - 1) && Game.objCell[x + (y - 1) * Game.height].on)
            neighbours++;
        if (inScreen(x + 1, y - 1)
                && Game.objCell[x + 1 + (y - 1) * Game.height].on)
            neighbours++;
        if (inScreen(x + 1, y) && Game.objCell[x + 1 + y * Game.height].on)
            neighbours++;
        if (inScreen(x + 1, y + 1)
                && Game.objCell[x + 1 + (y + 1) * Game.height].on)
            neighbours++;
        if (inScreen(x, y + 1) && Game.objCell[x + (y + 1) * Game.height].on)
            neighbours++;
        if (inScreen(x - 1, y + 1)
                && Game.objCell[x - 1 + (y + 1) * Game.height].on)
            neighbours++;
        if (inScreen(x - 1, y) && Game.objCell[x - 1 + y * Game.height].on)
            neighbours++;

    }

    public void changeState() {

        on = neighbours == 2 && on || neighbours == 3;

    }

    private boolean inScreen(int x, int y) {

        return x >= 0 && x < Game.width && y >= 0 && y < Game.height;

    }

}

对不起,我无法提供更多信息,但我真的不知道出了什么问题。

提前致谢。

1 个答案:

答案 0 :(得分:0)

你是怎么说你不能让细胞正确渲染?

可能存在一些问题(但是如果没有完全理解您的代码,我将无法给出明确的答案 - 这实际上更适合作为评论,但我无法发表评论):

  • 您之前是否将所有“开启”值设置为false / True(我会假设您需要填充初始种子?)
  • 您正在定义一维数组...... ObjCell[] ...您可以使用二维数组(ObjCell[width][height]或类似数据)并更改检查邻居逻辑?

此链接可能帮助:Game of Life in Java, Overpopulation but can't figure out why