初学者的Java错误

时间:2014-12-21 17:39:15

标签: java

我刚开始学习Java。我写了一个这样的程序:

Bacteria.class

import java.awt.*;


public class Bacteria extends Creature{

public static final int COLOR_STEP = 256/MAX_AGE;
Bacteria(Board board, int _x, int _y){


    super.board = board;
    //initialize age, x, y
    super.age = 1;
    super.x = _x;
    super.y = _y;
}

//Draw a circle representing the creature at its position
//This function is done.

public void draw(Graphics g) {
    g.setColor(new Color(0, COLOR_STEP * age, 0));
    g.fillOval(x * board.CELL_SIZE, y * board.CELL_SIZE, board.CELL_SIZE, board.CELL_SIZE);
}
public void tick() {
    //your code
    // this is how the creature lives in one tick
    // make the creature get older if it is not dead.
    // if it's got older than MAX_AGE then it should die
    // ...call board.addDead() to register the dead creature
    // if it can have baby then try reproduce()
    age++;
    if (this.canHaveBaby()) {
        this.reproduce();
    }
    //this.reproduce();
    if (age > MAX_AGE) {
        board.addDead(this);
    }
}
}

Creature.class

import java.awt.*;


public class Creature {
public static final int MAX_AGE = 5;
public Board board;

public int age; // age of the creature measures in ticks
public int x; // (x,y) is the position of the creature
public int y;

Creature(Board board, int _x, int _y) {
    this.board = board;
    //initialize age, x, y
    age = 1;
    x = _x;
    y = _y;
}

public Creature() {
}


//Draw a circle representing the creature at its position
//This function is done.
public void draw(Graphics g){}

public void tick(){}

public void reproduce() {
    //your code
    // if it can have a baby then produce a baby
    // ...in an empty cell next to its cell.
    // board.emptyCell() should be used
    // ....to check whether a cell in the board is empty
    // and call board.addBaby() to place the baby to the board
    //int x_current = x, y_current = y;
    if(board.emptyCell(x, y+1)){
        Creature baby = new Creature(board,x, y+1);
        //System.out.println("1");
        board.addBaby( baby);
    }
    else if(board.emptyCell(x+1,y)){
        Creature baby = new Creature(board,x+1, y);
        board.addBaby(baby);
    }
    else if(board.emptyCell(x+1,y+1)){
        Creature baby = new Creature(board,x+1, y+1);
        board.addBaby(baby);
    }
    else if(board.emptyCell(x-1,y)){
        Creature baby = new Creature(board,x-1, y);
        board.addBaby(baby);
    }
    else if(board.emptyCell(x,y-1)){
        Creature baby = new Creature(board,x, y-1);
        board.addBaby(baby);
    }
    else if(board.emptyCell(x-1,y-1)){
        Creature baby = new Creature(board,x-1, y-1);
        board.addBaby(baby);
    }
    else if(board.emptyCell(x-1,y+1)){
        Creature baby = new Creature(board,x-1, y+1);
        board.addBaby(baby);
    }
    else if(board.emptyCell(x+1,y-1)){
        Creature baby = new Creature(board,x+1, y-1);
        board.addBaby(baby);
    }
}

public boolean canHaveBaby() {
    //your code
    // if the creature is dead or it is too young
    // then it cannot have a baby
    if(age == 0 || age > MAX_AGE){
        return false;
    }
    return true;
}

public boolean atPosition(int _x, int _y) {
    //your code
    // return true if the creature is at the position (_x,_y)
    // you need this function for board.empty to function correctly
    if(_x == x && _y == y){
        return true;
    }
    return false;
}
}

Board.class

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;


public class Board extends JPanel {

public static final int CELL_SIZE = 10;
private final int rows = 10;
private final int cols = 20;
ArrayList<Creature> creatureList;

ArrayList<Creature> babies, dead;
// ArrayList<Bacteria> bacteriaList;
// ArrayList<Bacteria> babies_bac, dead_bac;
Board() {
    super();
    setBackground(Color.white);
    creatureList = new ArrayList<Creature>();
    //creatureList.size();
    babies = new ArrayList<Creature>();
    dead = new ArrayList<Creature>();
    setPreferredSize(new Dimension(cols * CELL_SIZE, rows * CELL_SIZE));
    creatureList.add(new Bacteria(this, 1, 1));
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    for (Creature creature: creatureList) {
        creature.draw(g);
    }
}

public void tick() {
    dead = new ArrayList<Creature>();
    babies = new ArrayList<Creature>();

    for (Creature creature: creatureList) {
         creature.tick();
    }
    creatureList.addAll(babies);
    creatureList.removeAll(dead);
}

public void addBaby(Creature baby) {
    if (baby != null) babies.add(baby);
}

public void addDead(Creature deadCreature) {
    if (deadCreature != null) dead.add(deadCreature);
}

public boolean emptyCell(int x, int y) {
    if (x < 0 || y < 0 || x >= cols || y >= rows) return false;

    for (Creature creature : creatureList) {
        if (creature.atPosition(x, y)) {
            return false;
        }
    }
    for (Creature creature : creatureList) {
        if (creature.atPosition(x, y)) {
            return false;
        }
    }
    return true;
}

public String getPopulation() {
    return String.format("%d", creatureList.size());
}
}

和BacteriaSimulator.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class BacteriaSimulator extends JFrame {
private Board board;
private JLabel label;

public BacteriaSimulator() {
    initUI();
    setTitle("Bacteria Simulator");
    setSize(350, 200);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private void initUI() {
    JPanel panel = new JPanel();

    board = new Board();
    panel.add(board);

    label = new JLabel(board.getPopulation());
    panel.add(label);

    JButton button = new JButton("Tick");
    button.addActionListener(new ButtonNextListener());
    panel.add(button);

    add(panel);
    pack();
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            BacteriaSimulator ex = new BacteriaSimulator();
            ex.setVisible(true);
        }
    });
}

private class ButtonNextListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        board.tick();
        label.setText(board.getPopulation());
        repaint();
    }
}
}

我希望Board.class与creatureList一起使用。但是,董事会仍然使用细菌(creatureList.add(new Bacteria(this, 1, 1));)初始化。我遇到了一个问题,即Board.class无法运行我想要的draw()和tick()方法。如何绘制对象细菌并做出正确的tick()方法?你能帮助我吗!谢谢!

1 个答案:

答案 0 :(得分:0)

您的draw()和tick()函数是Bacteria.java的一部分。它们不能在Board.java中使用,除非您创建一个Bacteria实例,然后使用它调用方法。